zoukankan      html  css  js  c++  java
  • 【Spring】Spring和JUnit5整合及注解

    Spring和JUnit5整合及注解

    • 原写法:

      需要手写代码,根据配置文件加载上下文,从而得到bean,调用方法。

      public class MyTest2 {
      
          @Test
          public void test() {
              ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
              TutorService tutorService = context.getBean("tutorServiceImpl", TutorService.class);
              Tutor tutor = tutorService.getTutorById("20170000");
              System.out.println(tutor);
          }
      
      }
      

    可以使用注解简化

    • 加入依赖:spring-test的版本和spring要一致。

      		<dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>4.12</version>
                  <scope>test</scope>
              </dependency>
              <dependency>
                  <groupId>org.junit.jupiter</groupId>
                  <artifactId>junit-jupiter</artifactId>
                  <version>RELEASE</version>
                  <scope>test</scope>
              </dependency>
              <!--spring-test-->
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-test</artifactId>
                  <version>5.2.12.RELEASE</version>
                  <scope>test</scope>
              </dependency>
      
    • 使用注解的测试类

      JUnit5使用@ExtendWith(SpringExtension.class)

      *@ExtendWith和@ContextConfiguration可以合并,写复合注解@SpringJUnitConfig(locations="classpath:applicationContext.xml")

      import org.junit.jupiter.api.Test;//保证这个包导入
      
      @ExtendWith(SpringExtension.class)
      @ContextConfiguration("classpath:applicationContext.xml")
      public class MyTest {
      
          @Autowired
          TutorService tutorService;
      
          @Test
          public void test2() {
              Tutor tutor = tutorService.getTutorById("20170000");
              System.out.println("tutor=" + tutor);
          }
      }
      

      *JUnit4使用@Runwith(SpringJUnit4ClassRunner.class)

  • 相关阅读:
    ajax _02【XML响应,post请求】
    ajax_01【httpRequest.responseText】
    方法的定义【js函数】
    Promise基本用法
    promise
    筛选(1)
    ng-cli 中HTTP请求思路(1) (接口请求处理)
    PHP占用CPU过高几种思路
    关于tcpdump的那点事~
    虚拟机固定IP那点事
  • 原文地址:https://www.cnblogs.com/musecho/p/14455266.html
Copyright © 2011-2022 走看看