添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
代码:
@RunWith(SpringRunner.class)
// Application.class 改为你springboot的启动类
@SpringBootTest(classes=Application.class)
@AutoConfigureMockMvc
public class ElasticSearchServiceTest {
@Autowired
MockMvc mockMvc;
@Autowired
private ElasticSearchServiceImpl elasticSearchService;
/**
* 测试Service
*/
@Test
public void testQuery() {
List<User> list = elasticSearchService.queryAll();
Assert.assertNotNull(list);
}
/**
* 测试Controller
*/
@Test
public void testQueryUsers() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/users"))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
}
}