栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring使用junit进行测试时报错,出现空指针

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring使用junit进行测试时报错,出现空指针

控制台抛出的错误

十一月 04, 2021 12:02:58 下午 org.junit.vintage.engine.discovery.TestClassRequestResolver determineRunnerTestDescriptor
警告: Runner org.junit.internal.runners.ErrorReportingRunner (used on com.briup.test.AOPXMLTest) does not support filtering and will therefore be run completely.
null

这个错误在junit显示的是NullPointerException

//其实,这个错误的主要原因就是junit的版本不匹配
Junit4在类的开头使用的是“@RunWith”,Junit5中类的前面使用的“@ExtendWith”,所以说两个版本使用的注解是不一样的。
Junit4的“@Test”需要导入的是org.junit.Test,而Junit5的“@Test”这个注解,需要导入的是org.junit.jupiter.api.Test。
所以说,如果使用Junit4的话,就把导入改为“import org.junit.Test”。
如果使用Junit5的话,就把“@RunWith”改成“@ExtendWith”
还需要注意:使用“@RunWith”和“@ExtendWith”这两个注解时里面的参数是不同的(切记)
这样就可以解决了。

//这是我测试类里面的代码
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import com.briup.service.ITeacherService;


@RunWith(SpringRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AOPXMLTest {
	@Autowired
	private ITeacherService teacherService;

	@Test
	public void test() {
		System.out.println(teacherService);
		System.out.println(teacherService.getClass());
	}
	
	@Test
	public void test_saveOrUpdate() {
		teacherService.saveOrUpdate();
	}
	
	@Test
	public void test_delete() {
		teacherService.delete();
	}
	
	@Test
	public void test_deleteBatch() {
		teacherService.deleteBatch();
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/440011.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号