Spring junit整合

282 阅读1分钟

一、整合junit4

  • Maven依赖
    <!--   spring框架     -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.2.2.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
         <!--     junit4       -->
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.13</version>
         <scope>test</scope>
     </dependency>
    
  • 配置文件,开启扫描
    <?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: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>
    
  • 调用
    @RunWith(SpringJUnit4ClassRunner.class) //单元测试框架
    @ContextConfiguration("classpath:bean.xml") // 加载配置文件
    public class UserTest {
      @Autowired
      private User user;
    
      @Test
      public void test() {
        Integer id = user.getId();
        System.out.println(id);
        user.setId(null);
        System.out.println(user);
      }
    }
    

二、整合junit5

  • Maven依赖
    <!--   spring框架     -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.2.2.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>
    
  • 调用
    //@ExtendWith(SpringExtension.class)
    //@ContextConfiguration("classpath:bean.xml") // 加载配置文件
    @SpringJUnitConfig(locations = "classpath:bean.xml")  // 下面一句顶上面两句
    public class UserTest {
      @Autowired
      private User user;
    
      @Test
      public void test() {
        Integer id = user.getId();
        System.out.println(id);
        user.setId(null);
        System.out.println(user);
      }
    }
    

三、Spring MVC 整合 Junit5

参考文档

  • SpringMVC 模拟器,可以模拟页面请求,对Controller进行测试
    @SpringJUnitWebConfig(locations = {"classpath:springmvc-servlet.xml", "classpath:applicationContext.xml"})
    public class EmployeeControllerTest {
      MockMvc mockMvc;
      // 固定写法,复制粘贴就好
      @BeforeEach
      void setup(WebApplicationContext wac) {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
      }
    
      @Test
      public void getEmpsTest() throws Exception {
        // 获得一个模拟的请求建造器
        MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/getEmps");
        // 模拟器工作,获得一个结果
        MvcResult result = mockMvc.perform(requestBuilder).andReturn();
        // 从结果中获取请求出来
        MockHttpServletRequest request = result.getRequest();
        PageInfo pageInfo = (PageInfo) request.getAttribute("pageInfo");
        System.out.println("当前页码" + pageInfo.getPageNum());
        for (int navigatepageNum : pageInfo.getNavigatepageNums()) {
          System.out.print(navigatepageNum + " ");
        }
      }
    }