Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to create methods get () and status () to create a test controller with mockmvc?

I am trying to test my first controller, followed a few examples on the internet, but is in error in methods get() and status() to compile.

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

My 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:

Multiple markers at this line - The method get(String) is undefined for the type ClienteControllerTest - The method status() is undefined for the type ClienteControllerTest

like image 270
Tiago Costa Avatar asked Sep 27 '14 18:09

Tiago Costa


People also ask

What is MockMvc in JUnit?

MockMvc provides support for Spring MVC testing. It encapsulates all web application beans and makes them available for testing. Let's see how to use it: private MockMvc mockMvc; @BeforeEach public void setup() throws Exception { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build(); }

How do I make a MockMvc instance?

There are two ways to create a MockMvc instance: using Spring Boot's auto-configuration or hand-crafting it. Following Spring Boot's auto-configuration principle, we only need to annotate our test with @WebMvcTest .

How do you write a unit test for a controller?

Writing a Unit Test for REST Controller First, we need to create Abstract class file used to create web application context by using MockMvc and define the mapToJson() and mapFromJson() methods to convert the Java object into JSON string and convert the JSON string into Java object.


2 Answers

Try adding the following imports:

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
like image 136
OlgaMaciaszek Avatar answered Oct 14 '22 09:10

OlgaMaciaszek


If you just need get and status, add the following

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
like image 22
Rishi Avatar answered Oct 14 '22 10:10

Rishi