@RepeatedTest(5) @ExtendWith(TemporaryFolderExtension.class) public void willCreateANewTemporaryFileEveryTime(TemporaryFolder temporaryFolder) throws IOException { File file = temporaryFolder.createFile("foo.txt"); assertThat(file.exists(), is(true)); if (temporaryFilePaths.isEmpty()) { temporaryFilePaths.add(file.getAbsolutePath()); } else { assertThat( "Received the same value twice, expected each random value to be different!", temporaryFilePaths, not(hasItem(file.getAbsolutePath()))); temporaryFilePaths.add(file.getAbsolutePath()); } }
@RepeatedTest(5) @ExtendWith(TemporaryFolderExtension.class) public void willCreateANewTemporaryDirectoryEveryTime(TemporaryFolder temporaryFolder) { File dir = temporaryFolder.createDirectory("bar"); assertThat(dir.exists(), is(true)); if (temporaryDirectoryPaths.isEmpty()) { temporaryDirectoryPaths.add(dir.getAbsolutePath()); } else { assertThat( "Received the same value twice, expected each random value to be different!", temporaryDirectoryPaths, not(hasItem(dir.getAbsolutePath()))); temporaryDirectoryPaths.add(dir.getAbsolutePath()); } }
@Test @ExtendWith(TemporaryFolderExtension.class) @ExpectedException(type = RuntimeException.class, messageIs = "Doh!") public void canHandleTheKitchenSink(TemporaryFolder temporaryFolder, @Random Long anyLong) throws IOException { // randomness assertThat(anyString, notNullValue()); assertThat(anyLong, notNullValue()); // system property assertThat(System.getProperty("x"), is("y")); // temporary folder File file = temporaryFolder.createFile("foo.txt"); assertThat(file.exists(), is(true)); // expected exception throw new RuntimeException("Doh!"); }
@Test @ExtendWith(CustomExtension.class) @ExplicitParamInjection public void testParametersNeedExtraAnnotation(@Default Foo foo, Bar bar, @MyQualifier BeanWithQualifier bean) { // Bar should be resolved by another extension Assertions.assertNotNull(bar); Assertions.assertEquals(CustomExtension.class.getSimpleName(), bar.ping()); // Foo should be resolved as usual Assertions.assertNotNull(foo); Assertions.assertEquals(Foo.class.getSimpleName(), foo.ping()); // BeanWithQualifier should be resolved Assertions.assertNotNull(bean); Assertions.assertEquals(BeanWithQualifier.class.getSimpleName(), bean.ping()); }
@Test @ExtendWith(CustomExtension.class) public void testParametersNeedExtraAnnotation(@Default Foo foo, Bar bar, @MyQualifier BeanWithQualifier bean) { // Bar should be resolved by another extension Assertions.assertNotNull(bar); Assertions.assertEquals(CustomExtension.class.getSimpleName(), bar.ping()); // Foo should be resolved as usual Assertions.assertNotNull(foo); Assertions.assertEquals(Foo.class.getSimpleName(), foo.ping()); // BeanWithQualifier should be resolved Assertions.assertNotNull(bean); Assertions.assertEquals(BeanWithQualifier.class.getSimpleName(), bean.ping()); }
@Test @ExtendWith(FooBarExtension.class) void doesNotResolveEveryParameter(String string, String[] fooBar, Injector injector) { assertThat(string).isEqualTo(TestModule.STRING); assertThat(fooBar).asList().containsExactly("foo", "bar").inOrder(); assertNull(injector.getExistingBinding(Key.get(String[].class))); }
@Test @ExtendWith(TemporaryFolderExtension.class) public void canInjectATemporaryFolderAsAParameter(TemporaryFolder temporaryFolder) throws IOException { File file = temporaryFolder.createFile("foo.txt"); assertThat(file.exists(), is(true)); File dir = temporaryFolder.createDirectory("bar"); assertThat(dir.exists(), is(true)); }
@RepeatedTest(5) @ExtendWith(RandomBeansExtension.class) public void willInjectANewRandomValueEachTime(@Random String anyString) { assertThat(anyString, notNullValue()); if (anyStrings.isEmpty()) { anyStrings.add(anyString); } else { assertThat( "Received the same value twice, expected each random value to be different!", anyStrings, not(hasItem(anyString))); anyStrings.add(anyString); } }
@TestTemplate @ExtendWith(DataProviderExtension.class) @DataProvider(value = { "2016-02-19 | 2016 | 02 | 19 | 00 | 00 | 00 | 000 | UTC", "2016-02-19T20:15:22.629 GMT | 2016 | 02 | 19 | 20 | 15 | 22 | 629 | UTC", }, splitBy = "\\|", stringConverter = DateTimeAwareStringConverter.class) // @formatter:off void testDateTimeDirectAnnotation(Date date, int year, int month, int dayOfMonth, int hourOfDay, int minute, int second, int millis, String timeZone) { // Expect: assertThat(date).isEqualTo(date(year, month, dayOfMonth, hourOfDay, minute, second, millis, timeZone)); }
@TestTemplate @ExtendWith(CustomConverterDataProviderExtension.class) @CustomConverterDataProvider(value = { "2016-02-19 | 2016 | 02 | 19 | 00 | 00 | 00 | 000 | UTC", "2016-02-19T20:15:22.629 GMT | 2016 | 02 | 19 | 20 | 15 | 22 | 629 | UTC", }, splitBy = "\\|", trimValues = true) // @formatter:off void testDateTimeDirectMetaAnnotation(Date date, int year, int month, int dayOfMonth, int hourOfDay, int minute, int second, int millis, String timeZone) { // Expect: assertThat(date).isEqualTo(date(year, month, dayOfMonth, hourOfDay, minute, second, millis, timeZone)); }
@TestTemplate @ExtendWith(UseDataProviderExtension.class) @UseDataProvider("dateTimeMetaAnnotationAndDataProviderMethodProvider") void testDateTimeAnnotationAndDataProviderMethod(Date date, int year, int month, int dayOfMonth, int hourOfDay, int minute, int second, int millis, String timeZone) { // Expect: assertThat(date).isEqualTo(date(year, month, dayOfMonth, hourOfDay, minute, second, millis, timeZone)); }
@TestTemplate @ExtendWith(UseDataProviderExtension.class) @UseDataProvider(resolver = DataProviderStartWithTestMethodNameResolver.class) void testNumber(Number number) { // When: int count = counterOne.incrementAndGet(); // Then: assertThat(count).isEqualTo(number.intValue()); }
@TestTemplate @ExtendWith(MyTestTemplateInvocationContextProvider.class) void testTemplate(String parameter) { System.out.println(parameter); }
@ExtendWith(MyParameterResolver.class) @Test public void test(Object parameter) { System.out.println("My parameter " + parameter); }
@ExtendWith(IgnoreIOExceptionExtension.class) @Test public void firstTest() throws IOException { throw new IOException("IO Exception"); }
@Override public Collection<Class<? extends Annotation>> getQualifyingAnnotations() { return asList(Test.class, TestFactory.class, ExtendWith.class); }
@ExtendWith(IgnoreIOExceptionExtension.class) @Test public void test1() throws IOException { throw new IOException("IO Exception"); }
@Test @ExtendWith(RandomBeansExtension.class) public void canInjectARandomString(@Random String anyString) { assertThat(anyString, notNullValue()); }
/** * Here I go a step further and annotate my days dynamically, by specifying the days I don't want the test to run * on with another custom annotation called @{@link DisabledWeekdays}. * * My extension {@link DisabledOnWeekday} later searches for @{@link DisabledWeekdays} and determines whether the * test should run or not. */ @Test @DisabledWeekdays({Calendar.THURSDAY, Calendar.SATURDAY}) @ExtendWith(DisabledOnWeekday.class) void disabledOnWeekdaysTest() {}