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
Tag: unit-testing
What is the purpose of @SmallTest, @MediumTest, and @LargeTest annotations in Android?
I’m new to Android and I’ve seen example code using these annotations. For example: What does that annotation accomplish? Answer As an addition to Davidann’s answer and mainly OP’s question in the comment: In the context of the code above, does it actually DO anything except leave a note for other developers? Does it enforce anything? Are there any tools
TestNg’s @BeforeTest on base class only happening once per fixture
I’m trying to use @BeforeTest to get code to … run once before every test. This is my code: “BeforeTest” is only printed once, not twice. What am I doing wrong? Answer Use @BeforeMethod, not @BeforeTest. The meaning of @BeforeTest is explained in the documentation.
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
Unit testing mathematical code
I am writing a small utility for calculating a complicated mathematical formula (using commons-math library for integration and root finding). I was trying to write it in the same way as a normal …
Specifying an order to junit 4 tests at the Method level (not class level)
I know this is bad practice, but it needs to be done, or I’ll need to switch to testng. Is there a way, similar to JUnit 3’s testSuite, to specify the order of the tests to be run in a class? Answer If you’re sure you really want to do this: There may be a better way, but this is
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