Skip to content

Commit

Permalink
Merge pull request #130 from nick130589/feature/displayname
Browse files Browse the repository at this point in the history
Added support @DisplayName annotation
  • Loading branch information
HardNorth authored Apr 30, 2024
2 parents f69ee7d + 57fd1b1 commit 0b769b5
Show file tree
Hide file tree
Showing 16 changed files with 563 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ protected StartTestItemRQ buildStartStepRq(@Nonnull final ExtensionContext conte
@Nullable final Date startTime) {
StartTestItemRQ rq = new StartTestItemRQ();
rq.setStartTime(ofNullable(startTime).orElseGet(Calendar.getInstance()::getTime));
rq.setName(createStepName(context));
rq.setName(createStepName(context, itemType));
rq.setDescription(ofNullable(description).orElseGet(() -> createStepDescription(context, itemType)));
rq.setType(itemType == TEMPLATE ? SUITE.name() : itemType.name());
String codeRef = getCodeRef(context);
Expand Down Expand Up @@ -795,11 +795,38 @@ protected FinishTestItemRQ buildFinishTestItemRq(@Nonnull ExtensionContext conte
* Extension point to customize test step name
*
* @param context JUnit's test context
* @param itemType a test method item type
* @return Test/Step Name being sent to ReportPortal
*/
protected String createStepName(ExtensionContext context) {
protected String createStepName(ExtensionContext context, ItemType itemType) {
String name = context.getDisplayName();
return name.length() > 1024 ? name.substring(0, 1021) + "..." : name;
String defaultValue = getMethodName(name);

if (itemType != STEP) {
return defaultValue;
}

Optional<Method> optionalMethod = getOptionalTestMethod(context);
if (!optionalMethod.isPresent()) {
return defaultValue;
}
Method method = optionalMethod.get();

com.epam.reportportal.annotations.DisplayName displayNameFromMethod = method.getAnnotation(com.epam.reportportal.annotations.DisplayName.class);
if (displayNameFromMethod != null) {
return getMethodName(displayNameFromMethod.value());
}

com.epam.reportportal.annotations.DisplayName displayNameFromClass = method.getDeclaringClass().getAnnotation(com.epam.reportportal.annotations.DisplayName.class);
if (displayNameFromClass != null) {
return getMethodName(displayNameFromClass.value());
}

return defaultValue;
}

private String getMethodName(String value) {
return value.length() > 1024 ? value.substring(0, 1021) + "..." : value;
}

/**
Expand Down
281 changes: 281 additions & 0 deletions src/test/java/com/epam/reportportal/junit5/DisplayNameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
package com.epam.reportportal.junit5;

import com.epam.reportportal.junit5.features.displayname.*;
import com.epam.reportportal.junit5.util.TestUtils;
import com.epam.reportportal.service.Launch;
import com.epam.reportportal.util.test.CommonUtils;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
import io.reactivex.Maybe;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.mockito.ArgumentCaptor;
import org.mockito.stubbing.Answer;

import java.util.List;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

/**
* @author <a href="mailto:[email protected]">Mykola Zakapko</a>
*/
public class DisplayNameTest {
public static class TestExtension extends ReportPortalExtension {
static Launch LAUNCH;

@Override
protected Launch getLaunch(ExtensionContext context) {
return LAUNCH;
}
}

@BeforeEach
public void setupMock() {
TestExtension.LAUNCH = mock(Launch.class);
when(TestExtension.LAUNCH.startTestItem(any())).thenAnswer((Answer<Maybe<String>>) invocation -> CommonUtils.createMaybeUuid());
when(TestExtension.LAUNCH.startTestItem(any(),
any()
)).thenAnswer((Answer<Maybe<String>>) invocation -> CommonUtils.createMaybeUuid());
}

@Test
void displayNameFromMethodAnnotationTest() {
TestUtils.runClasses(DisplayNameAnnotatedClassTest.class);

Launch launch = TestExtension.LAUNCH;

verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(1)).startTestItem(notNull(), captor.capture()); // Start a test

StartTestItemRQ request = captor.getValue();
assertThat(request.getName(), equalTo(DisplayNameAnnotatedClassTest.TEST_DISPLAY_NAME_CLASS));
}

@Test
void displayNameFromClassAnnotationTest() {
TestUtils.runClasses(DisplayNameAnnotatedClassTest.class);

Launch launch = TestExtension.LAUNCH;

verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(1)).startTestItem(notNull(), captor.capture()); // Start a test

StartTestItemRQ request = captor.getValue();
assertThat(request.getName(), equalTo(DisplayNameAnnotatedClassTest.TEST_DISPLAY_NAME_CLASS));
}

@Test
void displayNameFromBothMethodAndClassAnnotationTest() {
TestUtils.runClasses(DisplayNameAnnotatedMethodAndClassTest.class);

Launch launch = TestExtension.LAUNCH;

verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(1)).startTestItem(notNull(), captor.capture()); // Start a test

StartTestItemRQ request = captor.getValue();
//from both annotations the expected one should be taken from the method
assertThat(request.getName(), equalTo(DisplayNameAnnotatedMethodTest.TEST_DISPLAY_NAME_METHOD));
}

@Test
void displayNameFromMethodAnnotationDynamicTest() {
TestUtils.runClasses(DisplayNameAnnotatedMethodDynamicTest.class);

Launch launch = TestExtension.LAUNCH;
verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(2)).startTestItem(notNull(), captor.capture()); // Start a test

List<String> testStepsDisplayName = captor.getAllValues()
.stream()
.filter(e -> e.getType().equals(ItemType.STEP.name()))
.map(StartTestItemRQ::getName)
.collect(Collectors.toList());

assertThat(testStepsDisplayName, hasItem(DisplayNameAnnotatedMethodDynamicTest.TEST_DISPLAY_NAME_DYNAMIC_METHOD));
}

@Test
void displayNameFromClassAnnotationDynamicTest() {
TestUtils.runClasses(DisplayNameAnnotatedClassDynamicTest.class);

Launch launch = TestExtension.LAUNCH;
verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(2)).startTestItem(notNull(), captor.capture()); // Start a test

List<String> testStepsDisplayName = captor.getAllValues()
.stream()
.filter(e -> e.getType().equals(ItemType.STEP.name()))
.map(StartTestItemRQ::getName)
.collect(Collectors.toList());

assertThat(testStepsDisplayName, hasItem(DisplayNameAnnotatedClassDynamicTest.TEST_DISPLAY_NAME_DYNAMIC_CLASS));
}

@Test
void displayNameFromBothMethodAndClassAnnotationDynamicTest() {
TestUtils.runClasses(DisplayNameAnnotatedMethodAndClassDynamicTest.class);

Launch launch = TestExtension.LAUNCH;
verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(2)).startTestItem(notNull(), captor.capture()); // Start a test

List<String> testStepsDisplayName = captor.getAllValues()
.stream()
.filter(e -> e.getType().equals(ItemType.STEP.name()))
.map(StartTestItemRQ::getName)
.collect(Collectors.toList());
//from both annotations the expected one should be taken from the method
assertThat(testStepsDisplayName, hasItem(DisplayNameAnnotatedMethodDynamicTest.TEST_DISPLAY_NAME_DYNAMIC_METHOD));
}

@Test
void displayNameJunitAndRPFromMethodAnnotationTest() {
TestUtils.runClasses(DisplayNameBothJunitAndRPAnnotatedMethodTest.class);

Launch launch = TestExtension.LAUNCH;

verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(1)).startTestItem(notNull(), captor.capture()); // Start a test

StartTestItemRQ request = captor.getValue();
assertThat(request.getName(), equalTo(DisplayNameBothJunitAndRPAnnotatedMethodTest.TEST_DISPLAY_NAME_METHOD));
}

@Test
void displayNameJunitAndRPFromClassAnnotationAnnotationTest() {
TestUtils.runClasses(DisplayNameBothJunitAndRPAnnotatedClassTest.class);

Launch launch = TestExtension.LAUNCH;

verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(1)).startTestItem(notNull(), captor.capture()); // Start a test

StartTestItemRQ request = captor.getValue();
assertThat(request.getName(), equalTo(DisplayNameBothJunitAndRPAnnotatedClassTest.TEST_DISPLAY_NAME_CLASS));
}

@Test
void displayNameJunitAndRPFromMethodAnnotationDynamicTest() {
TestUtils.runClasses(DisplayNameBothJunitAndRPAnnotatedMethodDynamicTest.class);

Launch launch = TestExtension.LAUNCH;
verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(2)).startTestItem(notNull(), captor.capture()); // Start a test

List<String> testStepsDisplayName = captor.getAllValues()
.stream()
.filter(e -> e.getType().equals(ItemType.STEP.name()))
.map(StartTestItemRQ::getName)
.collect(Collectors.toList());

assertThat(testStepsDisplayName, hasItem(DisplayNameBothJunitAndRPAnnotatedMethodDynamicTest.TEST_DISPLAY_NAME_DYNAMIC_METHOD));
}

@Test
void displayNameJunitAndRPFromClassAnnotationDynamicTest() {
TestUtils.runClasses(DisplayNameBothJunitAndRPAnnotatedClassDynamicTest.class);

Launch launch = TestExtension.LAUNCH;
verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(2)).startTestItem(notNull(), captor.capture()); // Start a test

List<String> testStepsDisplayName = captor.getAllValues()
.stream()
.filter(e -> e.getType().equals(ItemType.STEP.name()))
.map(StartTestItemRQ::getName)
.collect(Collectors.toList());

assertThat(testStepsDisplayName, hasItem(DisplayNameBothJunitAndRPAnnotatedClassDynamicTest.TEST_DISPLAY_NAME_DYNAMIC_CLASS));
}

@Test
void displayNameJunitAndRPFromBothMethodAndClassAnnotationTest() {
TestUtils.runClasses(DisplayNameBothJunitAndRPAnnotatedMethodAndClassTest.class);

Launch launch = TestExtension.LAUNCH;

verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(1)).startTestItem(notNull(), captor.capture()); // Start a test

StartTestItemRQ request = captor.getValue();
//from both annotations the expected one should be taken from the method
assertThat(request.getName(), equalTo(DisplayNameAnnotatedMethodTest.TEST_DISPLAY_NAME_METHOD));
}

@Test
void displayNameJunitAndRPFromBothMethodAndClassAnnotationDynamicTest() {
TestUtils.runClasses(DisplayNameBothJunitAndRPAnnotatedMethodAndClassDynamicTest.class);

Launch launch = TestExtension.LAUNCH;
verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(2)).startTestItem(notNull(), captor.capture()); // Start a test

List<String> testStepsDisplayName = captor.getAllValues()
.stream()
.filter(e -> e.getType().equals(ItemType.STEP.name()))
.map(StartTestItemRQ::getName)
.collect(Collectors.toList());
//from both annotations the expected one should be taken from the method
assertThat(testStepsDisplayName, hasItem(DisplayNameAnnotatedMethodDynamicTest.TEST_DISPLAY_NAME_DYNAMIC_METHOD));
}

@Test
void displayNameFromClassAnnotationWithEmptyValueTest() {
TestUtils.runClasses(DisplayNameAnnotatedClassWithEmptyValueTest.class);

Launch launch = TestExtension.LAUNCH;

verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(1)).startTestItem(notNull(), captor.capture()); // Start a test

StartTestItemRQ request = captor.getValue();
assertThat(request.getName(), equalTo(DisplayNameAnnotatedClassWithEmptyValueTest.TEST_DISPLAY_NAME_CLASS));
}

@Test
void displayNameFromMethodAnnotationWithEmptyValueTest() {
TestUtils.runClasses(DisplayNameAnnotatedMethodWithEmptyValueTest.class);

Launch launch = TestExtension.LAUNCH;

verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(1)).startTestItem(notNull(), captor.capture()); // Start a test

StartTestItemRQ request = captor.getValue();
assertThat(request.getName(), equalTo(DisplayNameAnnotatedMethodWithEmptyValueTest.TEST_DISPLAY_NAME_METHOD));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.epam.reportportal.junit5.features.displayname;

import com.epam.reportportal.annotations.DisplayName;
import com.epam.reportportal.junit5.DisplayNameTest;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.stream.Stream;

import static org.junit.jupiter.api.DynamicTest.dynamicTest;

@ExtendWith(DisplayNameTest.TestExtension.class)
@DisplayName(DisplayNameAnnotatedClassDynamicTest.TEST_DISPLAY_NAME_DYNAMIC_CLASS)
public class DisplayNameAnnotatedClassDynamicTest {
public static final String TEST_DISPLAY_NAME_DYNAMIC_CLASS = "My test displayName on the dynamic class";
@TestFactory
Stream<DynamicTest> testForTestFactory() {
return Stream.of(dynamicTest("My dynamic test", () -> System.out.println("Inside dynamic test")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.epam.reportportal.junit5.features.displayname;

import com.epam.reportportal.junit5.DisplayNameTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@com.epam.reportportal.annotations.DisplayName(DisplayNameAnnotatedClassTest.TEST_DISPLAY_NAME_CLASS)
@ExtendWith(DisplayNameTest.TestExtension.class)
public class DisplayNameAnnotatedClassTest {
public static final String TEST_DISPLAY_NAME_CLASS = "My display name";
@Test
public void testDisplayNameTest() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.epam.reportportal.junit5.features.displayname;

import com.epam.reportportal.junit5.DisplayNameTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@com.epam.reportportal.annotations.DisplayName(DisplayNameAnnotatedClassWithEmptyValueTest.TEST_DISPLAY_NAME_CLASS)
@ExtendWith(DisplayNameTest.TestExtension.class)
public class DisplayNameAnnotatedClassWithEmptyValueTest {
public static final String TEST_DISPLAY_NAME_CLASS = "";
@Test
public void testDisplayNameTest() {
}
}
Loading

0 comments on commit 0b769b5

Please sign in to comment.