java - Mockito when...thenResult always returns null -


with above code error in line of test

when(request.getservletcontext().getattribute("sessionfactory"))     .thenreturn(factory); 

any ideas?

java class

protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {          sessionfactory sessionfactory = (sessionfactory) request.getservletcontext().getattribute("sessionfactory");         ............... } 

test class

 @test     public void testservlet() throws exception {         httpservletrequest request = mock(httpservletrequest.class);                httpservletresponse response = mock(httpservletresponse.class);               factory = contextinitialized();        when(request.getservletcontext().getattribute("sessionfactory")).thenreturn(factory); //always error here         when(request.getparameter("empid")).thenreturn("35");         printwriter writer = new printwriter("somefile.txt");         when(response.getwriter()).thenreturn(writer);         new deleteemployee().doget(request, response);          verify(request, atleast(1)).getparameter("username"); // if want verify username called...         writer.flush(); // may not have been flushed yet...         asserttrue(fileutils.readfiletostring(new file("somefile.txt"), "utf-8")                    .contains("my expected string"));     } 

when(request.getservletcontext().getattribute("sessionfactory")).thenreturn(factory); 

this bit:

request.getservletcontext().getattribute("sessionfactory") 

is chained call; you're trying stub both request, , servlet context request returns.

you can that, need use deep stubs:

httpservletrequest request = mock(httpservletrequest.class, returns_deep_stubs); 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -