@Override public MessageRequest apply(MessageRequest messageRequest, TestInfo testInfo) { checkArgument(testInfo.getTestClass().isPresent()); checkArgument(testInfo.getTestMethod().isPresent()); String methodName = testInfo.getTestMethod().get().getName(); String className = testInfo.getTestClass().get().getSimpleName(); String basicSummary = String.format("Called from %s within %s", methodName, className); return MessageRequest.builder() .from(messageRequest) .addAttachments(Attachment.builder() .fallback(basicSummary) .text(basicSummary) .build()) .build(); }
@Test @DisplayName("My test") @Tag("my-tag") void testOne(TestInfo testInfo) { System.out.println(testInfo.getDisplayName()); System.out.println(testInfo.getTags()); System.out.println(testInfo.getTestClass()); System.out.println(testInfo.getTestMethod()); }
@Test @DisplayName("My test") @Tag("my-tag") void test1(TestInfo testInfo) { System.out.println(testInfo.getDisplayName()); System.out.println(testInfo.getTags()); System.out.println(testInfo.getTestClass()); System.out.println(testInfo.getTestMethod()); }
@ParameterizedTest @ArgumentsSource(MessageRequestProvider.class) void testSendMessage(MessageRequest messageRequest, TestInfo testInfo) { assertThat(SlackWebHookService.with(assumingEnvironmentWebHookToken()) .sendMessage(EnrichTestMessageRequest.get().apply(messageRequest, testInfo)), is(equalTo(ResponseCode.OK))); }
@Test void junitAndSpringMethodInjectionCombined(@Autowired Cat kittyCat, TestInfo testInfo, ApplicationContext context, TestReporter testReporter) { assertNotNull(testInfo, "TestInfo should have been injected by JUnit"); assertNotNull(testReporter, "TestReporter should have been injected by JUnit"); assertNotNull(context, "ApplicationContext should have been injected by Spring"); assertNotNull(kittyCat, "Cat should have been @Autowired by Spring"); }
SpringJUnit5ConstructorInjectionTests(ApplicationContext applicationContext, @Autowired Person dilbert, @Autowired Dog dog, @Value("${enigma}") Integer enigma, TestInfo testInfo) { this.applicationContext = applicationContext; this.dilbert = dilbert; this.dog = dog; this.enigma = enigma; this.testInfo = testInfo; }
@Test @DisplayName("TEST 1") @Tag("my-tag") void test1(TestInfo testInfo) { assertEquals("TEST 1", testInfo.getDisplayName()); assertTrue(testInfo.getTags().contains("my-tag")); assertTrue(TestHelper.getLastRecordedStateForVariable("testInfo") != null); }
@DataProvider static Object[][] loadFromExternalFile(TestInfo testInfo, TestReporter testReporter) { checkNotNull(testInfo, "'testInfo' is not set"); checkNotNull(testReporter, "'testReporter' is not set"); String testDataFile = testInfo.getTestMethod().get().getAnnotation(ExternalFile.class).value(); // Load the data from the external file here ... return new Object[][] { { testDataFile } }; }
@ParameterizedTest // @formatter:off @DataProvider(value = { "2 | 3 | 2, 3", "c | d | c, d", }, splitBy = "\\|") // @formatter:on void testThree(String a, String b, String expected, TestInfo testInfo) { // Expect: assertThat(testInfo.getDisplayName()).contains(expected); }
@TestTemplate @UseDataProvider void testDivide(int dividend, int divisor, int result, TestInfo testInfo) { // Expect: assertThat(dividend / divisor).isEqualTo(result); assertThat(testInfo.getDisplayName()) .endsWith(String.format("dividend=%d, divisor=%d, result=%d", dividend, divisor, result)); }
@TestTemplate // @formatter:off @DataProvider(value = { "2 | 3 | 2, 3", "c | d | c, d", }, splitBy = "\\|") // @formatter:on void testThree(String a, String b, String expected, TestInfo testInfo) { // Expect: assertThat(testInfo.getDisplayName()).contains(expected); }
@BeforeEach void init(TestInfo testInfo) { String displayName = testInfo.getDisplayName(); System.out.printf("@BeforeEach %s %n", displayName); }
@BeforeEach default void beforeEachTest(TestInfo testInfo) { log.info("About to execute {}", testInfo.getDisplayName()); }
@AfterEach default void afterEachTest(TestInfo testInfo) { log.info("Finished executing {}", testInfo.getDisplayName()); }
@RepeatedTest(value = 2, name = "{displayName} {currentRepetition}/{totalRepetitions}") @DisplayName("Repeat!") void customDisplayName(TestInfo testInfo) { System.out.println(testInfo.getDisplayName()); }
@RepeatedTest(value = 2, name = RepeatedTest.LONG_DISPLAY_NAME) @DisplayName("Test using long display name") void customDisplayNameWithLongPattern(TestInfo testInfo) { System.out.println(testInfo.getDisplayName()); }
@RepeatedTest(value = 2, name = RepeatedTest.SHORT_DISPLAY_NAME) @DisplayName("Test using short display name") void customDisplayNameWithShortPattern(TestInfo testInfo) { System.out.println(testInfo.getDisplayName()); }
@BeforeEach public void setUp(TestInfo testInfo) throws Exception { this.mockMvc = MockMvcBuilders.standaloneSetup(new UserAccountController(this.userService, this.docService)).build(); String displayName = testInfo.getDisplayName(); assertTrue(displayName.equals("Should return error message for when user not existing in the database tries to login.")); }
@BeforeEach public void before(TestInfo testInfo, @TempDirectory(parentPath = ".") Path outDir) throws IOException, InterruptedException { testName = testInfo.getTestMethod().get().getName(); this.outDir = outDir; }
@ParameterizedTest @ValueSource(strings = { "Hello", "JUnit" }) void withOtherParams(String word, TestInfo info, TestReporter reporter) { reporter.publishEntry(info.getDisplayName(), "Word: " + word); assertNotNull(word); }
@BeforeEach void initialize(@Mock MyType myType, TestInfo testInfo) { when(myType.getName()).thenReturn(testInfo.getDisplayName()); when(numberGenerator.next()).thenReturn(42); }
@BeforeEach default void beforeEachTest(TestInfo testInfo) { }
@AfterEach default void afterEachTest(TestInfo testInfo) { }
@ParameterizedTest @UseDataProvider void testObjectArrayArray(Object a, Object b, String expected, TestInfo testInfo) { // Expect: assertThat(testInfo.getDisplayName()).contains(expected); }
@ParameterizedTest @UseDataProvider void testObjectArray(Object a, TestInfo testInfo) { // Expect: assertThat(testInfo.getDisplayName()).contains("a"); }
@TestTemplate @UseDataProvider void testObjectArrayArray(Object a, Object b, String expected, TestInfo testInfo) { // Expect: assertThat(testInfo.getDisplayName()).contains(expected); }
@TestTemplate @UseDataProvider void testObjectArray(Object a, TestInfo testInfo) { // Expect: assertThat(testInfo.getDisplayName()).contains("a"); }