refactoring junit tests - nordic testing days · refactoring junit tests franziska sauerwein...
TRANSCRIPT
![Page 1: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/1.jpg)
Refactoring JUnit TestsFranziska Sauerwein @Singsalad
1
![Page 2: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/2.jpg)
2
![Page 3: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/3.jpg)
Setup (find a pair!)
install IDE (USB)import/download projectrun ConferenceTest
https://github.com/franziskas/RefactoringJUnitTests
3
![Page 4: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/4.jpg)
The problem: Tests that are
complex to readhard to maintaintake a long time to run
-> Tests lose their value4
![Page 5: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/5.jpg)
Some reasons for this:
complex object modelsbad code designuse of external resources
5
![Page 6: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/6.jpg)
What we can do (short term)
simplify setup & assertionsclean test codeseparate dependencies
6
![Page 7: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/7.jpg)
Builder
hides irrelevant dataallows easy changes to testdefaults (e.g. new validation)simplifies creation & enablereuse
7
![Page 8: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/8.jpg)
Matcher
increases test readabilityimproves test failure messageis combinable & reusable
8
![Page 9: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/9.jpg)
Combining Matchers
allOf(..)
anyOf(..)
not(..)9
![Page 10: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/10.jpg)
Object Matchers
equalTo(..) / sameInstance(..)
instanceOf(..) / isCompatibleType(..)
notNullValue(..) / nullValue(..)
10
![Page 11: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/11.jpg)
Collection Matchers
array(..) / hasItemInArray(..)
hasEntry(..) / hasKey(..) / hasValue(..)
hasItem(..), hasItems(..)
11
![Page 12: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/12.jpg)
Number Matchers
closeTo(..)
greaterThan(..) / greaterThanOrEqualTo(..)
lessThan(..) / lessThanOrEqualTo(..)
12
![Page 13: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/13.jpg)
String Matchers
equalToIgnoringCase(..)
equalToIgnoringWhiteSpace(..)
containsString(..)
endsWith(..) / startsWith(..)13
![Page 14: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/14.jpg)
Custom Matchersprivate Matcher<User> hasEmail() { return new TypeSafeDiagnosingMatcher<Foo>() {
@Override public void describeTo(final Description description) { description.appendText("expected user to have an email"); } @Override protected boolean matchesSafely(final User user, final Description mismatchDescription) { mismatchDescription .appendText(" was ") .appendValue(user.getEmail();
return user.getEmail()!= null && user.getEmail().length() > 3;} };}
14
![Page 15: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/15.jpg)
Custom Matchersprivate Matcher<User> hasEmail() { return new TypeSafeDiagnosingMatcher<Foo>() {
@Override public void describeTo(final Description description) { description.appendText("expected user to have an email"); } @Override protected boolean matchesSafely(final User user, final Description mismatchDescription) { mismatchDescription .appendText(" was ") .appendValue(user.getEmail();
return user.getEmail()!= null && user.getEmail().length() > 3;} };}
15
![Page 16: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/16.jpg)
Custom Matchersprivate Matcher<User> hasEmail() { return new TypeSafeDiagnosingMatcher<Foo>() {
@Override public void describeTo(final Description description) { description.appendText("expected user to have an email"); } @Override protected boolean matchesSafely(final User user, final Description mismatchDescription) { mismatchDescription .appendText(" was ") .appendValue(user.getEmail();
return user.getEmail()!= null && user.getEmail().length() > 3;} };}
16
![Page 17: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/17.jpg)
Custom Matchersprivate Matcher<User> hasEmail() { return new TypeSafeDiagnosingMatcher<Foo>() {
@Override public void describeTo(final Description description) { description.appendText("expected user to have an email"); } @Override protected boolean matchesSafely(final User user, final Description mismatchDescription) { mismatchDescription .appendText(" was ") .appendValue(user.getEmail();
return user.getEmail()!= null && user.getEmail().length() > 3;} };}
17
![Page 18: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/18.jpg)
www.marcphilipp.de/blog/2013/01/02/hamcrest-quick-reference/
18
![Page 19: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/19.jpg)
Modularisation
one test - one concernunit tests for functionalitylarger tests for journey
19
![Page 20: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/20.jpg)
Summary
fix the code designin the meantime, ease the paintreat test code at least as well asproduction code
20
![Page 21: Refactoring JUnit Tests - Nordic Testing Days · Refactoring JUnit Tests Franziska Sauerwein @Singsalad 1. 2. Setup (find a pair!) ... -> Tests lose their value 4. Some reasons for](https://reader034.vdocuments.mx/reader034/viewer/2022052105/604042d5548eba6d885f1a9e/html5/thumbnails/21.jpg)
Try it yourself!
21