Dhivya
Dhivya

Reputation: 152

JUnit Mockito Request and Response

I am trying to create a test for a login page using JUnit where I need to mock out dev controller.

As I used a HttpServlet in my dev environment, for testing environment it is asking for httprequest...

I went for Mock request where my file is not getting where I'm using a controller not a servlet.

Can anyone help me on this?

Below is my JUnit Controller

package com.atoc.test.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
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.setup.MockMvcBuilders;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;

import com.atoc.test.DAO.UserTestDAO;
import com.atoc.test.service.UserTestService;
import com.bpa.qaproduct.controller.UserController;
import com.bpa.qaproduct.entity.User;
import com.bpa.qaproduct.service.UserService;

import org.apache.commons.io.FileUtils;



@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:testApplicationContext.xml" })
public class UserControllerTest{


    @Mock
    private  UserService  userService;

    @Mock
    private HttpServletRequest request;

    @Mock
    private HttpServletResponse response;

    @InjectMocks
    private UserController userController;

    private MockMvc mockMvc;

    @Before
    public void setup() {
         MockitoAnnotations.initMocks(this);

         mockMvc = MockMvcBuilders.standaloneSetup(userController).build();

    }

    @Test
    public void testviewUserrList() throws Exception {
         User user =  new User();

        //int value = null;
        when(userService.getUserFilterCount(user)).thenReturn(20);
        //mockMvc.perform(get("/todo/"));
         mockMvc.perform(get("/user/viewUserList")).andExpect(status().isOk());
    }

    @Test
    public void testloginVerification() throws Exception
    {
        User user = new User();

        HttpServletRequest request =  Mockito.mock(HttpServletRequest.class);     
        HttpServletResponse response = Mockito.mock(HttpServletResponse.class); 


        when(request.getParameter("userName")).thenReturn("me");
        when(request.getParameter("password")).thenReturn("secret");

        mockMvc.perform(post("/user/loginVerification"))
         .andExpect(status().isOk());
        System.out.println("***********"+request.getParameter("userName"));
    }
}

The test case does not pass and httprequest is not passing values.

So I'm getting a NullPointerException in my running environment

I mocked out the request and response methods but I'm still getting the same error.

Passing value is not null

I'm giving some values there setting time might be the problem I didn't extend any Mockito in main class where I'm using inner methods

Upvotes: 1

Views: 9843

Answers (1)

Stefan Birkner
Stefan Birkner

Reputation: 24510

You have to provide request parameters to mockmvc instead of a request object.

@Test
public void testloginVerification() throws Exception {
    User user = new User();

    mockMvc.perform(post("/user/loginVerification")
        .param("userName", "me")
        .param("password", "secret"))
      .andExpect(status().isOk());
}

Also the User object is not interacting with the other code of your test. I think you're doing something completely wrong. Can you add the code of the UserController?

Upvotes: 1

Related Questions