@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() + " 的时候,测试方法需要至少一个参数"; }); }); }
@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); } }
@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); } }
@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"); }
@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); } }
@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); } }
@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"); }
@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); }
@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); }
@ParameterizedTest @ArgumentsSource(SerializedMessageRequestsProvider.class) void testDeserialization(JsonNode json) { MoreAssertions.assertSerializable(json, MessageRequest.class, MessageRequestTests::assertValid); }
@ParameterizedTest @ArgumentsSource(SerializedAttachmentsProvider.class) void testDeserialization(JsonNode json) { MoreAssertions.assertSerializable(json, Attachment.class, AttachmentTests::assertValid); }
@ParameterizedTest @ArgumentsSource(SerializedColorsProvider.class) void testSerialization(JsonNode json) { MoreAssertions.assertSerializable(json, Color.class, ColorTests::assertValid); }
@ParameterizedTest @ArgumentsSource(SerializedFootersProvider.class) void testDeserialization(JsonNode json) { MoreAssertions.assertSerializable(json, Footer.class, FooterTests::assertValid); }
@ParameterizedTest @ArgumentsSource(SerializedAuthorsProvider.class) void testSerialization(JsonNode json) { MoreAssertions.assertSerializable(json, Author.class, AuthorTests::assertValid); }
@ParameterizedTest @ArgumentsSource(SerializedFieldsProvider.class) void testSerialization(JsonNode json) { MoreAssertions.assertSerializable(json, Field.class, FieldTests::assertValid); }
@ParameterizedTest @ArgumentsSource(SerializedTitlesProvider.class) void testDeserialization(JsonNode json) { MoreAssertions.assertSerializable(json, Title.class, TitleTests::assertValid); }
@ParameterizedTest @ArgumentsSource(MessageRequestProvider.class) void testSendMessage(MessageRequest messageRequest, TestInfo testInfo) { assertThat(SlackWebHookService.with(assumingEnvironmentWebHookToken()) .sendMessage(EnrichTestMessageRequest.get().apply(messageRequest, testInfo)), is(equalTo(ResponseCode.OK))); }
@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); } }
@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."); }
@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")); }
@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); } }
@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."); }
@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")); }
@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); } }
@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."); }
@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)); }
@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."); }
@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."); }
@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); }
@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); }
@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."); }
@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."); }
@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()))); }
@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."); }
@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."); }
@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()))); }
@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."); }
@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); }