错误截图:

Failed to load ApplicationContext

错误分析:测试类启动不了;这里使用SpringBoot的测试组件,需要添加如下两个注解:

@RunWith(value = SpringJUnit4ClassRunner.class)

让测试运行在spring的环境,这样我们测试的时候就可以和开发的时候一样编写代码,例如使用@Autowired注解直接注入

@SpringBootTest(classes = Application.class)

执行当前的这个类是测试类

解决问题的方法:

由于使用IDEA来构建Spring Boot 项目,因此上面的两个测试类启动项就不需要了(使用的话就会出现上图错误),直接继承生成的测试类即可。

Failed to load ApplicationContext

                                                                             自动生成测试代码

继承

public class ApiServiceTest extends ItcastApplicationTests{

    @Autowired
    private ApiService apiService;

    @Test
    public void testGetHtml() throws Exception {
        String html = this.apiService.getHtml("https://www.autohome.com.cn/bestauto/1");
        System.out.println(html);
    }

    @Test
    public void testGetImage() throws Exception {
        this.apiService.getImage(
                "https://car2.autoimg.cn/cardfs/product/g24/M09/AE/EB/800x0_1_q87_autohomecar__wKgHIVpxGh6AFSN1AAY8kcz3Aww921.jpg");
    }

 

 

 

 

相关文章: