开发者

Is there a repository of pre-built JUnit tests for common Java Interfaces?

I'm teaching a data structures class this semester and we buil开发者_StackOverflowd 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?


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:

  • CoreMatchers
  • JUnitMatchers


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜