Unit test case template
@RunWith(SpringRunner.class) //@SpringBootTest(classes = PersonConfig.class) @ContextConfiguration(classes = PersonConfig.class) public class Test { @Resource private PersonEventService personEventService; @Test public void test() { // ApplicationContext context = new AnnotationConfigApplicationContext(PersonEventService.class); // PersonEventService personEventService = context.getBean(PersonEventService.class); System.out.println(personEventService); personEventService.registerUser("userName222"); } }
@RunWith annotation
Explanation:
Under normal circumstances, the test class needs @ RUNWITH to tell java what running environment this class runs in, such as starting and creating the application context of spring. Otherwise, you need to write a bunch of environment configuration code at startup. You can still run without @ RUNWITH in the IDEA because it is recognized as a JUNIT running environment in the IDEA, which is equivalent to a self recognized runwidth environment configuration. But not in other ides, so try to add this annotation.
--------
Copyright notice: This is the original article of CSDN blogger "yinglala", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint.
Original link: https://blog.csdn.net/qq_21108099/article/details/111496005
@ContextConfiguration annotation
Explanation:
There are two ways to use it:
1.@ContextConfiguration(locations={"classpath*:/spring1.xml","classpath*:/spring2.xml"})
Specify xml configuration file
2.@ContextConfiguration(classes = PersonConfig.class)
Specify java configuration class
XML mode:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd > <!-- Automatically scan the package --> <context:component-scan base-package="com" /> </beans>
This XML file automatically scans all bean s under the com package through the < context: component scan base package = "com" / > tag.
@The locations = {"classpath*:/*.xml"} in the brackets of ContextConfiguration means that all. XML files in the class path are included. Then the newly created XML file will be included, and the automatically scanned beans in it can be obtained. At this time, you can use the @ Autowired annotation in the test class to obtain all beans under the previously automatically scanned package
The difference between classpath and classpath *:
-
classpath: it will only find files in your class path.
-
classpath *: contains not only the class path, but also the jar file (class path) for searching.
JAVA configuration class:
/** * Configuration file java */ @Configuration @ComponentScan("com.xxx.xxx.controller.eventtest") public class PersonEventConfig { }
If we use Java, it will be very simple. We don't need to write XML files. We can create a Java class to replace XML files. Just add @ Configuration annotation on this class and @ ComponentScan annotation to start automatic scanning. If the annotation doesn't write anything in parentheses, @ComponentScan scans the same packages as the Configuration class by default.
Compared with XML, is it cool? This is also the way officially advocated.