【发布时间】:2020-01-18 07:09:04
【问题描述】:
我在为我的 LinuxCommandController 创建单元测试时遇到问题。 LinuxCommandController 从它的后映射的响应体中返回一个字符串。它调用 LinuxCommandService,后者使用帖子正文中的 JSON 字符串运行命令。
我已经尝试了很多方法来让它工作,我的最新错误如下。我也尝试过返回一个字符串,并得到所有类型的错误。我觉得这不应该这么难,而且我错过了一些简单的东西。
任何帮助将不胜感激。
caservice: Compilation failure
[ERROR] /api/LinuxControllerTest.java:[59,55] cannot find symbol
[ERROR] symbol: method thenReturn(org.springframework.http.ResponseEntity)
[ERROR] location: class java.lang.String
这是我的 junit 测试类。
package com.development.api;
import com.development.services.LinuxCommandService;
//import jdk.internal.net.http.ResponseBodyHandlers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
//import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.http.ResponseEntity;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.http.MediaType;
import static org.mockito.Mockito.when;
@RunWith(SpringRunner.class)
@WebMvcTest(LinuxController.class)
@WithMockUser
@AutoConfigureMockMvc
public class LinuxControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private LinuxCommandService linuxCommandService;
@InjectMocks
private LinuxController linuxController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders
.standaloneSetup(linuxController)
.build();
}
@Test
public void test_that_the_jenkins_controller_received_all_required_params_and_our_service_returned_a_201_created() throws Exception {
String jsonValue = "{\"command\":\"CMD12}";
ResponseEntity linuxResponse = ResponseEntity.status(HttpStatus.CREATED)
.body(HttpStatus.CREATED);
when(linuxCommandService.runCommand(jsonValue).thenReturn(linuxResponse));
mockMvc.perform( MockMvcRequestBuilders
.post("/api/linux/command")
.content(jsonValue)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isCreated());
}
}
这是控制器。
import com.services.LinuxCommandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
@RestController
@NoArgsConstructor
@RequestMapping("/api/linux")
public class LinuxController {
@Autowired
LinuxCommandService linuxCommandService;
@PostMapping(path = "/command", consumes = "application/json", produces = "application/json")
@ResponseStatus(code = HttpStatus.CREATED)
public String create(@RequestBody @NotBlank String requestCommand) {
return linuxCommandService.runCommand(requestCommand);
}
}
并且服务方法返回一个字符串,
public String runCommand(String requestCommand) {
.....
return "string returned";
}
【问题讨论】:
-
您的 IDE 会告诉您出了什么问题...提示,您的代码中缺少
)... -
如果我缺少括号,我不会收到语法错误吗?我检查了这个,看起来他们正确关闭了?
-
这正是你在编译时得到的。
标签: java spring-boot unit-testing junit mocking