背景:原本的springboot改成了不需要maven依赖自动导入jar包的java项目,导入了JUnit包和spring-test和spring-boot-test
Junit测试配置类
@RunWIth(SpringJUnit4ClassRunner.class)
//Application.class是系统启动类,不需要启动,直接运行测试类就能以junit的方式启动项目
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class BaseTest(){
@Before
public void testBefore(){
System.out.println("------------->测试开始")
}
@After
public void testAfter(){
System.out.println("------------->测试结束")
}
}
public class test1 extends BaseTest{
//要测试的类
@Autowired
private AAAAAA a;
@Test
public void test(){
}
}
写成依赖是
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version> 4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version> 1.5.1.RELEASE</version>
</dependency>
报错:SpringJUnit4ClassRunner如果无法导入jar包,
则可能是你的spring-test的包中没有相关的方法,是因为版本的问题,可以换版本,我这里的版本没有问题
报错:java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
则是因为你导入的junit包内没有集成hamcrest-core核心包,
你可以专门再导入这个包,或者重新从网上下载一个全的junit包,
hamcrest-core的maven依赖为
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version> 1.3</version>
</dependency>
这样应该就可以了,下面给一个junit和hamcrest的关系的网址:https://blog.csdn.net/hanpompy/article/details/7622251
补:关于java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.makeAccessible
很有可能是依赖了多个不同版本的jar包,或者自己引用了一遍,idea又引用了一遍(自己引用的idea不认,非得自己引用),这个时候看看是不是自己引用出现空格、标点……的错误哦,或者就直接使用idea的引用。