How to use Junit with Demoiselle 2.4.2?

Asked

Viewed 270 times

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!

1 answer

0


Alex the problem seems in the injection of Managedbean, probably Démoiselle is not able to do the injection because it is a class managed by JSF. Usually the rules of the business are in the classes with the annotation @Bussinesscontroller. I had not yet tried to inject a Managedbean. Don’t you have a Caixaoperacaobc class where you confirm Bertura() is actually being realized? If you do, change the test class and change your method to receive the balance (or the object that contains it) and insert that class into your test. Managedbean will basically get the data from the screen and direct to the respective business class to carry out the rules. In that case you wouldn’t need it for your tests.

  • 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 example FacesContext. This has now been my problem. My goal in including the ViewControllers 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.