(Unit) Testing Swiss Knife: All the Tools You Wanted to Know

I love testing. And I like productivity. There are many tools and libraries that make writing tests easier, more convenient, more fun. I would like to introduce here those that I found the most useful during the years, from selected advanced features of JUnit to assertion libraries, powerful behavior/fault injection, testing of database-related code, andContinue reading “(Unit) Testing Swiss Knife: All the Tools You Wanted to Know”

Advertisement

hasProperty, the Hidden Gem of Hamcrest (and assertThat)

If you got used to JUnit 4’s assertThat with various matchers (of course you will need junit-dep.jar and hamcrest.jar to get the full set instead of the small subset integrated in junit.jar), make sure you don’t overlook the matcher hasProperty. It is very useful if you have non-trivial objects and cannot use some more flexibleContinue reading “hasProperty, the Hidden Gem of Hamcrest (and assertThat)”

JUnit Tip: Verifying that an Exception with a Particular Message was Thrown

JUnit has a hidden treasure which makes it easy to do something we have long longed for – namely not only to verify that an exception of a particular type has been thrown but also that its message contains the expected message. The hidden pearl is the @Rule ExpectedException and its JavaDoc documents well howContinue reading “JUnit Tip: Verifying that an Exception with a Particular Message was Thrown”

Link: Advanced Usage of JUnit Theories, Multiple DataPoints, and ParameterSuppliers

It is surprising how difficult it is to find documentation for some JUnit features such as Theories. May be they haven’t bothered to write it because it is still considered “experimental” (even though included in JUnit since 4.4). As usually we have to rely on Jens Schauder’s blog and I’d like to draw your attentionContinue reading “Link: Advanced Usage of JUnit Theories, Multiple DataPoints, and ParameterSuppliers”

DRY: Use JUnit @Rule Instead of Repeating Setup/@Before in Each Test

I was for a long time unhappy that DbUnit Express users have to create a @Before method in each test just to get the test database initialized. Fortunately since version 1.3.0 they don’t need to do it anymore thanks to JUnit Rules (if you are not familiar with them, they are an alternative to @Before/@After and @BeforeClass/@AfterClass, readContinue reading “DRY: Use JUnit @Rule Instead of Repeating Setup/@Before in Each Test”

Mocking out LDAP/JNDI in unit tests

When unit testing a class that queries an LDAP server using Java’s JNDI API I needed to replace the actual remote LDAP server with a mock LDAP access layer so that the unit test (remember, this is not an integration test) doesn’t depend on any external SW/HW. Few hours of googling haven’t yielded any suitableContinue reading “Mocking out LDAP/JNDI in unit tests”

Injecting timing aspect into JUnit test in Eclipse using AspectJ, AJDT

Introduction This blog describes my experience with using AOP, namely AspectJ, to measure the individual execution times and their average of a particular low-level method invoked many times during a JUnit test run from within Eclipse. It concentrates on creating the aspect and integrating (weaving) it into the JUnit test execution in the Eclipse environment.Continue reading “Injecting timing aspect into JUnit test in Eclipse using AspectJ, AJDT”