I love Java Series --- [Spring integration Junit: Spring-test]

purpose:

In the test class, each test method has the following two lines of code:

 

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

IAccountService accountService = ac.getBean("accountService",IAccountService.class);

 

These two lines of action is to obtain a container, if you do not write it, you will be prompted direct null pointer exception. So it can not be easily deleted.

Therefore, the use pom.xml file import-dependent spring-test, two words can be encapsulated into the frame in the spring, to facilitate the call after us.

step:

1. Import spring-context in pom.xml file.

  <-! The Spring Framework -> 
    < dependency > 
    < groupId > org.springframework </ groupId > 
    < artifactId > the Spring-context </ artifactId > 
    < Version > 5.0.2.RELEASE </ Version > attention to the version with spring- version of the same test
   </ dependency >

 

2. Import junit in pom.xml file.

 

    < Dependency > 
      < groupId > junit </ groupId > 
      < artifactId > junit </ artifactId > 
      < Version > 4.12 </ Version > <-! Here should be noted that, spring5 and above requirements must be version 4.12 and junit , or it does not take -> 
      < scope > the Test </ scope > 
    </ dependency >

 

 

 

 

3. Import the spring integrated Junit coordinates ---- spring-test in the pom.xml file.

 

< Dependency > 
 < groupId > org.springframework </ groupId > 
 < artifactId > the Spring-the Test </ artifactId > 
 < Version > 5.0.2.RELEASE </ Version > note the version to be consistent with the version of spring-context, inconsistent error. 
 </ Dependency >

 

4. Replace the existing annotations runner @RunWith

 

/ ** 
* test class 
* @author juvenile siege lion 
* @Version 1.0 
* / 
@RunWith (the SpringJUnit4ClassRunner. Class )
 public  class AccountServiceTest { 
}

 

5. @ContextConfiguration specify the location of the spring configuration file

 

/ ** 
* test class 
* @author juvenile siege lion 
* @Version 1.0 
* / 
@RunWith (the SpringJUnit4ClassRunner. Class ) 
@ContextConfiguration (locations = { "CLASSPATH: bean.xml" })
 public  class AccountServiceTest { 
} 
@ContextConfiguration Note: 
locations attribute: Specifies the location of the configuration file. If the path is a class, you need classpath: show 

classes properties: annotation specifies the class. Xml configuration when not in use, this property specifies the position of the needed class annotation format: @ContextConfig (classes = Config.class).

 

6. @Autowired injection test data to the class variable

 

/ ** 
* test class 
* @author juvenile siege lion 
* @Version 1.0 
* / 
@RunWith (the SpringJUnit4ClassRunner. Class ) 
@ContextConfiguration (locations = { "CLASSPATH: bean.xml" })
 public  class AccountServiceTest { 
@Autowired 
Private IAccountService AS ; 
}

 

Question: Why not test class assigned to the xml?

 

Before explaining this problem, first lift everyone's concerns, assigned to the XML can not use it? 
The answer is yes, no problem, you can use. 
So why not use xml to configure the way? 
The reason is this: 
First: When we configured a bean in xml, spring load the configuration file to create a container, it will create the object. 
Second: we simply test class in the test function, and it does not participate in the project program logic, it will not solve the need to ask 
questions, so created, it did not use. Then there is the container will result in waste of resources. 
Therefore, based on the above two points, we should not be put to the test configuration xml file.

 

Guess you like

Origin www.cnblogs.com/hujunwei/p/11115744.html