欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Spring单元测试类ApplicationTests错误的解决

时间:2022-05-02 10:40:17|栏目:JAVA代码|点击:

Spring单元测试类ApplicationTests错误

1)正确写法

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());
	}	
}

2)异常写法

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());
	}	
}

SpringTest单元测试错误

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

可能由以上两种可能导致出错

要这样

上一篇:java 中clone()的使用方法

栏    目:JAVA代码

下一篇:Java基础字符编码与内存流详细解读

本文标题:Spring单元测试类ApplicationTests错误的解决

本文地址:http://www.codeinn.net/misctech/200776.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有