JUnit
Capitulo 2 JUnit EJB
En este capitulo mostrare como hacer un Test de un EJB Basico.
Referencia
https://www.youtube.com/watch?v=iFMxRTPO7FA
https://netbeans.org/kb/docs/javaee/javaee-entapp-junit.html
http://blog.payara.fish/testing-applications-with-payara-server-arquillian
Testing Calculadora
En este capitulo mostrare como hacer un Test de un EJB Basico
Crear un proyecto EJB

Project Name: ejbtest

En el Servidor seleccionar GlassFish

Se crea el proyecto

Crear un Session Bean

Name: Calculator

Dar clic derecho y seleccionar Insert Code

Ahora seleccionar Add Business Method

En el Name:add
en Return Type: int
Agregar dos parƔmetros de tipo int a,b.

Genera el código

Modificar el mƩtodo add
@Override
public int add(int a, int b) {
return a +b;
}
Refactorizar la interfaz CalculatorLocal y colocarle el nombre de CalculatorRemote

Editar y cambiar la anotación @Local por @Remote
package com.avbravo.ejb;
import javax.ejb.Remote;
/**
*
* @author avbravo
*/
@Remote
public interface CalculatorRemote {
int add(int a, int b);
}
Agregar dependencias
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.1</version>
<scope>test</scope>
</dependency>
Crear el Test
Categories: Unit Test
File Types: Test for Existing Class

Seleccionar la clase Calculator

Se genera la clase CalculatorTest
package com.avbravo.ejb;
import javax.ejb.embeddable.EJBContainer;
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.*;
/**
*
* @author avbravo
*/
public class CalculatorTest {
public CalculatorTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of add method, of class Calculator.
*/
@org.junit.Test
public void testAdd() throws Exception {
System.out.println("add");
int a = 0;
int b = 0;
EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer();
CalculatorLocal instance = (CalculatorLocal)container.getContext().lookup("java:global/classes/Calculator");
int expResult = 0;
int result = instance.add(a, b);
assertEquals(expResult, result);
container.close();
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
}
Testing Metodo String
Podemos hacer un Test basico de un mƩtodo que devuelve un String
Clase App
package org.glassfish.embedded.samples;
import javax.ejb.Stateless;
/\*\*
\* Hello world!
\*/
@Stateless
public class App {
public static String sayHello(String name) {
return "Hello " + name;
}
}
Test
Verificar que no sea null la clase, que devuelva el mensaje.
public void testEJB() throws NamingException {
EJBContainer ejbC = EJBContainer.createEJBContainer();
Context ctx = ejbC.getContext();
App app = (App) ctx.lookup("java:global/classes/App");
assertNotNull(app);
String NAME = "Duke";
String greeting = app.sayHello(NAME);
assertNotNull(greeting);
assertTrue(greeting.equals("Hello " + NAME));
ejbC.close();
}
Last updated
Was this helpful?