View 17-ParameterResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BookmarkResolver implements ParameterResolver { | |
@Override | |
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | |
// Le type d'arguments concernés par le resolver | |
return parameterContext.getParameter().getType() == Bookmark.class; | |
} | |
@Override | |
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { |
View 16-CustomAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IntegrationTest | |
class MyIntegrationTests { | |
// ... | |
} | |
@ExtendWith({ | |
MyIntegrationTestExtension.class, | |
SpringExtension.class // Implémenté par Spring | |
}) | |
@Retention(RetentionPolicy.RUNTIME) |
View 15-ExtendWith.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.jupiter.api.Test; | |
import org.junit.jupiter.api.extension.*; | |
@ExtendWith(MyIntegrationTestExtension.class) | |
class MyIntegrationTests { | |
@Test | |
void test() { | |
// ... | |
} |
View junit-platform.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
junit.jupiter.displayname.generator.default = \ | |
org.junit.jupiter.api.DisplayNameGenerator$ReplaceUnderscores |
View 13-ParameterizedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@CsvSource({ | |
"http://www.junit.org, JUnit, Testing", | |
"https://www.oracle.com/fr/java/, Java, Language" | |
}) | |
@DisplayName("Should create the bookmark") | |
@ParameterizedTest(name = "for the url {0} named {1} & tagged {2}") | |
void should_create_the_bookmark(String url, String name, String tag) {} |
View 12-DisplayName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.jupiter.api.DisplayName; | |
@DisplayName("BookmarkRepository") | |
class BookmarkRepositoryTest { | |
@Test | |
@DisplayName("Should save a bookmark") | |
void should_save_bookmark(Bookmark bookmark) {} | |
@Nested |
View 11-BookmarkTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BookmarkTest implements EqualityTest<Bookmark> { | |
@Override | |
public Bookmark createValue() { | |
return Bookmark.create("http://www.test.com", "name"); | |
} | |
@Override | |
public Bookmark createOtherValue() { | |
return Bookmark.create("http://www.test2.com", "other name"); |
View 10-InterfaceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface EqualityTest<T> { | |
T createValue(); | |
T createOtherValue(); | |
@Test | |
default void should_be_equal() { | |
assertThat(createValue()).isEqualTo(createValue()); | |
assertThat(createValue().hashCode()).isEqualTo(createValue().hashCode()); | |
} |
View 9-MethodSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ParameterizedTest | |
@MethodSource("somePeople") | |
void a_test_with_method_source(String name, int age, Month birthMonth) { | |
// ... | |
} | |
private static Stream<Arguments> somePeople() { | |
return Stream.of( | |
Arguments.of("Alice", 25, JUNE), | |
Arguments.of("Bob", 32, AUGUST) |
View 8-CsvSourceWithExplicitConverters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ParameterizedTest | |
@ValueSource(strings = {"JANUARY", "FEBRUARY", "MARCH"}) | |
void a_test_with_explicit_conversion( | |
@ConvertWith(MonthToNumberConverter.class) int month) { | |
assertTrue(month <= 3); | |
} | |
class MonthToNumberConverter extends SimpleArgumentConverter { | |
@Override |
NewerOlder