SSM in the test unit using a frame, spring integration Junit

Test class problems and Solutions

 

3.1.1      problem

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

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); IAccountService as = 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.

3.1.2      Solutions analysis

In response to these problems, we need a program that can help us create the container automatically. Once the program can automatically create a spring container for us, we do not need to manually create the problem will be solved.

We all know that the principle junit unit test (spoken in the web stage course), but apparently, junit can not be achieved, because of its own can not know whether we use spring framework, not to mention the spring to help us create a container . Fortunately, however, junit give us exposure to a comment, you can let us replace its runner.

At this point, we need to rely on spring framework, as it provides a run, you can read the configuration file (or notes) to create a container. We just need to tell it where the configuration file on the line.

 

Configuration Steps

3.2.1      The first step: copy integration junit essential jar package to the lib directory

It should be noted here that, when introduced into the jar package, needs to import a spring aop in a jar.

3.2.2      Step 2: Use @RunWith notes to replace the original runner

/**

* Test class

@Version 1.0

*/ @RunWith(SpringJUnit4ClassRunner.class) public class AccountServiceTest {

 

}

3.2.3      Use: The third step @ContextConfiguration specified spring location profile

/**

*/ @RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations= {"classpath:bean.xml"})

public class AccountServiceTest {

}

@ContextConfiguration notes:

locations properties: the location of the specified configuration file. If the path is a class, you need classpath: show classes properties: specifies the type of annotation. Xml configuration when not in use, this property specifies the position of the need annotation.

 

 

3.2.4      Fourth step: using @Autowired to the test data class variable injection

/**

*/ @RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations= {"classpath:bean.xml"})

public class AccountServiceTest {

 

@Autowired

private IAccountService as ;

}

 

 

Why not test class assigned to xml in

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 problem of demand, 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/blogofbin/p/11594148.html