idea中springboot使用junit测试报错的问题

  参考文章:http://www.bubuko.com/infodetail-3595596.html

  原因:

         官方建议如果跑Junit5建议使用IDEA 2017.3之后的版本.因为较之前的版本还不支持Junit5; 
 
 
    解决方案:
        1】换高版本的idea
        2】换junit4
 
    springboot工程如何使用junit4:
    1)修改pom.xml
      使用springboot启动器创建的工程的pom中默认会带测试启动器spring-boot-starter-test;
      spring-boot-starter-test默认依赖Junit5(JUnit Platform + JUnit Jupiter + JUnit Vintage);
      需要排除Junit5相关的依赖包:排除junit-jupiter-api,然后再加入Junit4的依赖
    
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
   <exclusions>
      <exclusion>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-api</artifactId>
      </exclusion>
   </exclusions>
</dependency>

    2)创建测试类(Application为spring工程的启动类)

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class Test {
    @Autowired
    private StringRedisTemplate strTemp;
 
    @Test
    public void hello(){
        System.out.println("hello");
    }
}

  

    

posted on 2021-08-27 16:51  zgz2016  阅读(27)  评论(0编辑  收藏  举报

导航