EJB3+Tutorial+1+-+Initialization+and+Lookup

There's a bit of boilerplate code to get the JBoss EJB3 Embeddable Container initialized before we can look up our session bean. For now we'll just use this code so we won't describe it in any detail here.

By the way, I recommend you cut and past this code rather than type it.

include page="Ejb3JBossUtilJava"

To create this file,
 * 1) Select your **src** directory
 * 2) Right-click and select **New::Class**
 * 3) For **Class Name** enter **JBossUtil**
 * 4) For **Package** enter **util**
 * 5) Click **Finish**
 * 6) Type or copy the code from above into the new file then save it by pressing ctrl-s

Using JUnit
Now we need to enter basic system setup and then execute a test. The following unit test performs basic setup and initialization, looks up a session bean and sends it a message.

To create this test: code format="java5" package service.impl;
 * 1) Select your **test** source folder
 * 2) Right-click, select **New:Class**
 * 3) Enter **HelloWorldServiceImplTest** for the name
 * 4) Enter **ervice.impl** for the package
 * 5) Click **Finish**
 * 6) Copy the text below into the file (replacing the entire contents)
 * 7) Save the file

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

import service.HelloWorldService; import util.JBossUtil;

public class HelloWorldServiceImplTest { private HelloWorldService service;

@BeforeClass public static void startDeployer { // Note, we could stop the deployer when we are done but we do not since // the JVM will shut down and stop the deployer for us. JBossUtil.startDeployer; }

@Before public void lookupService { service = JBossUtil.lookup(HelloWorldService.class,               "HelloWorldServiceImpl/local"); }

@Test public void sayHello { service.sayHelloTo("Brett"); } } code

Execute your "Unit Test"
Run this JUnit "test" and make sure it runs successfully. (Right-click on the class, select **Run As:JUnit Test**.

You will see the following output: code Hello to Brett code

Note that this example produces output to the console. This example service is not really very testable. How might you "fix" this?

Nice
Congratulations, you've created your first EJB3 Session bean and executed it.