Solucionado (ver solução)
Solucionado
(ver solução)
4
respostas

Testes em ejb

Olá pessoal,

Estou a algum tempo tentando fazer testes nos ejbs usando o JUnit

Minha configuração está abaixo: Wildfly 10 Java 8 Java EE 7 EJB 3.1

Tentei fazer os testes usando Junit 4.12 e OpenEJB 7.0.3 e não obtive sucesso. Tentei rodar o teste no site da apache https://github.com/apache/tomee/tree/042d4d9fc647c32ee31c4c7455a4769817564340/examples/simple-stateless

Fiz a troca do Openejb 4.5.1 pelo 7.0.3 porém após colocar todos os jars solicitados tomei o erro abaixo: javax.ejb.EJBException: Provider error. No provider found at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:67) at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:43) at org.superbiz.stateless.basic.CalculatorTest.startTheContainer(CalculatorTest.java:38) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 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:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) Caused by: javax.validation.ValidationException: Could not create Configuration. at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:166) at org.apache.openejb.assembler.classic.ValidatorBuilder.getConfig(ValidatorBuilder.java:154) at org.apache.openejb.assembler.classic.ValidatorBuilder.buildFactory(ValidatorBuilder.java:107) at org.apache.openejb.assembler.classic.ValidatorBuilder.buildFactory(ValidatorBuilder.java:65) at org.apache.openejb.assembler.classic.LazyValidatorFactory.ensureDelegate(LazyValidatorFactory.java:53) at org.apache.openejb.assembler.classic.LazyValidatorFactory.getFactory(LazyValidatorFactory.java:62) at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:926) at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:718) at org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:343) at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:56) ... 18 more Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:164) ... 27 more

alguém consegue me dar uma luz?

4 respostas

Como tá o código do seu teste? Acho que pode ser bom vocÊ postar, porque aí mais gente consegue ajudar. Eu, particularmente, nunca tive experiência fazendo este teste integrado de ejb. Minha ideia sempre foi fazer de unidade mesmo, passando as coisas no construtor e mandando bala.

Olá alberto ó código de teste é exatamente o que está no github https://github.com/apache/tomee/blob/042d4d9fc647c32ee31c4c7455a4769817564340/examples/simple-stateless/src/test/java/org/superbiz/stateless/basic/CalculatorTest.java

O que eu alterei foi o jar pois eu preciso usar meu repositório interno, e esses jars não são os mesmos do repositório oficial do maven.

O meu problema prático é que existem muitas regras de negócio dentro do EJB, é muito difícil descobrir se a regra foi quebrada numa refatoração por que não consigo testar os containers.

O Código de teste do arquivo.

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.superbiz.stateless.basic;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.ejb.embeddable.EJBContainer;
import javax.naming.NamingException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class CalculatorTest {

    private static EJBContainer ejbContainer;

    private CalculatorBean calculator;

    @BeforeClass
    public static void startTheContainer() {
        ejbContainer = EJBContainer.createEJBContainer();
    }

    @Before
    public void lookupABean() throws NamingException {
        Object object = ejbContainer.getContext().lookup("java:global/simple-stateless/CalculatorBean");

        assertTrue(object instanceof CalculatorBean);

        calculator = (CalculatorBean) object;
    }

    @AfterClass
    public static void stopTheContainer() {
        if (ejbContainer != null) {
            ejbContainer.close();
        }
    }

    /**
     * Test Add method
     */
    @Test
    public void testAdd() {

        assertEquals(10, calculator.add(4, 6));

    }

    /**
     * Test Subtract method
     */
    @Test
    public void testSubtract() {

        assertEquals(-2, calculator.subtract(4, 6));

    }

    /**
     * Test Multiply method
     */
    @Test
    public void testMultiply() {

        assertEquals(24, calculator.multiply(4, 6));

    }

    /**
     * Test Divide method
     */
    @Test
    public void testDivide() {

        assertEquals(2, calculator.divide(12, 6));

    }

    /**
     * Test Remainder method
     */
    @Test
    public void testRemainder() {

        assertEquals(4, calculator.remainder(46, 6));

    }

}
solução!

Realmente preciso entender melhor como fazer testes unitários em classes que existam ejb, acabei saindo do projeto que precisava criar os testes.

Acho que me falta um entendimento melhor do ejb e como fazer testes unitários quando o BO é um ejb.