controller测试用mockmvc但是没必要,用postman测试就够了
service测试用以下的例子
package com.service; import com.nxx.DemoApplication; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = {DemoApplication.class}) public class MyServiceTest { @Autowired private MyService myService; @Test public void helloService() { System.out.println("xjxjx"); Assert.assertEquals(myService.helloService(), "hello"); } }
需要注意的点:
1、idea中Gradle的runTests配置成IDEA,否则出现No tests found for given includes:错误
2.@RunWith和@SpringbootTest后面的参数要一致
3.用@Autowire不用构造函数方式,否则报错