Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Fault Injection into Unit Tests with JBoss Byteman

DZone's Guide to

Fault Injection into Unit Tests with JBoss Byteman

· Java Zone
Free Resource

Make it happen: rapid app development on Kubernetes as a managed service.

How do you test error handling in your application? Do you? Is it at all possible to simulate SocketTimeoutException, SQLException? Normally the answer would be that it isn’t possible or very difficult, requiring a complicated setup. Not anymore – with JBoss Byteman you can easily inject whatever code – e.g. throw new SocketTimeoutException() – in any place – e.g. Socket.connect. The code is injected before your test runs and unloaded when it finishes. Here is an example:

@RunWith(BMUnitRunner.class)
public class BytemanJUnitTests {
@Test(expected=MyServiceUnavailableException.class)
   @BMRule(name="throw timeout at 1st call",
   targetClass = "Socket",
   targetMethod = "connect",
   action = "throw new java.io.IOException()")
   public void testErrorInPipeline() throws Exception {
      // Invokes internally Socket.connect(..):
      new MyHttpClient("http://example.com/data").read();
   }
}

What’s even more cool, the Byteman rules also support various conditions such as count downs or rendezvous – a special object that can be used to synchonize and order independent thread, e.g. to ensure that thread A executes method a before thread B executes method b – which makes it possible to test different orders of execution and concurrency issues that they might cause.

Fault injection in JUnit tests and the use of conditions such as rendezvous is well explained in the official tutorial Fault Injection Testing With Byteman.

In general, JBoss Byteman 2.0.0 is a Bytecode Manipulation, Testing, Fault Injection, Logging and generally aspect-oriented programming tool. More exactly it is a Java agent which helps testing, tracing, and monitoring code. Additional code is injected (instrumentation) based on simple scripts (rules) in the event-condition-action form (the conditions may use counters, timers etc.). Contrary to standard AOP, there is no need to create classes or compile code. “Byteman is also simpler to use and easier to change, especially for testing and ad hoc logging purposes.” “Byteman was invented primarily to support automation of tests for multi-threaded and multi-JVM Java applications using a technique called fault injection.”

Yyou can download Byteman from the project site or get it from the JBoss Maven repository (let’s hope they’ll release into Maven Central too.)

 

From http://theholyjava.wordpress.com/2012/02/25/cool-tools-fault-injection-into-unit-tests-with-jboss-byteman-easier-testing-of-error-handling/

Tutorial: WordPress as distributed microservices on Kubernetes.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}