在SpringBoot项目开发过程中,我们引用了大量的注解,这样导致我们在对其进行测试时需要首先对bean进行创建,那么简单的Test注解就无法实现了,这个时候加入其它注解协助实现bean的创建
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { @Autowired private Service service; @Test public void contextLoads() { service.test(); } }
* 在执行过程中可能会出现eurekaAutoServiceRegistration 创建bean异常,网上给出的错误原因是端口被占用,所以建议在进行单元测试的时候关掉对应服务 * 其实在报上述错误的时候我们已经拿到了想要的结果了,所以如果短时间内解决不掉这个错误,可以先不去处理