Created
December 16, 2020 14:20
-
-
Save bmaggi/08fc79eddca205f28cf77c2432dd651a to your computer and use it in GitHub Desktop.
Junit 5 Custom Parameter Source with Boolean as Example
This file contains hidden or 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.extension.ExtensionContext; | |
import org.junit.jupiter.params.provider.Arguments; | |
import org.junit.jupiter.params.provider.ArgumentsProvider; | |
import org.junit.jupiter.params.support.AnnotationConsumer; | |
import java.util.stream.Stream; | |
public class BooleanArgumentsProvider implements ArgumentsProvider, AnnotationConsumer<BooleanSource> { | |
BooleanArgumentsProvider() { | |
} | |
public Stream<Arguments> provideArguments(ExtensionContext context) { | |
return Stream.of(Arguments.arguments(Boolean.TRUE),Arguments.arguments(Boolean.FALSE)); | |
} | |
@Override | |
public void accept(BooleanSource booleanSource) { | |
} | |
} |
This file contains hidden or 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.params.provider.ArgumentsSource; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD}) | |
@Retention(RetentionPolicy.RUNTIME) | |
@ArgumentsSource(BooleanArgumentsProvider.class) | |
public @interface BooleanSource { | |
} |
This file contains hidden or 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.params.ParameterizedTest; | |
import org.junit.jupiter.params.provider.NullSource; | |
import static org.junit.jupiter.api.Assertions.assertTrue; | |
class ExampleTest { | |
@ParameterizedTest | |
@BooleanSource | |
@NullSource | |
void myTestFail( Boolean bool) { | |
assertTrue(true || bool); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment