java - JUnit Mockito Request and Response -
i trying create test login page using junit need mock out dev controller.
as used httpservlet in dev environment, testing environment asking httprequest...
i went mock request file not getting i'm using controller not servlet.
can me on this?
below 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 not pass , httprequest not passing values.
so i'm getting nullpointerexception in running environment
i mocked out request , response methods i'm still getting same error.
passing value not null
i'm giving values there setting time might problem didn't extend mockito in main class i'm using inner methods
you have provide request parameters mockmvc instead of 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 user
object not interacting other code of test. think you're doing wrong. can add code of usercontroller?
Comments
Post a Comment