0
Hello
I have a project with the following profile:
- Eclipse Luna
- Java 6
- JSF 2.2/Primefaces 5.3
- Jboss 7
- Demoiselle 2.4.2.
The application already works, the problem is in the execution of test cases of the Junit 4.12. The test case shown below corresponds to the invocation of a method of a Managed bean annotated with @ViewController
, that is, is the test of a backing bean one-page XHTML:
@RunWith(DemoiselleRunner.class)
public class CaixaAberturaTests {
@Inject
CaixaOperacaoEditMB caixaOperacao;
@Test
public void shouldConfirmarAberturaParaUsuarioLogado() {
// Arrange
caixaOperacao.setSaldoInicial(new BigDecimal(100.00));
// Act
String destino = caixaOperacao.confirmarAbertura();
// Assert
assertNotNull(destino);
}
}
When executing, the exception is thrown on the session line Arrange, which is nothing more than the attribution of a value to a property. The code of such a method follows below:
public void setSaldoInicial(BigDecimal value) {
saldoInicial = value;
}
However, the method never gets to be executed, it is intercepted before and in this process is that the exception is thrown, as stack next:
java.lang.NullPointerException
at br.gov.frameworkdemoiselle.internal.context.FacesViewContextImpl.getSession(FacesViewContextImpl.java:143)
at br.gov.frameworkdemoiselle.internal.context.FacesViewContextImpl.getStore(FacesViewContextImpl.java:88)
at br.gov.frameworkdemoiselle.internal.context.AbstractCustomContext.get(AbstractCustomContext.java:92)
at br.gov.frameworkdemoiselle.internal.context.AbstractCustomContext.get(AbstractCustomContext.java:79)
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:73)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:79)
at br.ufpr.restaurante.view.CaixaOperacaoEditMB$Proxy$_$$_WeldClientProxy.setSaldoInicial(CaixaOperacaoEditMB$Proxy$_$$_WeldClientProxy.java)
at br.ufpr.restaurante.view.tests.CaixaAberturaTests.shouldConfirmarAberturaParaUsuarioLogado(CaixaAberturaTests.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at br.gov.frameworkdemoiselle.junit.DemoiselleRunner.runChild(DemoiselleRunner.java:60)
at br.gov.frameworkdemoiselle.junit.DemoiselleRunner.runChild(DemoiselleRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at br.gov.frameworkdemoiselle.junit.DemoiselleRunner.run(DemoiselleRunner.java:73)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
I’d like some tips to solve the case.
Thanks in advance!
Thanks for the answer. After several tests and readings, I realized that the problem is that to use Junit with
ViewControllers
it is necessary to use mocks to provide JSF backend support objects, for exampleFacesContext
. This has now been my problem. My goal in including theViewControllers
is to have the highest code coverage rate in unit tests, as it is application code that also needs to be assured. As I apply TDD, it is natural that whole the written code has a test case for it.– AlexSC