Intellij IDEA使用junit单元测试及其junit与spring版本不兼容问题

Intellij IDEA自动创建单元测试,这在我之前的博客已有介绍  IntelliJ IDEA中用快捷键自动创建测试类


下面是我在创建springboot测试类中的说明和遇到的问题

创建好了测试类后

1.测试service层测试类需要加上注解:@Runwith,@SpringBootTest
2.测试Controller层测试类需要加上注解:@Runwith,@SpringBootTest,@AutoConfigureMoceMvc

例如我的service测试:

import com.oldbig.domain.Girl;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class GirlServiceTest {

    @Autowired
    private GirlService girlService;

    @Test
    public void getAge() throws Exception {
        Girl girl = girlService.getAge(5);
        Assert.assertEquals(new Integer(15),girl.getAge());
    }

}

如果测试成功的话并且数据对应的话则无异常显示,但是我在运行时出现:

java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher.错误

表示我的junit版本太低了需要4.12以上,我换了4.12后,发现项目都运行不了(本来用4.10项目可以运行只是测试错误):显示程序包org.junit不存在 错误


这时候我换了更高版本4.4,4.5之类的都不行,百度了以下,发现了  4.12-beta-3(烈火汉化版),添加入依赖

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12-beta-3</version>
</dependency>
发现项目能运行,测试也能顺利进行,终于解决了bug

转载于:https://my.oschina.net/u/1987703/blog/1617536

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值