Skip to content

Instantly share code, notes, and snippets.

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) {
@IntegrationTest
class MyIntegrationTests {
// ...
}
@ExtendWith({
MyIntegrationTestExtension.class,
SpringExtension.class // Implémenté par Spring
})
@Retention(RetentionPolicy.RUNTIME)
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.*;
@ExtendWith(MyIntegrationTestExtension.class)
class MyIntegrationTests {
@Test
void test() {
// ...
}
junit.jupiter.displayname.generator.default = \
org.junit.jupiter.api.DisplayNameGenerator$ReplaceUnderscores
@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) {}
import org.junit.jupiter.api.DisplayName;
@DisplayName("BookmarkRepository")
class BookmarkRepositoryTest {
@Test
@DisplayName("Should save a bookmark")
void should_save_bookmark(Bookmark bookmark) {}
@Nested
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");
public interface EqualityTest<T> {
T createValue();
T createOtherValue();
@Test
default void should_be_equal() {
assertThat(createValue()).isEqualTo(createValue());
assertThat(createValue().hashCode()).isEqualTo(createValue().hashCode());
}
@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)
@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