Use of test units in SSM framework spring integration JUnit process details
Problems and solutions in test class
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);
The purpose of these two lines of code is to obtain the container. If it is not written, a null pointer exception will be prompted directly. So it can't be deleted easily.
Analysis of Solutions
To solve the above problems, what we need is that the program can automatically help us create containers. Once the program can automatically create a spring container for us, we don't need to create it manually, and the problem is solved.
We all know the principle of JUnit unit testing (described in the web phase course), but obviously JUnit can't be implemented because it can't know whether we use the spring framework, let alone help us create the spring container. Fortunately, JUnit exposed us an annotation that allows us to replace its runner.
At this time, we need to rely on the spring framework because it provides a runner that can read the configuration file (or annotation) to create the container. We just need to tell it where the configuration file is.
Configuration steps
Step 1: copy and integrate the necessary jar package of JUnit into the Lib directory
It should be noted here that when importing a jar package, you need to import a jar package of AOP in spring.
Step 2: replace the original runner with @ runwith annotation
/** * 测试类 * @Version 1.0 */ @RunWith(SpringJUnit4ClassRunner.class) public class AccountServiceTest { }
Step 3: use @ contextconfiguration to specify the location of the spring configuration file
/** */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations= {"classpath:bean.xml"}) public class AccountServiceTest { }
@Contextconfiguration annotation:
Locations property: used to specify the location of the configuration file. If it is under the class path, you need to use classpath: to indicate the classes attribute: the class used to specify the annotation. When XML configuration is not used, the location of the annotation class needs to be specified with this attribute.
Step 4: use @ Autowired to inject data into the variables in the test class
/** */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations= {"classpath:bean.xml"}) public class AccountServiceTest { @Autowired private IAccountService as ; }
Why not put the test class into XML
Before explaining this problem, let's dispel everyone's doubts. Can it be used in XML? The answer is yes, no problem, you can use it.
So why not configure it into XML? The reason is as follows:
First: when we configure a bean in XML and spring loads the configuration file to create a container, the object will be created.
Second: the test class is only used when we test functions. In the project, it does not participate in program logic and will not solve requirements problems. Therefore, it is created and not used. If it is stored in the container, it will cause a waste of resources.
Therefore, based on the above two points, we should not configure the test into an XML file.
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.