as presented at JBug Newcastle the 13th December 2011
git clone git://github.com/Sanne/ArquillianDemo.git
cd ArquillianDemo
mvn dependency:go-offline
(optional, but let's start downloading dependencies already)
Using Eclipse, it's recommended to use the plugin m2e
to import the Maven project to have it setup all dependencies. All mayor other IDEs have Maven integrations, or you can use the command line instead.
The git master branch you checked out has some implementations of EJBs, but tests are missing.
The project is already setup to use Arquillian and ShrinkWrap in the Maven pom.xml, but the test implementations are missing.
-
Locate and open
com.acme.ejb.GreeterTestCase
-
Add
@RunWith(Arquillian.class)
on the top level type to flag it as an Arquillian test (org.junit.runner.RunWith and org.jboss.arquillian.junit.Arquillian) -
Add a deployment descriptor:
@Deployment public static JavaArchive createDeployment() { return ShrinkWrap.create(JavaArchive.class, "test.jar").addClasses(Greeter.class, GreeterBean.class); }
-
Have the EJB injected in the test:
@EJB Greeter greeter;
-
Now verify that injection will work, and that the bean behaves as expected:
@Test public void shouldBeAbleToInjectEJBAndInvoke() throws Exception { String userName = "Earthlings"; Assert.assertEquals("Hello, " + userName, greeter.greet(userName)); }
-
Run the test. As any usual JUnit test!
-
Locate and open
com.acme.ejb.TemperatureConverterTestCase
-
Add
@RunWith(Arquillian.class)
on the top level type to flag it as an Arquillian test (org.junit.runner.RunWith and org.jboss.arquillian.junit.Arquillian) -
Add a deployment descriptor:
@Deployment public static JavaArchive createDeployment() { return ShrinkWrap.create(JavaArchive.class, "test.jar").addClasses(TemperatureConverter.class, TemperatureConverterBean.class); }
-
Have the EJB injected in the test:
@EJB TemperatureConverter converter;
-
Add some tests:
@Test public void testConvertToCelsius() { assertEquals(converter.convertToCelsius(32d), 0d, 0d); assertEquals(converter.convertToCelsius(212d), 100d, 0d); } @Test public void testConvertToFarenheit() { assertEquals(converter.convertToFarenheit(0d), 32d, 0d); assertEquals(converter.convertToFarenheit(100d), 212d, 0d); } @Test public void testTransactionActive() { assertTrue(converter.isTransactionActive()); }
-
Run it, again as usual.
-
Locate and open
com.acme.ejb.calc.MortgageCalculatorEnvEntryTestCase
-
Add
@RunWith(Arquillian.class)
again -
Add a deployment descriptor, this the we make a WAR archive:
@Deployment public static Archive<?> createDeployment() { // we have to create a war because ejb-jar.xml must be put in WEB-INF return ShrinkWrap.create(WebArchive.class, "test.war") .addClasses(MortgageCalculator.class, MortgageCalculatorBean.class) .addAsWebInfResource("interest-rate-ejb-jar.xml", "ejb-jar.xml"); }
-
Inject the calculator bean:
@EJB MortgageCalculator calculator;
-
Test that the calculator was configured by our xml configuration file:
@Test public void shouldCalculateMonthlyPaymentAccuratelyWithBuiltInRate() { Assert.assertEquals("Interest rate should be set by ejb-jar.xml", 5.5, calculator.getCurrentInterestRate()); double principal = 750000; int term = 30; BigDecimal expected = new BigDecimal(Double.toString(4258.42)); BigDecimal actual = calculator.calculateMonthlyPayment(principal, term); Assert.assertEquals("A banking error has been detected!", expected, actual); }
-
Run the tests
- You could have a look into com.acme.ejb.nointerface.NoInterfaceEJBTestCase
git checkout solution