Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any publically available java collections unit tests? [duplicate]

Tests which i can plug-in my implementation of Set and ListIterator specifically.

like image 600
i30817 Avatar asked Aug 19 '12 02:08

i30817


People also ask

Should JUnit tests be public?

JUnit 4 requires that all test methods are public and don't return anything. Let's add two test methods to our test class: The testOne() method simply prints the String 'Test One' to System.

Should I test private methods java?

The short answer is that you shouldn't test private methods directly, but only their effects on the public methods that call them. Unit tests are clients of the object under test, much like the other classes in the code that are dependent on the object.

What does .test do in java?

Java testing provides thorough and functioning test cases that can test every aspect of your application. A JUnit test case is exactly what it sounds like: a test scenario measuring functionality across a set of actions or conditions to verify the expected result. JUnit is a simple framework to write repeatable tests.

Why don't we test private methods?

Private methods exist due to code reusability and to avoid having large public methods that do everything. By testing private methods, your tests will become more fragile because and you'll tend to break them every time you change code in and around those private methods.


2 Answers

Guava has a set of TestSuiteBuilders that, together, produce between a few hundred and a few thousand test cases for a given collection implementation, in the guava-testlib component. For example, you might write something like

public static Test suite() {
  return SetTestSuiteBuilder.using(new TestStringSetGenerator() {
      @Override protected Set<String> create(String[] elements) {
         return ImmutableSet.copyOf(elements);
       }
    })
    .named("ImmutableSet");
    .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
        CollectionFeature.SERIALIZABLE,
        CollectionFeature.ALLOWS_NULL_QUERIES)
    .createTestSuite();
}

This produces a complete, extremely exhaustive set of test cases for the Set implementation.

It's not as thoroughly documented as it could be, but it'll get you a very exhaustive test suite.

like image 158
Louis Wasserman Avatar answered Nov 06 '22 04:11

Louis Wasserman


The Hamcrest library provides lots of methods to unit test collections (eg assert that two collections contain the same elements etc). IMHO, it's pretty much an industry standard for this purpose.

like image 37
Bohemian Avatar answered Nov 06 '22 04:11

Bohemian