Skip to content

Instantly share code, notes, and snippets.

@gamlerhart
Created September 1, 2012 12:25
Show Gist options
  • Save gamlerhart/3571743 to your computer and use it in GitHub Desktop.
Save gamlerhart/3571743 to your computer and use it in GitHub Desktop.
Google Guava Testlib
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<version>13.0</version>
<scope>test</scope>
</dependency>
private Test myListTestSuite() {
return ListTestSuiteBuilder.using(
// This class is responsible for creating the collection
// And providing data, which can be put into the collection
// Here we use a abstract generator which will create strings
// which will be put into the collection
new TestStringListGenerator(){
@Override
protected List<String> create(String[] elements) {
// Fill here your collection with the given elements
return new MyListImplementation<String>(elements);
}
})
// The name of the test suite
.named("My List Tests")
// Here we give a hit what features our collection supports
.withFeatures(ListFeature.GENERAL_PURPOSE,
CollectionFeature.ALLOWS_NULL_VALUES,
CollectionFeature.SERIALIZABLE,
CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
CollectionSize.ANY)
.createTestSuite();
}
return ListTestSuiteBuilder
.using(new TestStringListGenerator(){
@Override
protected List<String> create(String[] elements) {
return new MyListImplementation<String>(elements);
}
})
.named("My List Tests")
.withFeatures(ListFeature.GENERAL_PURPOSE,
CollectionFeature.ALLOWS_NULL_VALUES,
CollectionFeature.SERIALIZABLE,
CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
CollectionSize.ANY)
.withSetUp(new Runnable() {
@Override
public void run() {
// some setup
}
})
.withTearDown(new Runnable() {
@Override
public void run() {
// some setup
}
})
.createTestSuite();
// Pass your test data provider to the using method
ListTestSuiteBuilder.using(new MyTestDataGenerator())
. // continue as normal
/**
* Own implementation of a test data generator.
* First look if an existing one fits your needs
*/
class MyTestDataGenerator implements TestListGenerator<MySpecialItem> {
@Override
public SampleElements<MySpecialItem> samples() {
return new SampleElements<MySpecialItem>(
new MySpecialItem("1"),
new MySpecialItem("2"),
new MySpecialItem("3"),
new MySpecialItem("4"),
new MySpecialItem("5")
);
}
@Override
public List<MySpecialItem> create(Object... elementsAsObjects) {
// Put the elements in the given array
// into the collection.
MySpecialItem[] castedElements = new MySpecialItem[elementsAsObjects.length];
int i = 0;
for (Object elementAsObject : elementsAsObjects) {
castedElements[i++] = (MySpecialItem) elementAsObject;
}
return new MyListImplementation<MySpecialItem>((MySpecialItem[])castedElements);
}
@Override
public MySpecialItem[] createArray(int length) {
// Just create an array of the element type with the given length
return new MySpecialItem[length];
}
@Override
public Iterable<MySpecialItem> order(List<MySpecialItem> insertionOrder) {
// In case your collection reorders elements,
// Then you should return the original order here
return insertionOrder;
}
}
// Let's assume our collection only can handle specialized items
class MySpecialItem implements Serializable{
private final String data;
MySpecialItem(String data) {
this.data = data;
}
@Override
public String toString() {
return "MySpecialItem{" +
"data='" + data + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MySpecialItem that = (MySpecialItem) o;
if (data != null ? !data.equals(that.data) : that.data != null) return false;
return true;
}
@Override
public int hashCode() {
return data != null ? data.hashCode() : 0;
}
}
// previous calls
.suppressing(Arrays.asList(suppressMethod(ListSubListTester.class, "testSubList_entireList")))
.createTestSuite();
private Method suppressMethod(Class testClass,
String methodName) {
try {
return testClass.getMethod(methodName, new Class[0]);
} catch (NoSuchMethodException e) {
throw new AssertionError("Could not find method to suppress ", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment