Unit Testing JBoss 5 Services

[复制链接]
查看11 | 回复2 | 2014-2-19 11:55:14 | 显示全部楼层 |阅读模式
The JBoss Microcontainer is a refactoring of JBoss’s JMX Microkernel to support direct POJO deployment and standalone use outside the JBoss application server.
It allows the creation of services using simple Plain Old Java Objects (POJOs) to be deployed into a standard Java SE runtime environment.
JBoss Microcontainer uses dependency injection to wire individual POJOs together to create services. Configuration is performed using either annotations or XML depending on where the information is best located.
The goal of this article is to show how easy it is to test these services using TestNG testing framework.
Configuring a service
PersonService is a simple POJO that doesn’t implement any special interfaces.
It has two properties, firstName and lastName, that are inject through a XML deployment descriptor.
The public method that clients will call is getName() and returns the person full name.
?Download PersonService.java
public class PersonService {
private String firstName;
private String lastName;

public void setFirstName(String firstName) { this.firstName = firstName; }
public void setLastName(String lastName) { this.lastName = lastName; }

public String getName() {
return firstName + " " + lastName;
}
}
Instances of this service are created by creating an XML deployment descriptor that contains a list of beans representing individual instances.
?Download jboss-beans.xml



Samuel
Santos



We have just declared that we want to create an instance of the PersonService class and register it with the name PersonService. This file is passed to an XML deployer associated with the microcontainer at runtime to perform the actual deployment and instantiate the beans.
Testing a service
JBoss Microcontainer makes it extremely easy to unit test services.
First, you need to create an EmbeddedBootstrap class and override the protected bootstrap() method.
This allows you to created an instance of JBoss Microcontainer together with an XML deployer.
?Download EmbeddedBootstrap.java
public class EmbeddedBootstrap extends BasicBootstrap {
protected BasicXMLDeployer deployer;

public EmbeddedBootstrap() throws Exception {
super();
}

public void bootstrap() throws Throwable {
super.bootstrap();
deployer = new BasicXMLDeployer(getKernel());
Runtime.getRuntime().addShutdownHook(new Shutdown());
}

public void deploy(URL url) {
try {

// Workaround the fact that the BasicXMLDeployer does not handle redeployment correctly

if (deployer.getDeploymentNames().contains(url.toString())) {

log.info("Service is already deployed.");

return;

}

deployer.deploy(url);
} catch (Throwable t) {

log.warn("Error during deployment: " + url, t);
}
}

public void undeploy(URL url) {
if (!deployer.getDeploymentNames().contains(url.toString())) {

log.info("Service is already undeployed.");

return;
}
try {

deployer.undeploy(url);
} catch (Throwable t) {

log.warn("Error during undeployment: " + url, t);
}
}

protected class Shutdown extends Thread {
public void run() {

log.info("Shutting down");

deployer.shutdown();
}
}
}
Next, you need to bootstrap the microcontainer. This is done in BaseTest class.
?Download BaseTest.java
public class BaseTest {
private URL url;
private EmbeddedBootstrap bootstrap;
private Kernel kernel;
private KernelController controller;

@BeforeSuite
public void setUpSuite() throws Exception {
// Start JBoss Microcontainer
bootstrap = new EmbeddedBootstrap();
bootstrap.run();

kernel = bootstrap.getKernel();
controller = kernel.getController();
}

@AfterSuite
public void tearDownSuite() {
}

protected void deploy(String url) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
this.url = cl.getResource(url);

bootstrap.deploy(cl.getResource(url));
}

protected void undeploy() {
bootstrap.undeploy(url);
}

@SuppressWarnings( { "unchecked" })
protected T getService(String name) {
ControllerContext context = controller.getInstalledContext(name);

return (context == null) ? null : (T) context.getTarget();
}
}
All the test classes extend this one, this allows you to run the microcontainer only once before the test suite.
Another benefit is to have all the utility methods used in every test class in a central place.
Now we can proceed to the last part, and that is to deploy the Person service.
?Download PersonServiceTest.java
public class PersonServiceTest extends BaseTest {
private static final Logger LOGGER = LoggerFactory.getLogger(PersonServiceTest.class);
private PersonService service;

@BeforeClass
public void setUp() {
deploy("jboss-beans.xml");
service = getService("PersonService");
}

@AfterClass
public void tearDown() {
undeploy();
}

@Test
public void getName() {
String name = service.getName();
LOGGER.info("Name: {}", name);
assertEquals("Samuel Santos", name);
}
}
To see it in action, get this example source code and run the Maven command mvn test.
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
nice job
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
GOOD!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行