11

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

peterh
  • 11,875
  • 18
  • 85
  • 108
Tiago Costa
  • 181
  • 1
  • 2
  • 4
  • 1
    Well the error is correct. Not sure if Eclipse quick-fix is smart enough but you should add a couple of static imports for the class `MockMvcRequestBuilders` which contains those methods. – M. Deinum Sep 29 '14 at 06:14

2 Answers2

52

Try adding the following imports:

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
OlgaMaciaszek
  • 3,662
  • 1
  • 28
  • 32
  • @Tiago Costa, if this answer resolve your question, would you accept it? By the way, I think it does. – Stephen Isienyi Mar 15 '16 at 15:50
  • This solved it for me even after 8 years. However, it wouldn't have been necessary if Spring and SpringBoot documentation showed import statements on their sample code. – tlarson Jul 21 '23 at 20:44
6

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;
Rishi
  • 1,646
  • 2
  • 15
  • 34