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();
	}
}
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值