Tests which i can plug-in my implementation of Set and ListIterator specifically.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With