gpt4 book ai didi

controller - 我需要创建方法 get() 和 status() 来创建带有 mockmvc 的测试 Controller 吗?

转载 作者:行者123 更新时间:2023-12-04 09:01:00 24 4
gpt4 key购买 nike

我正在尝试测试我的第一个 Controller ,在互联网上遵循了一些示例,但在方法中出错 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()));
}
}
我的类(class) Controller
@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) 未定义
客户端 Controller 测试
- 未定义该类型的方法 status()
客户端 Controller 测试

最佳答案

尝试添加以下导入:

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

关于controller - 我需要创建方法 get() 和 status() 来创建带有 mockmvc 的测试 Controller 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26077729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com