assertEquals("Must contain 2 ponies", 2, ponies.size());
Unit toolkit
Junit 4
Springboot test
Mockito
Independent and isolated
Repeatable
Fast
Descriptive
Independent and isolated
Repeatable
Fast
Descriptive
Which tests covers deleteAll function?
What is the error?
assertEquals("Must contain 2 ponies", 2, ponies.size());
.
.
.
assertEquals("Must contain 2 ponies", 2, ponies.size());
java.lang.AssertionError: Must contain 2 ponies
Expected :2
Actual :1
Moving to junit 5
testCompile("junit:junit:4.12")
testCompile("org.junit.jupiter:junit-jupiter-api:5.4.2")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.4.2")
testRuntime("org.junit.vintage:junit-vintage-engine:5.4.2")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.4.2")
import org.junit.Test;
.
import org.junit.Test;
import org.junit.jupiter.api.Test;
@Rule @RunWith @ClassRule
@Rule @RunWith @ClassRule
@ExtendWith
@Test(expected = MyException.class)
// MyTest
MyException e = assertThrows(
MyException.class,
() -> service.save(ponyDto));
@Ignore
@Disabled
@DisabledOnOs(OS.WINDOWS)
@DisabledOnJre(JRE.JAVA_8)
Class with hundred of tests
class PonyControllerTest {
@Test
void withoutFilters() throws Exception { ... }
@Test
void byType() throws Exception { ... }
@Test
void create() throws Exception { ... }
...
}
class PonyControllerTest {
@Nested
class ListAll {
@Test
void withoutFilters() throws Exception { ... }
@Test
void byType() throws Exception { ... }
}
@Nested
class Create { ... }
}
// default @TestInstance(Lifecycle.PER_METHOD)
class PonyServiceTest {
private SlowToCreate service = new SlowToCreate();
}
@TestInstance(Lifecycle.PER_CLASS)
class PonyServiceTest {
private SlowToCreate service = new SlowToCreate();
}
// junit-platform.properties
junit.jupiter.testinstance.lifecycle.default = per_class
Understand the error
assertEquals(2, myList.size());
assertEquals("Rainbow Dash", myList.get(0).getName());
assertEquals("Pinkie Pie", myList.get(1).getName());
org.opentest4j.AssertionFailedError:
Expected :2
Actual :1
Understand the error
assertEquals(2, myList.size());
assertEquals("Rainbow Dash", myList.get(0).getName());
assertEquals("Pinkie Pie", myList.get(1).getName());
org.opentest4j.AssertionFailedError:
Expected :2
Actual :1
Understand the error
assertEquals(2, myList.size());
assertEquals("Rainbow Dash", myList.get(0).getName());
assertEquals("Pinkie Pie", myList.get(1).getName());
org.opentest4j.AssertionFailedError:
Expected :Rainbow Dash
Actual :Pinkie Pie
Fluent assertions for java
assertThat(myList)
.contains(pinkiePie, rainbowDash);
.
.
assertThat(myList)
.contains(pinkiePie, rainbowDash);
assertThat(myList)
.containsExactlyInAnyOrder(pinkiePie, rainbowDash);
java.lang.AssertionError:
Expecting:
<[PonyDto{id='null', name='Pinkie Pie', type=null},
PonyDto{id='null', name='Big McIntosh', type=null}]>
at bzh.MyTest.test(MyTest.java:25)
java.lang.AssertionError:
Expecting:
<[PonyDto{id='null', name='Pinkie Pie', type=null},
PonyDto{id='null', name='Big McIntosh', type=null}]>
to contain exactly in any order:
<[PonyDto{id='null', name='Pinkie Pie', type=null},
PonyDto{id='null', name='Rainbow Dash', type=null}]>
at bzh.MyTest.test(MyTest.java:25)
java.lang.AssertionError:
Expecting:
<[PonyDto{id='null', name='Pinkie Pie', type=null},
PonyDto{id='null', name='Big McIntosh', type=null}]>
to contain exactly in any order:
<[PonyDto{id='null', name='Pinkie Pie', type=null},
PonyDto{id='null', name='Rainbow Dash', type=null}]>
elements not found:
<[PonyDto{id='null', name='Rainbow Dash', type=null}]>
at bzh.MyTest.test(MyTest.java:25)
java.lang.AssertionError:
Expecting:
<[PonyDto{id='null', name='Pinkie Pie', type=null},
PonyDto{id='null', name='Big McIntosh', type=null}]>
to contain exactly in any order:
<[PonyDto{id='null', name='Pinkie Pie', type=null},
PonyDto{id='null', name='Rainbow Dash', type=null}]>
elements not found:
<[PonyDto{id='null', name='Rainbow Dash', type=null}]>
and elements not expected:
<[PonyDto{id='null', name='Big McIntosh', type=null}]>
at bzh.MyTest.test(MyTest.java:25)
.
.
.
.
assertThat(fromDB)
.isEqualToIgnoringGivenFields(expected, "id");
.
.
assertThat(fromDB)
.isEqualToIgnoringGivenFields(expected, "id");
assertThat(fromDB)
.isEqualToComparingFieldByField(expected);
assertThat(fellowshipOfTheRing).hasSize(9)
.contains(frodo, sam)
.doesNotContain(sauron);
Multiple input/output
.
.
.
.
.
.
Multiple input/output
@Test
public void hasWings() {
assertTrue(service.hasWings(Pegasi));
}
Multiple input/output
@Test
public void hasWings() {
assertTrue(service.hasWings(Pegasi));
assertFalse(service.hasWings(Unicorns));
}
Multiple input/output
@Test
public void hasWings() {
assertTrue(service.hasWings(Pegasi));
assertFalse(service.hasWings(Unicorns));
assertFalse(service.hasWings(Earth));
}
@ParameterizedTest
@EnumSource(FlyingType.class)
void hasWings(FlyingType type) {
assertTrue(service.hasWings(type))
}
@ParameterizedTest
@MethodSource("hasWingsSource")
void hasWings(HasWingsData data) {
assertThat(service.hasWings(data.type)).isEqualTo(data.expected);
}
.
.
.
.
.
.
.
@ParameterizedTest
@MethodSource("hasWingsSource")
void hasWings(HasWingsData data) {
assertThat(service.hasWings(data.type)).isEqualTo(data.expected);
}
private Stream<HasWingsData> hasWingsSource() {
return Stream.of(
new HasWingsData(Pegasi, true),
new HasWingsData(Unicorns, false),
new HasWingsData(Earth, false)
);
}
myTestWithANameThatSayWhatItDoes()
@DisplayName("Map pony list to ponyDto list")
void mapPonyListToPonyDtoList() { }
@Nested
@DisplayName("List all")
class ListAll { }
Use kotlin
Method name is the new DisplayName
@Test @DisplayName("Map pony list to ponyDto list")
void mapPonyListToPonyDtoList { }
Method name is the new DisplayName
@Test
fun `Map pony list to ponyDto list`() { }
Pony pinkie = new Pony("Pinkie Pie", Earth)
Pony bigM = new Pony("Big McIntosh", Earth)
fun newPony(name: String = "Pinkie Pie",
type: PonyType = Earth) {
return Pony(name, type)
}
.
.
.
.
fun newPony(name: String = "Pinkie Pie",
type: PonyType = Earth) {
return Pony(name, type)
}
fun myTest() {
val pinkie = newPony()
}
fun newPony(name: String = "Pinkie Pie",
type: PonyType = Earth) {
return Pony(name, type)
}
fun myTest() {
val pinkie = newPony()
val bigM = newPony(name = "Big McIntosh")
}
fun newPony(name: String = "Pinkie Pie",
type: PonyType = Earth) {
return Pony(name, type)
}
fun myTest() {
val pinkie = newPony()
val bigM = newPony(name = "Big McIntosh")
val flying = newPony(type = Pegasi)
}
class HasWingsData {
public PonyType type;
public boolean expected;
public HasWingsData(PonyType type, boolean expected) {
this.type = type;
this.expected = expected;
}
@Override
public String toString() {
return "HasWingsData{" +
"type=" + type +
", expected=" + expected +
'}';
}
}
data class HasWingsData(val pony:Pony, val expected:Boolean)
private fun hasWingsSource() = Stream.of(
HasWingsData(newPony(type=Earth), false),
HasWingsData(newPony(type=Pegasi), true)
)
Package private
Tag
Mockk
Assertk
Mutation testing
Show love with your tests!
Organize them
Make them talk friendly
Find your preferred toolbox
Source code and slide can be found on https://github.com/Zomzog/unitEvolution