Stateless session beans are session beans whose instances have no conversational state. This means that all bean instances are equivalent when they are not involved in servicing a client-invoked method. The term 'stateless' signifies that an instance has no state for a specific client.
CalculatorBean
package org.superbiz.stateless.basic;
import javax.ejb.Stateless;
@Stateless
public class CalculatorBean {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
return a / b;
}
public int remainder(int a, int b) {
return a % b;
}
}
CalculatorTest
Our
CalculatorBean
can be easily tested using the EJBContainer
API in EJB 3.1package 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));
}
}
No comments:
Post a Comment