POST上具有JSON主体的Junit测试控制器

时间:2019-09-17 12:09:01

标签: java spring-boot unit-testing junit mocking

我在为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";
}

1 个答案:

答案 0 :(得分:1)

when(linuxCommandService.runCommand(jsonValue))
.thenReturn(linuxResponse.toString());