views:

46

answers:

3

I'm teaching a data structures class this semester and we build gobs of JUnit tests given component interfaces. We then use setUp to plug in a specific concrete implementation. In most cases we are implementing java.util interfaces. Is there a common set of rigorous JUnit tests for common Java components, or some sort of framework that makes designing the test cases less painful?

+2  A: 

For these purposes mocking may be the easiest way of having "concrete" implementations used by unit tests. One of the best is Mockito.

Boris Pavlović
+1  A: 

You could look at how Apache Harmony and GNU Classpath unit test their respective clean-room implementations of the java.util classes.

Stephen C
+2  A: 

Hamcrest provides several matchers that are specific to Maps and Collections.
You can find them in the tutorial.

(JUnit 4.x contains Hamcrest)

Sample code:

import static org.junit.Assert.*;
import static org.junit.matchers.JUnitMatchers.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;

public class DummyTest{

    @Test
    public void someTest(){

        final Collection<Integer> coll = Arrays.asList(1, 2, 3, 4, 5);
        assertThat(coll, hasItems(5, 4, 3, 2, 1));
        assertThat(coll, not(hasItems(6)));
        assertThat(coll,
            both(
                is(List.class)
            ).and(
                equalTo(
                    Arrays.asList(
                        0x1,0x2,0x3,0x4,0x5
                    )
            )
        ));

    }



}

Your main starting point would be these convenience classes:

seanizer