Java 类org.junit.jupiter.params.provider.ArgumentsSource 实例源码

项目:autotest    文件:AutoTestExtension.java   
@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
    Method templateMethod = Preconditions.notNull(context.getTestMethod().orElse(null),
            "test method must not be null");
    AutoTestNameFormatter formatter = createNameFormatter(templateMethod);
    AtomicLong invocationCount = new AtomicLong(0L);
    return (Stream) findRepeatableAnnotations(templateMethod, ArgumentsSource.class)
            .stream()
            .map(ArgumentsSource::value)
            .map(ReflectionUtils::newInstance)
            .map(provider -> AnnotationConsumerInitializer.initialize(templateMethod, provider))
            .flatMap(provider -> arguments(provider, context))
            .map(Arguments::get)
            .map((arguments) -> {
                return new AutoTestInvocationContext(formatter, arguments);
            })
            .peek((invocationContext) -> {
                invocationCount.incrementAndGet();
            }).onClose(() -> {
                Preconditions.condition(invocationCount.get() > 0L, () -> {
                    return "当使用注解 @" + AutoTest.class.getSimpleName() + " 的时候,测试方法需要至少一个参数";
                });
            });

}
项目:patience    文件:PatientWaitFutureTest.java   
@ParameterizedTest
@DisplayName("it returns successfully with valid arguments")
@ArgumentsSource(ValidArgumentsProvider.class)
void testValidArgumentsSucceed(Sleep sleep,
                               Duration initialDelay,
                               Duration defaultTimeout,
                               PatientExecutionHandler executionHandler,
                               DelaySupplierFactory delaySupplierFactory,
                               PatientExecutable<Boolean> executable,
                               Predicate<Boolean> filter,
                               Supplier<String> messageSupplier) {
    try {
        Assertions.assertNotNull(getInstance(sleep,
                                             initialDelay,
                                             defaultTimeout,
                                             executionHandler,
                                             delaySupplierFactory,
                                             executable,
                                             filter,
                                             messageSupplier),
                                 "Should have returned a non-null instance for valid arguments.");
    } catch (Throwable thrown) {
        Assertions.fail("Expected to construct a PatientWaitFuture with valid arguments but throwable was caught: " + thrown);
    }
}
项目:patience    文件:PatientWaitFutureTest.java   
@ParameterizedTest
@DisplayName("it returns successfully with valid arguments and a null message")
@ArgumentsSource(ValidArgumentsProvider.class)
void testSucceedsWithNullMessage(Sleep sleep,
                                 Duration initialDelay,
                                 Duration defaultTimeout,
                                 PatientExecutionHandler executionHandler,
                                 DelaySupplierFactory delaySupplierFactory,
                                 PatientExecutable<Boolean> executable,
                                 Predicate<Boolean> filter) {
    try {
        Assertions.assertNotNull(getInstance(sleep,
                                             initialDelay,
                                             defaultTimeout,
                                             executionHandler,
                                             delaySupplierFactory,
                                             executable,
                                             filter,
                                             (String) null),
                                 "Should have returned a non-null instance for valid arguments.");
    } catch (Throwable thrown) {
        Assertions.fail("Expected to construct a PatientWaitFuture with valid arguments but throwable was caught: " + thrown);
    }
}
项目:patience    文件:PatientWaitFutureTest.java   
@ParameterizedTest
@DisplayName("it throws an exception for invalid arguments")
@ArgumentsSource(InvalidArgumentsProvider.class)
void testInvalidArgumentsThrowsException(Sleep sleep,
                                         Duration initialDelay,
                                         Duration defaultTimeout,
                                         PatientExecutionHandler executionHandler,
                                         DelaySupplierFactory delaySupplierFactory,
                                         PatientExecutable<Boolean> executable,
                                         Predicate<Boolean> filter,
                                         Supplier<String> messageSupplier) {
    Assertions.assertThrows(IllegalArgumentException.class,
                            () -> getInstance(sleep,
                                              initialDelay,
                                              defaultTimeout,
                                              executionHandler,
                                              delaySupplierFactory,
                                              executable,
                                              filter,
                                              messageSupplier),
                            "Should have thrown an exception for an invalid argument");
}
项目:patience    文件:PatientRetryFutureTest.java   
@ParameterizedTest
@DisplayName("it returns successfully with valid arguments")
@ArgumentsSource(PatientRetryFutureTest.ValidArgumentsProvider.class)
void testValidArgumentsSucceed(Sleep sleep,
                               Duration initialDelay,
                               int defaultNumberOfRetries,
                               PatientExecutionHandler executionHandler,
                               DelaySupplierFactory delaySupplierFactory,
                               PatientExecutable<Boolean> executable,
                               Predicate<Boolean> filter,
                               Supplier<String> messageSupplier) {
    try {
        Assertions.assertNotNull(getInstance(sleep,
                                             initialDelay,
                                             defaultNumberOfRetries,
                                             executionHandler,
                                             delaySupplierFactory,
                                             executable,
                                             filter,
                                             messageSupplier),
                                 "Should have returned a non-null instance for valid arguments.");
    } catch (Throwable thrown) {
        Assertions.fail("Expected to construct a PatientRetryFuture with valid arguments but throwable was caught: " + thrown);
    }
}
项目:patience    文件:PatientRetryFutureTest.java   
@ParameterizedTest
@DisplayName("it returns successfully with valid arguments and a null message")
@ArgumentsSource(PatientRetryFutureTest.ValidArgumentsProvider.class)
void testSucceedsWithNullMessage(Sleep sleep,
                                 Duration initialDelay,
                                 int defaultNumberOfRetries,
                                 PatientExecutionHandler executionHandler,
                                 DelaySupplierFactory delaySupplierFactory,
                                 PatientExecutable<Boolean> executable,
                                 Predicate<Boolean> filter) {
    try {
        Assertions.assertNotNull(getInstance(sleep,
                                             initialDelay,
                                             defaultNumberOfRetries,
                                             executionHandler,
                                             delaySupplierFactory,
                                             executable,
                                             filter,
                                             (String) null),
                                 "Should have returned a non-null instance for valid arguments.");
    } catch (Throwable thrown) {
        Assertions.fail("Expected to construct a PatientRetryFuture with valid arguments but throwable was caught: " + thrown);
    }
}
项目:patience    文件:PatientRetryFutureTest.java   
@ParameterizedTest
@DisplayName("it throws an exception for invalid arguments")
@ArgumentsSource(PatientRetryFutureTest.InvalidArgumentsProvider.class)
void testInvalidArgumentsThrowsException(Sleep sleep,
                                         Duration initialDelay,
                                         int defaultNumberOfRetries,
                                         PatientExecutionHandler executionHandler,
                                         DelaySupplierFactory delaySupplierFactory,
                                         PatientExecutable<Boolean> executable,
                                         Predicate<Boolean> filter,
                                         Supplier<String> messageSupplier) {
    Assertions.assertThrows(IllegalArgumentException.class,
                            () -> getInstance(sleep,
                                              initialDelay,
                                              defaultNumberOfRetries,
                                              executionHandler,
                                              delaySupplierFactory,
                                              executable,
                                              filter,
                                              messageSupplier),
                            "Should have thrown an exception for an invalid argument");
}
项目:mastering-junit5    文件:ArgumentSourcesParameterizedTest.java   
@ParameterizedTest
@ArgumentsSources({ @ArgumentsSource(CustomArgumentsProvider1.class),
        @ArgumentsSource(CustomArgumentsProvider2.class) })
void testWithArgumentsSource(String first, int second) {
    System.out.println("Parameterized test with (String) " + first
            + " and (int) " + second);

    assertNotNull(first);
    assertTrue(second > 0);
}
项目:mastering-junit5    文件:ArgumentSourceParameterizedTest.java   
@ParameterizedTest
@ArgumentsSource(CustomArgumentsProvider1.class)
void testWithArgumentsSource(String first, int second) {
    System.out.println("Parameterized test with (String) " + first
            + " and (int) " + second);

    assertNotNull(first);
    assertTrue(second > 0);
}
项目:Mastering-Software-Testing-with-JUnit-5    文件:ArgumentSourcesParameterizedTest.java   
@ParameterizedTest
@ArgumentsSources({ @ArgumentsSource(CustomArgumentsProvider1.class),
        @ArgumentsSource(CustomArgumentsProvider2.class) })
void testWithArgumentsSource(String first, int second) {
    System.out.println("Parameterized test with (String) " + first
            + " and (int) " + second);

    assertNotNull(first);
    assertTrue(second > 0);
}
项目:Mastering-Software-Testing-with-JUnit-5    文件:ArgumentSourceParameterizedTest.java   
@ParameterizedTest
@ArgumentsSource(CustomArgumentsProvider1.class)
void testWithArgumentsSource(String first, int second) {
    System.out.println("Parameterized test with (String) " + first
            + " and (int) " + second);

    assertNotNull(first);
    assertTrue(second > 0);
}
项目:roboslack    文件:MessageRequestTests.java   
@ParameterizedTest
@ArgumentsSource(SerializedMessageRequestsProvider.class)
void testDeserialization(JsonNode json) {
    MoreAssertions.assertSerializable(json,
            MessageRequest.class,
            MessageRequestTests::assertValid);
}
项目:roboslack    文件:AttachmentTests.java   
@ParameterizedTest
@ArgumentsSource(SerializedAttachmentsProvider.class)
void testDeserialization(JsonNode json) {
    MoreAssertions.assertSerializable(json,
            Attachment.class,
            AttachmentTests::assertValid);
}
项目:roboslack    文件:ColorTests.java   
@ParameterizedTest
@ArgumentsSource(SerializedColorsProvider.class)
void testSerialization(JsonNode json) {
    MoreAssertions.assertSerializable(json,
            Color.class,
            ColorTests::assertValid);
}
项目:roboslack    文件:FooterTests.java   
@ParameterizedTest
@ArgumentsSource(SerializedFootersProvider.class)
void testDeserialization(JsonNode json) {
    MoreAssertions.assertSerializable(json,
            Footer.class,
            FooterTests::assertValid);
}
项目:roboslack    文件:AuthorTests.java   
@ParameterizedTest
@ArgumentsSource(SerializedAuthorsProvider.class)
void testSerialization(JsonNode json) {
    MoreAssertions.assertSerializable(json,
            Author.class,
            AuthorTests::assertValid);
}
项目:roboslack    文件:FieldTests.java   
@ParameterizedTest
@ArgumentsSource(SerializedFieldsProvider.class)
void testSerialization(JsonNode json) {
    MoreAssertions.assertSerializable(json,
            Field.class,
            FieldTests::assertValid);
}
项目:roboslack    文件:TitleTests.java   
@ParameterizedTest
@ArgumentsSource(SerializedTitlesProvider.class)
void testDeserialization(JsonNode json) {
    MoreAssertions.assertSerializable(json,
            Title.class,
            TitleTests::assertValid);
}
项目:roboslack    文件:SlackWebHookServiceTests.java   
@ParameterizedTest
@ArgumentsSource(MessageRequestProvider.class)
void testSendMessage(MessageRequest messageRequest, TestInfo testInfo) {
    assertThat(SlackWebHookService.with(assumingEnvironmentWebHookToken())
                    .sendMessage(EnrichTestMessageRequest.get().apply(messageRequest, testInfo)),
            is(equalTo(ResponseCode.OK)));
}
项目:patience    文件:PatientWaitTest.java   
@ParameterizedTest
@DisplayName("it returns a non-null instance successfully for valid arguments")
@ArgumentsSource(ValidArgumentsProvider.class)
void testCanInstantiateWithValidArguments(Sleep sleep,
                                          Duration initialDelay,
                                          Duration defaultTimeout,
                                          PatientExecutionHandler handler,
                                          DelaySupplierFactory delaySupplierFactory) {
    try {
        Assertions.assertNotNull(getInstance(sleep, initialDelay, defaultTimeout, handler, delaySupplierFactory),
                                 "Should have received a non-null instance with valid arguments.");
    } catch (Throwable thrown) {
        Assertions.fail("Should have been able to construct a PatientWait with valid arguments but caught throwable: " + thrown);
    }
}
项目:patience    文件:PatientWaitTest.java   
@ParameterizedTest
@DisplayName("it throws an exception for invalid arguments")
@ArgumentsSource(InvalidArgumentsProvider.class)
void testThrowsExceptionWithInvalidArguments(Sleep sleep,
                                             Duration initialDelay,
                                             Duration defaultTimeout,
                                             PatientExecutionHandler handler,
                                             DelaySupplierFactory delaySupplierFactory) {
    Assertions.assertThrows(IllegalArgumentException.class,
                            () -> getInstance(sleep, initialDelay, defaultTimeout, handler, delaySupplierFactory),
                            "Should have thrown an exception when the constructor is called with an invalid argument.");
}
项目:patience    文件:PatientWaitTest.java   
@ParameterizedTest
@DisplayName("it returns the given arguments")
@ArgumentsSource(ValidArgumentsProvider.class)
void testCanInstantiateWithValidArguments(Sleep sleep,
                                          Duration initialDelay,
                                          Duration defaultTimeout,
                                          PatientExecutionHandler handler,
                                          DelaySupplierFactory delaySupplierFactory) {
    PatientWait wait = getInstance(sleep, initialDelay, defaultTimeout, handler, delaySupplierFactory);
    Assertions.assertAll(() -> Assertions.assertEquals(sleep, wait.getSleep(), "Should return the given sleep"),
                         () -> Assertions.assertEquals(initialDelay, wait.getInitialDelay(), "Should return the given initial delay"),
                         () -> Assertions.assertEquals(defaultTimeout, wait.getDefaultTimeout(), "Should return the given default timeout"),
                         () -> Assertions.assertEquals(handler, wait.getExecutionHandler(), "Should return the given execution handler"),
                         () -> Assertions.assertEquals(delaySupplierFactory, wait.getDelaySupplierFactory(), "Should return the given delay supplier factory"));
}
项目:patience    文件:PatientRetryTest.java   
@ParameterizedTest
@DisplayName("it returns a non-null instance successfully for valid arguments")
@ArgumentsSource(ValidArgumentsProvider.class)
void testCanInstantiateWithValidArguments(Sleep sleep,
                                          Duration initialDelay,
                                          int defaultNumberOfRetries,
                                          PatientExecutionHandler handler,
                                          DelaySupplierFactory delaySupplierFactory) {
    try {
        Assertions.assertNotNull(getInstance(sleep, initialDelay, defaultNumberOfRetries, handler, delaySupplierFactory),
                                 "Should have received a non-null instance with valid arguments.");
    } catch (Throwable thrown) {
        Assertions.fail("Should have been able to construct a PatientRetry with valid arguments but caught throwable: " + thrown);
    }
}
项目:patience    文件:PatientRetryTest.java   
@ParameterizedTest
@DisplayName("it throws an exception for invalid arguments")
@ArgumentsSource(PatientRetryTest.InvalidArgumentsProvider.class)
void testThrowsExceptionWithInvalidArguments(Sleep sleep,
                                             Duration initialDelay,
                                             int defaultNumberOfRetries,
                                             PatientExecutionHandler handler,
                                             DelaySupplierFactory delaySupplierFactory) {
    Assertions.assertThrows(IllegalArgumentException.class,
                            () -> getInstance(sleep, initialDelay, defaultNumberOfRetries, handler, delaySupplierFactory),
                            "Should have thrown an exception when the constructor is called with an invalid argument.");
}
项目:patience    文件:PatientRetryTest.java   
@ParameterizedTest
@DisplayName("it returns the given arguments")
@ArgumentsSource(PatientRetryTest.ValidArgumentsProvider.class)
void testCanInstantiateWithValidArguments(Sleep sleep,
                                          Duration initialDelay,
                                          int defaultNumberOfRetries,
                                          PatientExecutionHandler handler,
                                          DelaySupplierFactory delaySupplierFactory) {
    PatientRetry retry = getInstance(sleep, initialDelay, defaultNumberOfRetries, handler, delaySupplierFactory);
    Assertions.assertAll(() -> Assertions.assertEquals(sleep, retry.getSleep(), "Should return the given sleep"),
                         () -> Assertions.assertEquals(initialDelay, retry.getInitialDelay(), "Should return the given initial delay"),
                         () -> Assertions.assertEquals(defaultNumberOfRetries, retry.getDefaultNumberOfRetries(), "Should return the given default timeout"),
                         () -> Assertions.assertEquals(handler, retry.getExecutionHandler(), "Should return the given execution handler"),
                         () -> Assertions.assertEquals(delaySupplierFactory, retry.getDelaySupplierFactory(), "Should return the given delay supplier factory"));
}
项目:patience    文件:AbstractRepeatedAttemptsExceptionTest.java   
@ParameterizedTest(name = "with message: <{0}>, and list: {1}")
@ArgumentsSource(ValidArgumentsProvider.class)
@DisplayName("it returns successfully with valid arguments")
void testCanBeInstantiatedWithValidArguments(String message,
                                             List<String> failedAttemptsDescriptions) {
    try {
        Assertions.assertNotNull(getInstance(message,
                                             failedAttemptsDescriptions),
                                 "Sending valid arguments to the constructor should have resulted in a non-null exception being returned.");
    } catch (Throwable thrown) {
        Assertions.fail("Should have been able to instantiate the exception but caught the throwable: " + thrown);
    }
}
项目:patience    文件:AbstractRepeatedAttemptsExceptionTest.java   
@ParameterizedTest(name = "with message: <{0}>, and list: {1}")
@ArgumentsSource(InValidArgumentsProvider.class)
@DisplayName("it throws an exception for invalid arguments")
void testThrowsForInvalidArguments(String message,
                                   List<String> failedAttemptsDescriptions) {
    Assertions.assertThrows(IllegalArgumentException.class,
                            () -> getInstance(message, failedAttemptsDescriptions),
                            "Should thrown an IllegalArgumentException for invalid arguments.");
}
项目:patience    文件:AbstractExceptionTest.java   
@ParameterizedTest(name = "with message: {0}, and cause: {1}")
@DisplayName("it returns successfully for a message and cause argument")
@ArgumentsSource(ValidMessageAndCauseArguments.class)
void testCanBeConstructedWithMessageAndCauseArgs(String message,
                                                 Throwable cause) {
    testInstantiation(() -> getInstance(message, cause));
}
项目:patience    文件:AbstractExceptionTest.java   
@ParameterizedTest(name = "with message: {0}, and cause: {1}")
@DisplayName("it returns the given message")
@ArgumentsSource(ValidMessageAndCauseArguments.class)
void testReturnsGivenMessage(String message, Throwable cause) {
    Assertions.assertEquals(message,
                            getInstance(message, cause).getMessage(),
                            "An exception should return it's given message.");
}
项目:patience    文件:AbstractExceptionTest.java   
@ParameterizedTest(name = "with message: {0}, and cause: {1}")
@DisplayName("it returns the given cause")
@ArgumentsSource(ValidMessageAndCauseArguments.class)
void testReturnsGivenCause(String message, Throwable cause) {
    Assertions.assertEquals(cause,
                            getInstance(message, cause).getCause(),
                            "An exception should return it's given cause.");
}
项目:patience    文件:IgnoringExecutionHandlerTest.java   
@ParameterizedTest
@DisplayName("it returns successfully for ignored throwable collection")
@ArgumentsSource(ValidIgnoredCollections.class)
void testCanInstantiateWithNullCollection(Collection<Class<? extends Throwable>> ignored) {
    Assertions.assertNotNull(getInstance(ignored),
                             "Should be able to instantiate with the given collection: " + ignored);
}
项目:patience    文件:IgnoringExecutionHandlerTest.java   
@ParameterizedTest
@DisplayName("it returns successfully for ignored throwable and not ignored throwable collections")
@ArgumentsSource(ValidIgnoredAndNotIgnoredCollections.class)
void testCanInstantiateWithNullCollection(Collection<Class<? extends Throwable>> ignored,
                                          Collection<Class<? extends Throwable>> notIgnored) {
    Assertions.assertNotNull(getInstance(ignored, notIgnored),
                             "Should be able to instantiate with the given ignored collection: " + ignored + ", and not ignored collection: " + notIgnored);
}
项目:patience    文件:DelaySuppliersTest.java   
@ParameterizedTest
@DisplayName("it throws an exception for an invalid argument")
@ArgumentsSource(InvalidFixedArgumentsProvider.class)
void testThrowsWithInvalidArguments(Duration duration) {
    Assertions.assertThrows(IllegalArgumentException.class,
                            () -> DelaySuppliers.fixed(duration),
                            "Should throw an exception for an invalid argument.");
}
项目:patience    文件:DelaySuppliersTest.java   
@ParameterizedTest
@DisplayName("it returns a non-null delay supplier for a valid argument")
@ArgumentsSource(ValidFixedArgumentsProvider.class)
void testReturnsSuccessfullyWithValidArguments(Duration duration) {
    Assertions.assertNotNull(DelaySuppliers.fixed(duration),
                             "Should return a non-null delay supplier for a valid argument.");
}
项目:patience    文件:DelaySuppliersTest.java   
@ParameterizedTest
@DisplayName("it returns a delay supplier that returns the expected duration supplier")
@ArgumentsSource(ValidFixedArgumentsProvider.class)
void testReturnsExpectedDelaySupplier(Duration duration,
                                      List<Duration> expectedSuppliedDurations) {
    Assumptions.assumeTrue(null != expectedSuppliedDurations && !expectedSuppliedDurations.isEmpty(),
                           "Should have received a non-null and non-empty list of expected durations.");
    Supplier<Duration> supplier = DelaySuppliers.fixed(duration).create();
    Assertions.assertAll(expectedSuppliedDurations.stream().map(next -> () -> Assertions.assertEquals(next, supplier.get())));
}
项目:patience    文件:DelaySuppliersTest.java   
@ParameterizedTest
@DisplayName("it throws an exception for an invalid argument")
@ArgumentsSource(InvalidExponentialArgumentsProvider.class)
void testThrowsWithInvalidArguments(int base,
                                    Duration duration) {
    Assertions.assertThrows(IllegalArgumentException.class,
                            () -> DelaySuppliers.exponential(base, duration),
                            "Should throw an exception for an invalid argument.");
}
项目:patience    文件:DelaySuppliersTest.java   
@ParameterizedTest
@DisplayName("it returns a non-null delay supplier for a valid argument")
@ArgumentsSource(ValidExponentialArgumentsProvider.class)
void testReturnsSuccessfullyWithValidArguments(int base,
                                               Duration duration) {
    Assertions.assertNotNull(DelaySuppliers.exponential(base, duration),
                             "Should return a non-null delay supplier for a valid argument.");
}
项目:patience    文件:DelaySuppliersTest.java   
@ParameterizedTest
@DisplayName("it returns a delay supplier that returns the expected duration supplier")
@ArgumentsSource(ValidExponentialArgumentsProvider.class)
void testReturnsExpectedDelaySupplier(int base,
                                      Duration duration,
                                      List<Duration> expectedSuppliedDurations) {
    Assumptions.assumeTrue(null != expectedSuppliedDurations && !expectedSuppliedDurations.isEmpty(),
                           "Should have received a non-null and non-empty list of expected durations.");
    Supplier<Duration> supplier = DelaySuppliers.exponential(base, duration).create();
    Assertions.assertAll(expectedSuppliedDurations.stream().map(next -> () -> Assertions.assertEquals(next, supplier.get())));
}
项目:patience    文件:SleepTest.java   
@ParameterizedTest
@DisplayName("throws an exception for an invalid duration")
@ArgumentsSource(InvalidDurationArgumentsProvider.class)
void testSleepForThrowsExceptionForInvalidDuration(Duration duration) {
    Assertions.assertThrows(IllegalArgumentException.class,
                            () -> getDefaultSleep().sleepFor(duration),
                            "Should throw an exception for an invalid duration.");
}
项目:patience    文件:SleepTest.java   
@ParameterizedTest
@DisplayName("calls sleepFor(long, int) with the expected arguments for the given duration")
@ArgumentsSource(DurationAndConversionArgumentsProvider.class)
void testSleepForProperlyConvertsTheGivenDuration(Duration duration,
                                                  long expectedMillis,
                                                  int expectedNanos) throws InterruptedException {
    Sleep sleep = spy(Sleep.class);
    sleep.sleepFor(duration);
    verify(sleep, times(1)).sleepFor(expectedMillis, expectedNanos);
}