使用@SpringBootTest注解进行单元测试

1、pom.xml文件中引入test包依赖,如下:

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

2、编写测试类 DemoApplicationTests.java,加入@SpringBootTest注解

@RunWith(SpringRunner.class)
@SpringBootTest(classes={DemoApplication.class})   //指定启动类
class DemoApplicationTests {
    @Autowired
    private ListService service;

    @Test
    void contextLoads() {
        //TODO 此处调用接口方法
        service.listTest();
        System.out.println("contextLoads");
    }

    @Test
    public void testTwo(){
        service.listTest();
        System.out.println("test hello 2");
    }

    @Before
    public void testBefore(){
        System.out.println("before");
    }

    @After
    public void testAfter(){
        System.out.println("after");
    }


}

结果

使用@SpringBootTest注解进行单元测试_第1张图片

 

你可能感兴趣的:(springBoot,Springboot,test)