A programmer-oriented testing framework for Java. JUnit - About

When do Java generics require instead of and is there any downside of switching?

Given the following example (using JUnit with Hamcrest matchers)

    Map<String, Class<? extends Serializable>> expected = null;
    Map<String, Class<java.util.Date>> result = null;
    assertThat(result, is(expected));

This does not compile with the JUnit AssertThat method signature of:

public static <T> void assertThat(T actual, Matcher<T> matcher)

The compiler error message is:

Error:Error:line (102)cannot find symbol method
    <? extends>>>)

However, if I change the assertThat method signature to:

public static <T> void assertThat(T result, Matcher<? extends T> matcher)

Then the compilation works.

So three questions:

  1. Why exactly doesn't the current version compile? Although I vaguely understand the covariance issues here, I certainly couldn't explain it if I had to.
  2. Is there any downside in changing the assertThat method to Matcher<? extends T>? Are there other cases that would break if you did that?
  3. Is there any point to the genericizing of the assertThat method in JUnit? The Matcher class doesn't seem to require it, since JUnit calls the matches method, which is not typed with any generic, and just looks like an attempt to force a type safety which doesn't do anything, as the Matcher will just not in fact match, and the test will fail regardless. No unsafe operations involved (or so it seems).

For reference, here is the JUnit implementation of assertThat:

public static <T> void assertThat(T actual, Matcher<T> matcher) {
	assertThat("", actual, matcher);

public static <T> void assertThat(String reason, T actual,
		Matcher<T> matcher) {
	if (!matcher.matches(actual)) {
		Description description= new StringDescription();
		description.appendText("\nExpected: ");
		description.appendText("\n     got: ").appendValue(actual)
		throw new java.lang.AssertionError(description.toString());

Source: (StackOverflow)

JUnit confusion: use 'extend Testcase' or '@Test'?

I've found the proper use (or at least the documentation) of JUnit very confusing. This question serves both as a future reference and as a real question.

If I've understood correctly, there are two main approaches to create and run a JUnit test:

Approach A (JUnit 3-style): create a class that extends TestCase, and start test methods with the word test. When running the class as a JUnit Test (in Eclipse), all methods starting with the word test are automatically run.

import junit.framework.TestCase;
public class DummyTestA extends TestCase {

    public void testSum() {
        int a = 5;
        int b = 10;
        int result = a + b;
        assertEquals(15, result);

Approach B (JUnit 4-style): create a 'normal' class and prepend a @Test annotation to the method. Note that you do NOT have to start the method with the word test.

import org.junit.*;
import static org.junit.Assert.*;

public class DummyTestB {

    public void Sum() {
        int a = 5;
        int b = 10;
        int result = a + b;
        assertEquals(15, result);

Mixing the two seems not to be a good idea, see e.g. this stackoverflow question:

Now, my questions(s):

  1. What is the preferred approach, or when would you use one instead of the other?
  2. Approach B allows for testing for exceptions by extending the @Test annotation like in @Test(expected = ArithmeticException.class). But how do you test for exceptions when using approach A?
  3. When using approach A, you can group a number of test classes in a test suite like this:

    TestSuite suite = new TestSuite("All tests");

    But this can't be used with approach B (since each testclass should subclass TestCase). What is the proper way to group tests for approach B?

Edit: I've added the JUnit versions to both approaches

Source: (StackOverflow)

Why must jUnit's fixtureSetup be static?

I marked a method with jUnit's @BeforeClass annotation, and got this exception saying it must be static. What's the rationale? This forces all my init to be on static fields, for no good reason as far as I see.

In .Net (NUnit), this is not the case.

Edit - the fact that a method annotated with @BeforeClass runs only once has nothing to do with it being a static method - one can have a non-static method run only once (as in NUnit).

Source: (StackOverflow)

Difference between setUp() and setUpBeforeClass()

When unit testing with JUnit, there are two similar methods, setUp() and setUpBeforeClass(). What is the difference between these methods? Also, what is the difference between tearDown() and tearDownAfterClass()?

Here are the signatures:

public static void setUpBeforeClass() throws Exception {

public static void tearDownAfterClass() throws Exception {

public void setUp() throws Exception {

public void tearDown() throws Exception {

Source: (StackOverflow)

Compare two JSON objects in Java

I'm looking for a JSON paring library that supports comparing two JSON objects ignoring child order, specifically for unit testing JSON returning from a web service against an expected value.

Do any of the major JSON libraries support this? the org.json simply does a reference comparison.

Source: (StackOverflow)

Junit: splitting integration test and Unit tests

I've inherited a load of Junit test, but these tests (apart from most not working) are a mixture of actual unit test and integration tests (requiring external systems, db etc).

So I'm trying to think of a way to actually separate them out, so that I can run the unit test nice and quickly and the integration tests after that.

The options are..

  1. Split them into separate directories.

  2. Move to Junit4 (from v3) and annotate the classes to separate them.

  3. Use a file naming convention to tell what a class is , i.e. AdapterATest and AdapterAIntergrationTest.

3 has the issue that Eclipse has the option to "Run all tests in the selected project/package or folder". So it would make it very hard to just run the integration tests.

2: runs the risk that developers might start writing integration tests in unit test classes and it just gets messy.

1: Seems like the neatest solution, but my gut says there must be a better solution out there.

So that is my question, how do you lot break apart integration tests and proper unit tests?

Source: (StackOverflow)

Junit vs TestNG

At work we are currently still using Junit3 to run our tests. We have been considering switching over to Junit4 for new tests being written but I have been keeping an eye on TestNG for a while now. What experiences have you all had with either Junit4 or TestNG and which seems to work better for very large numbers of tests. Having flexibility in writing tests is also important to us since our functional tests cover a wide aspect and need to be written in a variety of ways to get results.

Old tests will not be re-written as they do their job just fine. What I would like to see in new tests though is flexibility in the way the test can be written, natural assertions, grouping, and easily distributed test executions.

Source: (StackOverflow)

When to use Mockito.verify()

I write JUnit test cases for 3 purposes:

  1. To ensure that my code satisfies all of the required functionality, under all (or most of) the input combinations/values.
  2. To ensure that I can change the implementation, and rely on JUnit test cases to tell me that all my functionality is still satisfied.
  3. As a documentation of all the use cases my code handles, and act as a spec for refactoring - should the code ever need to be rewritten. (Refactor the code, and if my jUnit tests fail - you probably missed some use case).

I do not understand why or when Mockito.verify() should be used. When I see verify() being called, it is telling me that my JUnit is becoming aware of the implementation. (Thus changing my implementation would break my jUnits, even though my functionality was unaffected).

I'm looking for:

  1. What should be the guidelines for appropriate usage of Mockito.verify()

  2. Is it fundamentally correct for jUnits to be aware of, or tightly coupled to, the implementation of the class under test?

Source: (StackOverflow)

Pass a local file in to URL in Java

How do I create a new URL object using a local file, for the purpose of unit tests?


Source: (StackOverflow)

Why should I use Hamcrest-Matcher and assertThat() instead of traditional assertXXX()-Methods

When I look at the examples in the Assert class JavaDoc

assertThat("Help! Integers don't work", 0, is(1)); // fails:
// failure message:
// Help! Integers don't work
// expected: is <1> 
// got value: <0>
assertThat("Zero is one", 0, is(not(1))) // passes

I dont see a big advantage over, let's say, assertEquals( 0, 1 ).

It's nice maybe for the messages if the constructs get more complicated but do you see more advantages? Readability?

Source: (StackOverflow)

JUnit XML Format Specification that Hudson supports

I have Hudson as continuous integration server and I want to use option 'Publish JUnit test result report'. But I don't use xUnit tools for testing, instead of that i have shell scripts which run tests and return results in simple format. I thinking to make script which transforms these results to JUnit format. So i'm interesting how JUnit file must looks?

Source: (StackOverflow)

Meaning of epsilon argument of assertEquals for double values

I have a question about junit assertEquals to test double values. Reading API doc I can see:

public static void assertEquals(double expected, double actual)

Deprecated. Use assertEquals(double expected, double actual, double epsilon) instead

What does epsilon value mean? (Epsilon is a letter in the Greek alphabet, right?).

Can Someone explain to me how to use it?

Source: (StackOverflow)

How to capture a list of specific type with mockito

Is there a way to capture a list of specific type using mockitos ArgumentCaptore. This doesn't work:

ArgumentCaptor<ArrayList<SomeType>> argument = ArgumentCaptor.forClass(ArrayList.class);

Source: (StackOverflow)

Java: How to test methods that call System.exit()?

I've got a few methods that should call System.exit() on certain inputs. Unfortunately, testing these cases causes JUnit to terminate! Putting the method calls in a new Thread doesn't seem to help, since System.exit() terminates the JVM, not just the current thread. Are there any common patterns for dealing with this? For example, can I subsitute a stub for System.exit()?

[EDIT] The class in question is actually a command-line tool which I'm attempting to test inside JUnit. Maybe JUnit is simply not the right tool for the job? Suggestions for complementary regression testing tools are welcome (preferably something that integrates well with JUnit and EclEmma).

Source: (StackOverflow)

Getting "NoSuchMethodError: org.hamcrest.Matcher.describeMismatch" when running test in IntelliJ 10.5

I'm using JUnit-dep 4.10 and Hamcrest 1.3.RC2.

I've created a custom matcher that looks like the following:

public static class MyMatcher extends TypeSafeMatcher<String> {
    protected boolean matchesSafely(String s) {
        /* implementation */

    public void describeTo(Description description) {
        /* implementation */

    protected void describeMismatchSafely(String item, Description mismatchDescription) {

        /* implementation */

It works perfectly fine when run from the command line using Ant. But when run from IntelliJ, it fails with:

java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
    at org.hamcrest.MatcherAssert.assertThat(
    at org.hamcrest.MatcherAssert.assertThat(

My guess is that it's using the wrong hamcrest.MatcherAssert. How do I find which hamcrest.MatcherAssert it's using (ie which jar file it's using for hamcrest.MatcherAssert)? AFAICT, the only hamcrest jars in my classpath is 1.3.RC2.

Is IntelliJ IDEA using it's own copy of JUnit or Hamcrest?

How do I output the runtime CLASSPATH that IntelliJ is using?

Source: (StackOverflow)