Skip to content
Advertisement

Tag: unit-testing

Verifying partially ordered method invocations in JMockit

I’m trying to write a unit test (using JMockit) that verifies that methods are called according to a partial order. The specific use case is ensuring that certain operations are called inside a transaction, but more generally I want to verify something like this: Method beginTransaction is called. Methods operation1 through to operationN are called in any order. Method endTransaction

Loop through array, each element a JUnit test

I have a JUnit 4 test that loops through an array of test data: Because it’s all in one test method, the whole test stops as soon as one element in the array fails. Is there a way around that, without making a method for each test item? Maybe something with reflection? Answer Use JUnit 4’s parameterized tests. They are

How to write unit test for “InterruptedException”

In attempts of 100% code coverage, I came across a situation where I need to unit test block of code that catches an InterruptedException. How does one correctly unit test this? (JUnit 4 syntax please) Answer Right before invoking addMessage(), call Thread.currentThread().interrupt(). This will set the “interrupt” status flag on the thread. If the interrupted status is set when the

Bringing unit testing to an existing project

I’m working on an existing Java EE project with various maven modules that are developed in Eclipse, bundled together and deployed on JBoss using Java 1.6. I have the opportunity to prepare any framework and document how unit testing should be brought to the project. Can you offer any advice on… JUnit is where I expect to start, is this

JUnit terminates child threads

When I test the execution of a method that creates a child thread, the JUnit test ends before the child thread and kills it. How do I force JUnit to wait for the child thread to complete its execution? Answer After reading the question and some comments, it seems that what you need is a technique for unit testing asynchronous

Using Mockito’s generic “any()” method

I have an interface with a method that expects an array of Foo: I am mocking this interface using Mockito, and I’d like to assert that doStuff() is called, but I don’t want to validate what argument are passed – “don’t care”. How do I write the following code using any(), the generic method, instead of anyObject()? Answer Since Java

Advertisement