Skip to content

Instantly share code, notes, and snippets.

@takawitter
Created December 2, 2016 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takawitter/faff1bdd72dd87eea1a64e02b4a30607 to your computer and use it in GitHub Desktop.
Save takawitter/faff1bdd72dd87eea1a64e02b4a30607 to your computer and use it in GitHub Desktop.
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Consumer;
public class CollectionFixture<T> {
public CollectionFixture(Collection<T> collection){
this.collection = collection;
this.it = collection.iterator();
}
public CollectionFixture<T> assertHasNext(){
assertTrue(it.hasNext());
return this;
}
public CollectionFixture<T> assertNotHasNext(){
assertFalse(it.hasNext());
return this;
}
public CollectionFixture<T> assertSize(int expected){
assertEquals(expected, collection.size());
return this;
}
@SuppressWarnings("unchecked")
public <U extends T> CollectionFixture<T> next(Consumer<U> c){
c.accept((U)it.next());
return this;
}
private Collection<T> collection;
private Iterator<T> it;
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
public class SampleTest{
static class Fruit{}
static class Apple extends Fruit{boolean bitten;}
static class Orange extends Fruit{enum Type{Tangerine, Mandarin} Type type = Type.Tangerine;}
@Test
public void t() throws Throwable{
new CollectionFixture<Fruit>(Arrays.asList(new Apple(), new Orange()))
.next((Apple f) -> {
assertFalse(f.bitten);
})
.next((Orange f) -> {
assertEquals(f.type, Orange.Type.Tangerine);
})
.assertNotHasNext();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment