Springboot Junit 加载依赖,正常使用注解

在下图的目录下,写测试用例,发现注解失效,譬如 @Autowired等在这里插入图片描述
解决方法:
1、在pom.xml新增依赖:

<!--添加junit环境的jar包-->
<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
</dependency>

2、在测试类新增注解:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {Application.class})  // **Application.classs是我项目的启动类**
public class test{
    @Autowired(required = false) 
    ImageService imageService;

    @Test
    public void test1() { 
    	imageService.importImage("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1853346654,89435280&fm=26&gp=0.jpgx");
    }

}

版权声明:本文为RoseOfIce原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。