Integrate junit4

  • Maven rely on
    <! --> <dependency> <groupId>org.springframework</groupId> <artifactId> Spring-context </artifactId> <version>5.22..RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.22..RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.22..RELEASE</version> <scope>test</scope> </dependency> <dependency> <! -- junit4 --> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version>
         <scope>test</scope>
     </dependency>
    Copy the code
  • Enable scanning for configuration files
    
            
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
        <context:component-scan base-package="com.du.spring"/>
    
    </beans>
    Copy the code
  • call
    @RunWith(SpringJUnit4ClassRunner.class) // Unit test framework
    @ContextConfiguration("classpath:bean.xml") // Load the configuration file
    public class UserTest {
      @Autowired
      private User user;
    
      @Test
      public void test(a) {
        Integer id = user.getId();
        System.out.println(id);
        user.setId(null); System.out.println(user); }}Copy the code

Integrate junit5

  • Maven rely on
    <! --> <dependency> <groupId>org.springframework</groupId> <artifactId> Spring-context </artifactId> <version>5.22..RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.22..RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.22..RELEASE</version> <scope>test</scope> </dependency> <! -- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --><! -- junit5 --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.7. 0</version>
        <scope>test</scope>
    </dependency>
    Copy the code
  • call
    //@ExtendWith(SpringExtension.class)
    // @contextConfiguration ("classpath:bean.xml") // Load the configuration file
    @SpringJUnitConfig(locations = "classpath:bean.xml")  // The bottom sentence is the top two sentences
    public class UserTest {
      @Autowired
      private User user;
    
      @Test
      public void test(a) {
        Integer id = user.getId();
        System.out.println(id);
        user.setId(null); System.out.println(user); }}Copy the code

Spring MVC integration with Junit5

Reference documentation

  • SpringMVC simulator, which simulates page requests, rightControllertest
    @SpringJUnitWebConfig(locations = {"classpath:springmvc-servlet.xml", "classpath:applicationContext.xml"})
    public class EmployeeControllerTest {
      MockMvc mockMvc;
      // Just copy and paste it
      @BeforeEach
      void setup(WebApplicationContext wac) {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
      }
    
      @Test
      public void getEmpsTest(a) throws Exception {
        // Get a mock request builder
        MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/getEmps");
        // The simulator works to get a result
        MvcResult result = mockMvc.perform(requestBuilder).andReturn();
        // Get the request from the result
        MockHttpServletRequest request = result.getRequest();
        PageInfo pageInfo = (PageInfo) request.getAttribute("pageInfo");
        System.out.println("Current page number" + pageInfo.getPageNum());
        for (int navigatepageNum : pageInfo.getNavigatepageNums()) {
          System.out.print(navigatepageNum + ""); }}}Copy the code