Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit Test cases for Spring MVC 4 controller to check @PathVariable parameters in @RequestMapping

I have following controller code for which I have to write JUnit test case.

public class EquipmentController {

    private Map<String, Equipment> equiList = new HashMap <String,Equipment>();

    @RequestMapping("/rest/equipment/{Number}")
    public Equipment getEquipment(@PathVariable String Number){

        if(!equiList.containsKey(Number)){
            lNumber = DEFAULT;
        }
        return equiList.get(Number);

    }
}

I'm writing the JUnit test case for the same as below:

import static org.springframework.test.web.ModelAndViewAssert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({/* include live config here
    e.g. "file:web/WEB-INF/application-context.xml",
    "file:web/WEB-INF/dispatcher-servlet.xml" */})
public class EquipmentControllerTest {

    @Inject
    private ApplicationContext applicationContext;

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private HandlerAdapter handlerAdapter;
    private EquipmentController controller;

    @Before
    public void setUp() {
       request = new MockHttpServletRequest();
       response = new MockHttpServletResponse();
       handlerAdapter = applicationContext.getBean(HandlerAdapter.class);
       // Get the controller from the context here
       controller = new EquipmentController();
    }

    @Test
    public void testgetEquipment() throws Exception {
       request.getUriString()("lNumber");
       final Equipment equip = handlerAdapter.handle(request, response, 
           controller);
       assertViewName(equip, "view");
    }
}

But am not sure if this test class is correct or not as I am new to JUnit.

Can anyone please suggest how to do this.

like image 666
Nishant Kumar Avatar asked Aug 11 '15 09:08

Nishant Kumar


People also ask

What is the purpose of @PathVariable annotation in Spring MVC?

The @PathVariable annotation is used to extract the value from the URI. It is most suitable for the RESTful web service where the URL contains some value. Spring MVC allows us to use multiple @PathVariable annotations in the same method. A path variable is a critical part of creating rest resources.

What is the use of @PathVariable annotation in Spring boot?

Simply put, the @PathVariable annotation can be used to handle template variables in the request URI mapping, and set them as method parameters.

What is the use of @PathVariable annotation?

The @PathVariable annotation is used to extract the value of the template variables and assign their value to a method variable. A Spring controller method to process above example is shown below; @RequestMapping("/users/{userid}", method=RequestMethod.


1 Answers

Create a mock of your controller and use MockMvc to test your methods:

import static org.springframework.test.web.ModelAndViewAssert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({/* include live config here
    e.g. "file:web/WEB-INF/application-context.xml",
    "file:web/WEB-INF/dispatcher-servlet.xml" */})
public class EquipmentControllerTest {

    private MockMvc mockMvc;

    private EquipmentController controller;

    @Before
    public void setUp() {

       this.mockMvc = MockMvcBuilders.standaloneSetup(equipmentController).build()
    }

    @Test
    public void testgetEquipment() throws Exception {
      this.mockMvc.perform(get("/rest/equipment/{Number}", 3))
           .andExpect(status().isOk())
    }
}

where "3" represents value of your path variable.

like image 68
Branislav Lazic Avatar answered Oct 10 '22 00:10

Branislav Lazic