java - how to mock FacesContext with JMockit, All the tests fail with NullPointerException -
all junit tests fail nullpointerexception when bean uses facescontext or creates object of class uses facescontext. please suggest way test code junit.
bean
@managedbean(name = "displayissuesbean") @sessionscoped public class displayissuesbean implements serializable { private static final long serialversionuid = -3017739535520843880l; private static final logger logger = logger.getlogger(addissuebean.class); private transient addissuedto addnewissuedto = new addissuedto(); private transient idisplayissues displayissuesservice = new displayissuesservice(); private transient list<string> projectnames = displayissuesservice .getprojectnames(getemailid()); private transient list<issuedisplaydto> issues = new arraylist<issuedisplaydto>(); private transient list<issuedisplaydto> modifiedlist = new arraylist<issuedisplaydto>(); private string projectname; private int projectid; int firstid; int secondid; private transient issuedisplaydto backup1; private transient issuedisplaydto backup2; public string getemailid() { string emailid = facescontext.getcurrentinstance().getexternalcontext() .getremoteuser(); logger.info(emailid); return emailid; } public string setissuetypewithissuetypeid(int issuetypeid) { string issuetype = null; if (issuetypeid == 1) { issuetype = "bug"; } else if (issuetypeid == 2) { issuetype = "story"; } else if (issuetypeid == 3) { issuetype = "task"; } return issuetype; }
test
@test public void testissuetypewithissuetypeid() { displayissuesbean bean = new displayissuesbean(); assertequals("bug", bean.setissuetypewithissuetypeid(1)); }
error
java.lang.nullpointerexception @ com.jts1.controller.displayissuesbean.getemailid(displayissuesbean.java:104) @ com.jts1.controller.displayissuesbean.<init>(displayissuesbean.java:28) @ com.jts1.test.displayissuestest.testissuetypewithissuetypeid(displayissuestest.java:13) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:483) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:50) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:47) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:17) @ org.junit.runners.parentrunner.runleaf(parentrunner.java:325) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:78) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:57) @ org.junit.runners.parentrunner$3.run(parentrunner.java:290) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288) @ org.junit.runners.parentrunner.access$000(parentrunner.java:58) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50) @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:459) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:675) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:382) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:192)
mock facescontext
, record expectation externalcontext#getremoteuser()
:
@test public void testissuetypewithissuetypeid( @mocked facescontext faces, @mocked final externalcontext extctx ) { new expectations() {{ extctx.getremoteuser(); result = "test"; }}; displayissuesbean bean = new displayissuesbean(); assertequals("bug", bean.setissuetypewithissuetypeid(1)); }
Comments
Post a Comment