junit 4.11 – die neuerungen · 2014. 8. 13. · Überjunit kentbeck: aprogrammer-orientedtesting...

40
JUnit 4.11 – Die Neuerungen Marc Philipp VKSI Sneak Preview, 18. September 2013

Upload: others

Post on 02-Mar-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

JUnit 4.11 – Die NeuerungenMarc PhilippVKSI Sneak Preview, 18. September 2013

Page 2: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Über JUnit

Kent Beck:A programmer-oriented testingframework for Java.David Saff:JUnit is the intersection of allpossible useful Java testframeworks, not their union.Nicht nur für Unit Tests!

VKSI Sneak Preview, 18. September 2013 2

Page 3: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Neue Features seit Version 4.0

JUnit 4.11

Matchers

Theories

Rules

Categories

VKSI Sneak Preview, 18. September 2013 3

Page 4: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Matchers

René Magritte, Untitled 4

Page 5: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Neue Assertion: assertThat(...)

I Neue Assert-Methoden:<T> void assertThat(T actual, Matcher<? super T> matcher)<T> void assertThat(String reason, T actual, Matcher<? super T> matcher)

I Parameters:reason Zusätzliche Beschreibung für den Fehlerfall (optional)actual Tatsächlicher Wertmatcher Hamcrest-Matcher überprüft tatsächlichen Wert

VKSI Sneak Preview, 18. September 2013 5

Page 6: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Bessere Lesbarkeitimport static org.hamcrest.CoreMatchers.is;

public class BetterReadability {

@Test public void withoutMatchers() {assertEquals(2, 1 + 1);

}

@Test public void withMatchers() {assertThat(1 + 1, is(2));

}}

I Kein Raten mehr, was Erwartung bzw. tatsächliches Ergebnis istI Häufig besser lesbar als herkömmliche Assertions

VKSI Sneak Preview, 18. September 2013 6

Page 7: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Matcher kombinieren

Matcher lassen sich einfach kombinieren:assertThat( 1 + 1, is( not( 3 ) ) );

assertThat( 1 + 1, is( both( greaterThan( 1 ) ).and( lessThan( 3 ) ) ) );assertThat( 1 + 1, is( allOf( greaterThan( 1 ), lessThan( 3 ) ) ) );

assertThat( 1 + 1, is( either( greaterThan( 1 ) ).or( greaterThan( 3 ) ) ) );assertThat( 1 + 1, is( anyOf( greaterThan( 1 ), greaterThan( 3 ) ) ) );

VKSI Sneak Preview, 18. September 2013 7

Page 8: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Aussagekräftige FehlermeldungenHerkömmliche Zusicherung ohne BeschreibungassertFalse(asList(1, 2, 3).contains(2));

Ergebnis:AssertionError: at de.andrena.junit...

Neue Zusicherung mit MatcherassertThat(asList(1, 2, 3), not(hasItem(2)));

Ergebnis:AssertionError:Expected: not a collection containing <2>

but: was <[1, 2, 3]>

VKSI Sneak Preview, 18. September 2013 8

Page 9: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Vordefinierte MatcherAllgemein

assertThat( null, is( nullValue() ) );

int[] results = { 1, 3, 5, 7 };assertThat( 3, isIn( results ) ); // Auch mit Collections

assertThat( 3, isOneOf( 1, 3, 5, 7 ) );

VKSI Sneak Preview, 18. September 2013 9

Page 10: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Vordefinierte MatcherStrings

assertThat( "Hallo Welt", containsString( "o We" ) );assertThat( "Hallo Welt", startsWith( "Hall" ) );assertThat( "Hallo Welt", endsWith( "elt" ) );

assertThat( "Hallo Welt", is( equalToIgnoringCase( "hallO welT" ) ) );assertThat( "Hallo Welt", is( equalToIgnoringWhiteSpace( " Hallo Welt " ) ) );

assertThat( "", isEmptyString() );assertThat( "", isEmptyOrNullString() );

VKSI Sneak Preview, 18. September 2013 10

Page 11: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Vordefinierte MatcherComparables

assertThat( 1 + 1, comparesEqualTo( 2 ) );

assertThat( 1 + 1, greaterThan( 1 ) );

assertThat( 1 + 1, greaterThanOrEqualTo( 0 ) );

assertThat( 2.00001, is( closeTo( 2.0, 0.001 ) ) );

VKSI Sneak Preview, 18. September 2013 11

Page 12: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Vordefinierte MatcherIterables

List<Integer> list = Arrays.asList(1, 3, 5, 7);

assertThat( list, hasItem( 1 ) );assertThat( list, hasItem( greaterThan( 6 ) ) );

assertThat( list, hasItems( 5, 1 ) );assertThat( list, hasItems( lessThan( 4 ), greaterThan( 6 ) ) );

assertThat( list, everyItem( greaterThanOrEqualTo( 1 ) ) );

assertThat( list, contains( 1, 3, 5, 7 ) ); // auch mit Matchern

assertThat( list, containsInAnyOrder( 5, 1, 7, 3 ) );assertThat( list, containsInAnyOrder( is( 5 ), greaterThan( 2 ),

greaterThanOrEqualTo( 6 ), lessThan( 4 ) ) );

// auch mit Matchern:assertThat( list, hasSize( greaterThanOrEqualTo( 3 ) ) );assertThat( list, Matchers.<Integer> iterableWithSize( lessThan( 10 ) ) );

VKSI Sneak Preview, 18. September 2013 12

Page 13: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Vordefinierte MatcherMaps

Map<Integer, String> map = new HashMap<Integer, String>();map.put( 1, "one" );

assertThat( map, hasKey( 1 ) );

assertThat( map, hasValue( "one" ) );

assertThat( map, hasEntry( 1, "one" ) );

// natuerlich auch mit Matchern :-)

. . . und viele mehr . . .VKSI Sneak Preview, 18. September 2013 13

Page 14: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Hamcrest  1.3      Quick  Reference  General  purpose  is(T)  equalTo(T)    not(T)       :  Matcher<T>    

anything()  anything(String)     :  Matcher<Object>    

any(Class<T>)  instanceOf(Class<?>)  isA(Class<T>)     :  Matcher<T>    

nullValue()     :  Matcher<Object>  nullValue(Class<T>)     :  Matcher<T>  notNullValue()     :  Matcher<Object>  notNullValue(Class<T>)     :  Matcher<T>    

sameInstance(T)  theInstance(T)     :  Matcher<T>    

isIn(Collection<T>)  isIn(T[])  isOneOf(T...)  hasToString(String)  hasToString(Matcher<?  super  String>)     :  Matcher<T>  

Combining  multiple  matchers  is(Matcher<T>)  not(Matcher<T>)     :  Matcher<T>    

allOf(Matcher<?  super  T>...)  allOf(Iterable<Matcher<?  super  T>>)  anyOf(Matcher<?  super  T>...)  anyOf(Iterable<Matcher<?  super  T>>)     :  Matcher<T>    

both(Matcher<?  super  LHS>)  either(Matcher<?  super  LHS>)     :  Matcher<LHS>    

describedAs(String,  Matcher<T>,  Object...)     :  Matcher<T>  

Strings  containsString(String)  startsWith(String)  endsWith(String)     :  Matcher<String>    

equalToIgnoringCase(String)  equalToIgnoringWhiteSpace(String)     :  Matcher<String>    

isEmptyString()  isEmptyOrNullString()     :  Matcher<String>    

stringContainsInOrder(Iterable<String>)     :  Matcher<String>  

Iterables  everyItem(Matcher<U>)     :  Matcher<Iterable<U>>    

hasItem(T)  hasItem(Matcher<?  super  T>)    :  Matcher<Iterable<?  super  T>>    

hasItems(T...)  hasItems(Matcher<?  super  T>...)     :  Matcher<Iterable<T>>    

emptyIterable()     :  Matcher<Iterable<?  extends  E>>  emptyIterableOf(Class<E>)     :  Matcher<Iterable<E>>    

contains(E...)  contains(Matcher<?  super  E>...)  contains(Matcher<?  super  E>)  contains(List<Matcher<?  super  E>>)       :  Matcher<Iterable<?  extends  E>>    

containsInAnyOrder(T...)  containsInAnyOrder(Collection<Matcher<?  super  T>>)  containsInAnyOrder(Matcher<?  super  T>...)  containsInAnyOrder(Matcher<?  super  E>)       :  Matcher<Iterable<?  extends  E>>    

iterableWithSize(Matcher<?  super  Integer>)  iterableWithSize(int)     :  Matcher<Iterable<E>>  

Collections  hasSize(int)  hasSize(Matcher<?  super  Integer>)       :  Matcher<Collection<?  extends  E>>    

empty()     :  Matcher<Collection<?  extends  E>>  emptyCollectionOf(Class<E>)     :  Matcher<Collection<E>>  

Arrays  array(Matcher<?  super  T>...)     :  Matcher<T[]>    

hasItemInArray(T)  hasItemInArray(Matcher<?  super  T>)     :  Matcher<T[]>    

arrayContaining(E...)  arrayContaining(List<Matcher<?  super  E>>)  arrayContaining(Matcher<?  super  E>...)     :  Matcher<E[]>    

arrayContainingInAnyOrder(E...)  arrayContainingInAnyOrder(Matcher<?  super  E>...)  arrayContainingInAnyOrder(Collection<Matcher<?  super  E>>)       :  Matcher<E[]>    

arrayWithSize(int)  arrayWithSize(Matcher<?  super  Integer>)  emptyArray()     :  Matcher<E[]>  

Maps  hasEntry(K,  V)  hasEntry(Matcher<?  super  K>,  Matcher<?  super  V>)       :  Matcher<Map<?  extends  K,  ?  extends  V>>    

hasKey(K)  hasKey(Matcher<?  super  K>)   :  Matcher<Map<?  extends  K,  ?>>    

hasValue(V)  hasValue(Matcher<?  super  V>)  :  Matcher<Map<?,  ?  extends  V>>  

Beans  hasProperty(String)  hasProperty(String,  Matcher<?>)  samePropertyValuesAs(T)   :  Matcher<T>  

Comparables  comparesEqualTo(T  extends  Comparable<T>)  greaterThan(T  extends  Comparable<T>)  greaterThanOrEqualTo(T  extends  Comparable<T>)  lessThan(T  extends  Comparable<T>)  lessThanOrEqualTo(T  extends  Comparable<T>)     :  Matcher<T>  

Numbers  closeTo(double,  double)     :  Matcher<Double>  closeTo(BigDecimal,  BigDecimal)     :  Matcher<BigDecimal>  

Classes  typeCompatibleWith(Class<T>)   :  Matcher<java.lang.Class<?>>  

EventObjects  eventFrom(Object)  eventFrom(Class<?  extends  EventObject>,  Object)       :  Matcher<EventObject>  

DOM  hasXPath(String)  hasXPath(String,  NamespaceContext)  hasXPath(String,  Matcher<String>)  hasXPath(String,  NamespaceContext,  Matcher<String>)       :  Matcher<org.w3c.dom.Node>    Created  by  Marc  Philipp,  http://www.marcphilipp.de  This  work  is  licensed  under  a  Creative  Commons  Attribution-­‐ShareAlike  3.0  Unported  License,  http://creativecommons.org/licenses/by-­‐sa/3.0/  

Core   Library  

http://www.marcphilipp.de/blog/2013/01/02/hamcrest-quick-reference/

Page 15: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Wie verwende ich Hamcrest Matchers?

I Ab Version 4.11 wird JUnit ohne Matcher ausgeliefertI Um sie zu benutzen, muss Hamcrest als zusätzliche Abhängigkeithinzugefügt werden.

I hamcrest-core.jar und hamcrest-library.jar(oder hamcrest-all.jar)I org.hamcrest.Matchers enthält alle vordefinierten Matcher.

I Darüber hinaus lassen sich eigene Matcher definieren.

VKSI Sneak Preview, 18. September 2013 15

Page 16: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Ein eigener MatcherImplementierung

public class IsEmptyCollection extends TypeSafeMatcher<Collection<?>> {

@Override public void describeTo(Description description) {description.appendText("empty collection");

}

@Override protected boolean matchesSafely(Collection<?> collection) {return collection.isEmpty();

}

@Override public void describeMismatchSafely(Collection<?> collection,Description description) {

description.appendText("size was " + collection.size());}

@Factory public static Matcher<Collection<?>> empty() {return new IsEmptyCollection();

}}

VKSI Sneak Preview, 18. September 2013 16

Page 17: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Ein eigener MatcherBenutzung

@Test public void isEmpty() {Set<String> set = new TreeSet<String>();set.add("a");

assertThat(set, new IsEmptyCollection()); // Direkte InstantiierungassertThat(set, IsEmptyCollection.empty()); // Mit @Factory-MethodeassertThat(set, empty()); // Per Static ImportassertThat(set, is(empty())); // Syntactic Sugar

}

Result:Expected: empty collection

but: size was 1

VKSI Sneak Preview, 18. September 2013 17

Page 18: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Feature MatchersImplementierung

public class CollectionSizeMatcher extends FeatureMatcher<Collection<?>, Integer> {

public CollectionSizeMatcher(Matcher<? super Integer> subMatcher) {super(subMatcher, "collection with size", "size");

}

@Override protected Integer featureValueOf(Collection<?> actual) {return actual.size();

}

@Factory public static Matcher<Collection<?>> hasSize(Matcher<? super Integer>matcher) {

return new CollectionSizeMatcher(matcher);}

}

VKSI Sneak Preview, 18. September 2013 18

Page 19: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Feature MatchersBenutzung

@Test public void size() {assertThat(Arrays.asList("a"), hasSize(equalTo(2)));

}

Result:Expected: collection with size <2>

but: size was <1>

VKSI Sneak Preview, 18. September 2013 19

Page 20: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Fazit: MatchersI Assertions lassen sich oft eleganter formulierenI Manchmal sind die alten Assertion-Methoden klarer

ProblemJavas Typsystem macht einem ab und zu einen Strich durch dieRechnungI Boxing notwendig bei primitiven Typen

assertThat( 1 + 1, is( 2 ) )

I Mangelnde TypinferenzassertThat( list, Matchers.<Integer> iterableWithSize( lessThan( 10 ) ) );

VKSI Sneak Preview, 18. September 2013 20

Page 21: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Rules

René Magritte, Golconda 21

Page 22: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Was ist eine Rule?

Erweiterungsmechanismus für Ablauf der Testmethoden

VKSI Sneak Preview, 18. September 2013 22

Page 23: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Ablaufreihenfolge von JUnit-TestsBisher

Test-­‐Run  

@A,erClass  [0..*]  

@BeforeClass  [0..*]  

@A,er  [0..*]  

@Before  [0..*]  

@Test  

Test-­‐Run  1  

Test-­‐Run  2  

Klassenebene

Methodenebene

VKSI Sneak Preview, 18. September 2013 23

Page 24: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Ablaufreihenfolge von JUnit-TestsMit Rules

Test-­‐Run  

@ClassRule  [0..*]                

 @ClassRule  [0..*]  

@A4erClass  [0..*]  

@BeforeClass  [0..*]  

@Rule  [0..*]                @Rule  [0..*]  

@A4er  [0..*]  

@Before  [0..*]  

@Test  

Test-­‐Run  1  

Test-­‐Run  2  

Klassenebene

Methodenebene

VKSI Sneak Preview, 18. September 2013 24

Page 25: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Beispiele für Rules

I Ausführung eigenen Codes vor bzw. nach jeder TestmethodeI Behandlung fehlgeschlagener TestsI Überprüfung zusätzlicher Kriterien nach einem TestsI . . .

VKSI Sneak Preview, 18. September 2013 25

Page 26: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Beispiel: TemporaryFolderOhne Benutzung einer Rule

public class TemporaryFolderWithoutRule {private File folder;

@Before public void createTemporaryFolder() throws Exception {folder = File.createTempFile("myFolder", "");folder.delete();folder.mkdir();

}

@Test public void test() throws Exception {File file = new File(folder, "test.txt");file.createNewFile();assertTrue(file.exists());

}

@After public void deleteTemporaryFolder() {recursivelyDelete(folder); // does not fit on current slide...

}

VKSI Sneak Preview, 18. September 2013 26

Page 27: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Beispiel: TemporaryFolderUnter Verwendung einer Rule

public class TemporaryFolderWithRule {

@Rule public TemporaryFolder folder = new TemporaryFolder();

@Test public void test() throws Exception {File file = folder.newFile("test.txt");assertTrue(file.exists());

}

}

VKSI Sneak Preview, 18. September 2013 27

Page 28: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Beispiele für Rules

I Ausführung eigenen Codes vor bzw. nach jeder TestmethodeI Behandlung fehlgeschlagener TestsI Überprüfung zusätzlicher Kriterien nach einem TestsI . . .

VKSI Sneak Preview, 18. September 2013 28

Page 29: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Beispiel: ExpectedExceptionOhne Benutzung einer Rule

public class ExpectedExceptionWithoutRule {

int[] threeNumbers = { 1, 2, 3 };

@Test(expected = ArrayIndexOutOfBoundsException.class)public void exception() {

threeNumbers[3] = 4;}

@Test public void exceptionWithMessage() {try {

threeNumbers[3] = 4;fail("ArrayIndexOutOfBoundsException expected");

} catch (ArrayIndexOutOfBoundsException expected) {assertEquals("3", expected.getMessage());

}}

}

VKSI Sneak Preview, 18. September 2013 29

Page 30: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Beispiel: ExpectedExceptionUnter Verwendung einer Rule

public class ExpectedExceptionWithRule {

int[] threeNumbers = { 1, 2, 3 };

@Rule public ExpectedException thrown = ExpectedException.none();

@Test public void exception() {thrown.expect(ArrayIndexOutOfBoundsException.class);threeNumbers[3] = 4;

}

@Test public void exceptionWithMessage() {thrown.expect(ArrayIndexOutOfBoundsException.class);thrown.expectMessage("3");threeNumbers[3] = 4;

}}

VKSI Sneak Preview, 18. September 2013 30

Page 31: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Weitere vordefinierte Rules

ErrorCollectorSammelt fehlgeschlagene Assertions innerhalb einer Testmethode undgibt am Ende eine Liste der Fehlschläge aus.TestNameMerkt sich Namen der aktuell ausgeführten Testmethode und stellt ihnauf Anfrage zur Verfügung.TimeoutWendet gleichen Timeout auf alle Testmethoden einer Klasse an.

VKSI Sneak Preview, 18. September 2013 31

Page 32: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Rules auf Klassenebene

I Alle Rules, die TestRule implementieren, können auch aufKlassenebene verwendet werden.I Die Regel wird dann einmal pro Testklasse ausgeführt.

Aus@Rule public TemporaryFolder folder = new TemporaryFolder();

wird@ClassRule public static TemporaryFolder folder = new TemporaryFolder();

VKSI Sneak Preview, 18. September 2013 32

Page 33: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Schreib deine eigenen Regeln!

VKSI Sneak Preview, 18. September 2013 33

Page 34: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Eine eigene RegelImplementierung

public class SystemProperty extends ExternalResource {private final String key, value;private String oldValue;public SystemProperty(String key, String value) {

this.key = key;this.value = value;

}@Override protected void before() {

oldValue = System.getProperty(key);System.setProperty(key, value);

}@Override protected void after() {

if (oldValue == null) {System.getProperties().remove(key);

} else {System.setProperty(key, oldValue);

}}

}

VKSI Sneak Preview, 18. September 2013 34

Page 35: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Eine eigene RegelBenutzung

public class SomeTestUsingSystemProperty {

private static final String VALUE = "someValue";private static final String KEY = "someKey";

@Rule public SystemProperty systemProperty = new SystemProperty(KEY, VALUE);

@Test public void test() {assertThat(System.getProperty(KEY), is(VALUE));

}}

VKSI Sneak Preview, 18. September 2013 35

Page 36: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Reihenfolge von Rules

Die Ausführungsreihenfolge von Rules ist absichtlich undefiniert.Falls notwendig, lässt sich per RuleChain eine Reihenfolge festlegen:

@Rule public RuleChain chain = RuleChain.outerRule(new LoggingRule("outer rule")).around(new LoggingRule("middle rule")).around(new LoggingRule("inner rule"));

VKSI Sneak Preview, 18. September 2013 36

Page 37: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Vorteile von Regeln

WiederverwendbarkeitErmöglichen häufig benötigten Code auszulagern.KombinierbarkeitBeliebig viele Regeln in einem Test verwendbarDelegation statt VererbungHelfen Testklassenhierarchien zu vermeiden!ErweiterbarkeitEigene Regeln schreiben ist einfach.

VKSI Sneak Preview, 18. September 2013 37

Page 38: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Ausblick

Quint Buchholz, Mann auf einer Leiter 38

Page 39: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Was jetzt?

I Aktualisierung auf neue Version ist einfachI Alte Tests funktionieren weiterhinI Neue Tests profitieren von neuen FeaturesI Alte Tests können nach und nach vereinfacht werdenI Ausprobieren!

VKSI Sneak Preview, 18. September 2013 39

Page 40: JUnit 4.11 – Die Neuerungen · 2014. 8. 13. · ÜberJUnit KentBeck: Aprogrammer-orientedtesting frameworkforJava. DavidSaff: JUnitistheintersectionofall possibleusefulJavatest

Ausprobieren!http://www.junit.org/

http://hamcrest.org/JavaHamcrest/

Mail [email protected] @marcphilipp

Blog http://www.marcphilipp.de/

VKSI Sneak Preview, 18. September 2013 40