unit testing - JUnit Mockito mock one method in another class Error -
i use netbeans 8.1 , junit , mockito write unit test project. here pieces of code
to tested function:
public map<string, string> getallusers() { if (allusers == null) { if (session.checkacl2("donatebookprivilegelevel") || session.checkacl2("manageuserprivilegelevel")) { iterator<user> = userfc.findall().iterator(); system.out.println("pc::enum()"); allusers = new hashmap<string, string>(); while (it.hasnext()) { user item = it.next(); allusers.put(item.getname(), item.getuserid().tostring()); } } } return allusers; }
my test class:
package com.controller; import com.entities.user; import com.jsfc.util.jsfutil; import java.util.arraylist; import java.util.list; import java.util.map; import javax.faces.event.actionevent; import org.junit.after; import org.junit.afterclass; import org.junit.before; import org.junit.beforeclass; import org.junit.test; import static org.junit.assert.*; import org.junit.runner.runwith; import static org.mockito.matchers.any; import static org.mockito.matchers.anyint; import static org.mockito.matchers.anyobject; import static org.mockito.matchers.anystring; import org.mockito.mock; import org.mockito.mockito; import static org.mockito.mockito.doreturn; import static org.mockito.mockito.mock; import static org.mockito.mockito.spy; import static org.mockito.mockito.when; import org.mockito.mockitoannotations; import org.mockito.runners.mockitojunitrunner; @runwith(mockitojunitrunner.class) public class personalcentercontrollertest { public personalcentercontrollertest() { } @beforeclass public static void setupclass() { } @afterclass public static void teardownclass() { } @before public void setup() { mockitoannotations.initmocks(this); } @after public void teardown() { } /** * test of getallusers method, of class personalcentercontroller. */ @test public void testgetallusers() { system.out.println("getallusers"); personalcentercontroller pccontroller = new personalcentercontroller(); sessioncontroller session = mockito.mock(sessioncontroller.class); when(session.checkacl2(anystring())).thenreturn(true).thenreturn(true); map<string,string> usermap = null; usermap = pccontroller.getallusers(); } }
as can see, call session.checkacl2() defined in class, use using mockito mock class method inside class fix
sessioncontroller session = mockito.mock(sessioncontroller.class); doreturn(true).when(session).checkacl2((string) anyobject());
but calls nullpointerexception
@ if line.
are passing mocked sessioncontroller
instance personalcentercontroller
instance? if try calling sessioncontroller
methods right after mocking return expected?
sessioncontroller session = mockito.mock(sessioncontroller.class); when(session.checkacl2(anystring())).thenreturn(true).thenreturn(true); asserttrue(session.checkacl2("donatebookprivilegelevel")); asserttrue(session.checkacl2("manageuserprivilegelevel");
as see nullpointerexception
here session
instance either being null
or returning null
, being converted boolean
. first step figure out it.
Comments
Post a Comment