SwimJim
SwimJim

Reputation: 109

Junit test controller with JSON body on POST

I am having issues creating a unit test for my LinuxCommandController. The LinuxCommandController returns a string from the responsebody of it's post mapping. It invokes LinuxCommandService which runs a command using the JSON string from the post body.

I've tried numerous ways to get this to work, my latest error is below. I've tried having a string returned as well, and get all types of errors. I feel like this shouldn't be this difficult, and I'm missing something simple.

Any help would be appreciated.

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

Here is my junit test class.

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());
        }

}

Here is the controller.

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);
}

}

And the service method returns a string,

public String runCommand(String requestCommand) {
.....
return "string returned";
}

Upvotes: 0

Views: 2339

Answers (1)

Jude Niroshan
Jude Niroshan

Reputation: 4460

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

Upvotes: 1

Related Questions