dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


advertisement
 

Execute EJB JUnit Tests in Your Deployed Apps : Page 3

Extend the life of your JUnit tests by enabling them to be executed inside a real deployed J2EE app, not just on developer PCs.


advertisement

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Step 3: Provide a Mechanism to Pass Back Test Results.

When you execute TestSuites and TestCases, JUnit stores the results inside a special JUnit class called TestResult. Unfortunately, this class isn't serializable and it is tailored for JUnit test runners to use. In order to pass the results outside the container, you have to define an analogous object or DTO (Sun BluePrints) for transferring the result data to the client.

Define a DTO class called TestResultCollection for storing test results. Provide a constructor to initialize a TestResultCollection from a JUnit TestResult instance:

public class TestResultCollection implements Serializable { protected List failures; protected List errors; protected int runCount; public TestResultCollection(TestResult result) { failures = new ArrayList(); errors = new ArrayList(); copyResult(result); } protected void copyResult(TestResult result) { runCount = result.runCount(); Enumeration enum = result.errors(); while(enum.hasMoreElements()) { TestFailure tf = (TestFailure) enum.nextElement(); errors.add(tf.toString()); } enum = result.failures(); while(enum.hasMoreElements()) { TestFailure tf = (TestFailure) enum.nextElement(); failures.add(tf.toString()); } } }



Add the logic to your TestService to execute the JUnit TestSuite and return the results inside a TestResultCollection object:

public TestResultCollection executeTest(String testClass, Map params) { try { // initialise context ApplicationContext.get().putAll(params); // Create Test TestSuite suite = loadTestSuite(testClass); // Execute test TestResult result = new TestResult(); suite.run(result); // Copy results into serialisable collection return new TestResultCollection(result); } finally { ApplicationContext.close(); } }

Step 4: Wrap Your Test Service Inside an EJB.

You now have a working test service that can be embedded inside your server. The last step is to wrap and expose your test functionality using an EJB that the remote clients can access.

Create a stateless session bean called EjbTestRunnerBean. Define one public remote method called executeTest() for executing unit tests. This method is very similar to your existing TestService::executeTest() method with one difference: it has a parameter called teardown for controlling test data removal.

Inside your executeTest() method, instantiate and delegate test execution to your TestService:

public abstract class EjbTestRunnerBean implements javax.ejb.SessionBean { public TestResultCollection executeTest(String testClass, Map params, boolean tearDown) { TestService service = new TestService(); TestResultCollection results = service.executeTest(testClass, params); ... return results; }



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date