JUnit and spring consolidation, using spring managed objects for dependency injection to facilitate testing

Source: Internet
Author: User
Tags log4j

When you write JUnit single measurements using spring, you can use the annotations to start and configure a jar package for spring Pom to introduce the

<properties> <junit.version>4.9</junit.version> <slf4j.version>1.6.4</slf4j.ver Sion> <log4j.version>1.2.17</log4j.version> <spring.version>4.1.3.release</spring. version> </properties> <dependencies> <dependency> <groupid>junit</gro Upid> <artifactId>junit</artifactId> <version>${junit.version}</version&gt
            ;
            <scope>test</scope> </dependency> <!--log component--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <v ersion>${slf4j.version}</version> </dependency> <dependency> <groupid& Gt;org.slf4j</groupid> <artifactId>slf4j-log4j12</artifactId> <version>${s Lf4j.version}</version> </dependency> <dependency> <groupid>log4j</groupid&gt
            ; <artifactId>log4j</artifactId> <version>${log4j.version}</version> </depend ency> <!--Spring--> <dependency> <groupid>org.springframework</group Id> <artifactId>spring-context</artifactId> <version>${spring.version}</ve rsion> </dependency> <dependency> <groupid>org.springframework</groupi D> <artifactId>spring-core</artifactId> <version>${spring.version}</versio n> </dependency> <dependency> <groupid>org.springframework</groupid&gt
            ;
       <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> & Lt;/dependency> <dependency> <groupId>org.springframework</groupId> & Lt;artifactid>spring-web</artifactid> <version>${spring.version}</version> </d ependency> <dependency> <groupId>org.springframework</groupId> <ar Tifactid>spring-jdbc</artifactid> <version>${spring.version}</version> </depen dency> <dependency> <groupId>org.springframework</groupId> <ar Tifactid>spring-test</artifactid> <version>${spring.version}</version> </d Ependency> </dePendencies>

 
testing base Classes and JUnit tests
Package com.simple.test;

Import Org.junit.runner.RunWith;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * Test class base class, load Spring
 * @author fengyuxili
 *
/@RunWith (springjunit4classrunner.class)
@ Contextconfiguration ({
    "Classpath:spring/applicationcontext.xml",
    "classpath:spring/ Springmvc-config.xml "
}) public
abstract class Basetest {


}


package com.simple.test;

Import Org.junit.Test;
Import org.springframework.beans.factory.annotation.Autowired;

Import Com.simple.service.CommonService;

/**
 * Spring Test class
 * @author fengyuxili
 * */public
class Springtest extends basetest{

    @ autowired
    private Commonservice commonservice;

    @Test public
    void Testcommonservice () {
        commonservice.commoncheck ();
    }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.