Intellij IDEA使用junit单元测试及其junit与spring版本不兼容问题
来源:互联网 发布:跟矮个子女生啪啪知乎 编辑:程序博客网 时间:2024/05/21 07:57
Intellij IDEA自动创建单元测试,这在我之前的博客已有介绍 IntelliJ IDEA中用快捷键自动创建测试类
下面是我在创建springboot测试类中的说明和遇到的问题
创建好了测试类后
1.测试service层测试类需要加上注解:@Runwith,@SpringBootTest2.测试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)@SpringBootTestpublic 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
阅读全文
1 0
- Intellij IDEA使用junit单元测试及其junit与spring版本不兼容问题
- JUnit单元测试--IntelliJ IDEA
- JUnit单元测试--IntelliJ IDEA
- JUnit单元测试--IntelliJ IDEA
- IntelliJ IDEA 与junit
- Intellij idea使用Junit
- Junit 单元测试在 intelliJ IDEA 中的应用
- 初识IntelliJ IDEA下JUnit单元测试
- 初识IntelliJ IDEA下JUnit单元测试
- Junit单元测试与Spring整合
- Spring 使用Junit 进行单元测试
- Intellij IDEA junit 使用之org.junit不存在
- IntelliJ IDEA Junit
- IntelliJ Idea + Maven + Junit
- Intellij IDEA JUnit处理
- Intellij IDEA Junit 配置
- Spring Junit单元测试
- Spring + JUnit进行单元测试
- Net-snmp总结(一)-snmp的基本概念
- POJ
- 欢迎使用CSDN-markdown编辑器
- C++ 静态联编与动态联编
- 初窥Linux 之 我最常用的20条命令
- Intellij IDEA使用junit单元测试及其junit与spring版本不兼容问题
- MVP+Fresco+Retrofit实现的一个简单的适配器类
- markdown书写数学公式
- Windows C语言 Socket编程 client端(客户端)--断线重连版
- AdvancedInstaller打包工具使用(七)
- jdk下载
- TensorFlow 教程
- 大数据(三十)HBASE【Hbase 环境安装 -- 完全分布式安装】
- 批量插入数据 C# SqlBulkCopy使用