我需要创建方法get()和status()来创建一个带有mockmvc的测试控制器?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我需要创建方法get()和status()来创建一个带有mockmvc的测试控制器?相关的知识,希望对你有一定的参考价值。
嘿伙计们我正在尝试测试我的第一个控制器,在互联网上有几个例子,但在方法get()和status()编译错误。
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import br.com.boot.application.Application;
import br.com.boot.controller.ClienteController;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class)
@WebAppConfiguration
public class ClienteControllerTest {
@Autowired
private ClienteController controller;
@Mock
private MockMvc mock;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.mock = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void testandoClienteController() throws Exception{
this.mock.perform(get("/novo").andExpect(status().isOk()));
}
}
我的班长
@RestController
@RequestMapping("/cliente")
public class ClienteController {
@Autowired
private ClienteAplicacaoService service;
@RequestMapping(value = "/novo", method = RequestMethod.GET)
@ResponseBody
public ClienteData novo(@RequestBody NovoClienteComando comando){
String clienteId = service.novoCliente(comando);
return service.obterCliente(clienteId);
}
@RequestMapping("/obter")
@ResponseBody
public ClienteData obter(@RequestParam("clienteId") String clienteId){
return service.obterCliente(clienteId);
}
}
Error:
此行的多个标记 - 方法get(String)未定义类型ClienteControllerTest - 方法status()未定义类型ClienteControllerTest
答案
尝试添加以下导入:
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
以上是关于我需要创建方法get()和status()来创建一个带有mockmvc的测试控制器?的主要内容,如果未能解决你的问题,请参考以下文章