Python 官方文档:入门教程 => 点击学习
目录spring单元测试类ApplicationTests错误1)正确写法2)异常写法SpringTest单元测试错误经过查询资料总结出现此错误的原因可能有两种Spring单元测试类
package com.boot.demo02restful;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.boot.restful.Application;
import com.boot.restful.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=Application.class)
public class ApplicationTests {
@Autowired
@Qualifier(value="myUserService")
private UserService userSerivce;
@Before
public void setUp() {
// 准备,清空user表
userSerivce.deleteAllUsers();
}
@Test
public void test() throws Exception {
// 插入5个用户
userSerivce.create("a", 1);
userSerivce.create("b", 2);
userSerivce.create("c", 3);
userSerivce.create("d", 4);
userSerivce.create("e", 5);
// 查数据库,应该有5个用户
Assert.assertEquals(5, userSerivce.getAllUsers().intValue());
// 删除两个用户
userSerivce.deleteByName("a");
userSerivce.deleteByName("e");
// 查数据库,应该有5个用户
Assert.assertEquals(3, userSerivce.getAllUsers().intValue());
}
}
package com.boot.demo02restful;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.boot.restful.Application;
import com.boot.restful.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ApplicationTests {
@Autowired
@Qualifier(value="myUserService")
private UserService userSerivce;
@Before
public void setUp() {
// 准备,清空user表
userSerivce.deleteAllUsers();
}
@Test
public void test() throws Exception {
// 插入5个用户
userSerivce.create("a", 1);
userSerivce.create("b", 2);
userSerivce.create("c", 3);
userSerivce.create("d", 4);
userSerivce.create("e", 5);
// 查数据库,应该有5个用户
Assert.assertEquals(5, userSerivce.getAllUsers().intValue());
// 删除两个用户
userSerivce.deleteByName("a");
userSerivce.deleteByName("e");
// 查数据库,应该有5个用户
Assert.assertEquals(3, userSerivce.getAllUsers().intValue());
}
}
java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
进行SpringTest单元测试时遇到的错误
1、没有在测试方法上写@Test
2、@Test包导入出错,很有可能导入的是org.junit.jupiter.api.Test包,而使用Spring单元测试需要的包是org.junit.Test
可能由以上两种可能导致出错
要这样
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Spring单元测试类ApplicationTests错误的解决
本文链接: https://www.lsjlt.com/news/162809.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0