问题:
当写一个测试类的时候如果使用了java中的@Autowired
来自动装配一个bean的话,只使用junit的@Test会报空指针。
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
解决方法:
加上注解
@RunWith(SpringRunner.class)
@SpringBootTest(classes = WmsApplication.class)//启动类名
样例:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = WmsApplication.class)
public class Test1 {
@Autowired
TestBiz testBiz;
@Test
public void testStr() {
testBiz.bizInit();
testBiz.testStr();
}
}