Skip to content

Commit

Permalink
Merge pull request #108 from entrudo/RPP_Agents_Java_JUnit_LastErrorLog
Browse files Browse the repository at this point in the history
RPP_Agents_Java_JUnit_LastErrorLog
  • Loading branch information
HardNorth authored Apr 4, 2024
2 parents cbf8853 + 2be5c2f commit a2eedac
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public class ReportPortalListener implements ShutdownListener, RunnerWatcher, Ru
private static final String START_TIME = "START_TIME";
private static final String IS_RETRY = "IS_RETRY";
private static final String IS_THEORY = "IS_THEORY";
public static final String DESCRIPTION_TEST_ERROR_FORMAT = "%s\nError: \n%s";
private Throwable testThrowable;
private static final Map<Class<? extends Annotation>, ItemType> TYPE_MAP = Collections.unmodifiableMap(new HashMap<Class<? extends Annotation>, ItemType>() {
private static final long serialVersionUID = 5292344734560662610L;

Expand Down Expand Up @@ -581,6 +583,7 @@ protected void stopTestMethod(@Nonnull final Object runner, @Nonnull final Frame
*/
protected void stopTestMethod(@Nonnull final Object runner, @Nonnull final FrameworkMethod method,
@Nonnull final ReflectiveCallable callable, @Nonnull final ItemStatus status, @Nullable final Throwable throwable) {
testThrowable = throwable;
FinishTestItemRQ rq = buildFinishStepRq(runner, method, callable, status);
stopTestMethod(runner, method, callable, rq);

Expand Down Expand Up @@ -843,6 +846,11 @@ protected FinishTestItemRQ buildFinishStepRq(@Nullable final FrameworkMethod met
FinishTestItemRQ rq = new FinishTestItemRQ();
rq.setEndTime(Calendar.getInstance().getTime());
rq.setStatus(status.name());
if (status != ItemStatus.PASSED && testThrowable != null && method != null) {
rq.setDescription(String.format(DESCRIPTION_TEST_ERROR_FORMAT,
createStepDescription(this.context.getTestMethodDescription(method), method),
ExceptionUtils.getStackTrace(testThrowable)));
}
return rq;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.epam.reportportal.junit.callback;

import static com.epam.reportportal.junit.utils.TestUtils.PROCESSING_TIMEOUT;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;

import com.epam.reportportal.junit.ReportPortalListener;
import com.epam.reportportal.junit.features.callback.FailedDescriptionFeatureTest;
import com.epam.reportportal.junit.utils.TestUtils;
import com.epam.reportportal.listeners.ListenerParameters;
import com.epam.reportportal.service.ReportPortal;
import com.epam.reportportal.service.ReportPortalClient;
import com.epam.reportportal.util.test.CommonUtils;
import com.epam.ta.reportportal.ws.model.FinishTestItemRQ;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

public class FailedDescriptionTest {
private final String suiteId = CommonUtils.namedId("suite_");
private final String classId = CommonUtils.namedId("class_");
private final List<String> methodIds = Stream.generate(() -> CommonUtils.namedId("method_")).limit(2).collect(Collectors.toList());
private final ReportPortalClient client = mock(ReportPortalClient.class);
private final String testWithDescriptionAndAssertException = "testWithDescriptionAndAssertException";
private final String testWithDescriptionAndStepError = "testWithDescriptionAndStepError";
private final String testWithDescriptionAndException = "testWithDescriptionAndException";
private final String testErrorMessagePattern = "%s(com.epam.reportportal.junit.features.callback.FailedDescriptionFeatureTest)\nError: \n%s";
private final String assertErrorMessage = "java.lang.AssertionError: expected:<0> but was:<1>";
private final String exceptionStepErrorMessage = "java.util.NoSuchElementException: Test error message";
private final String exceptionErrorMessage = "java.lang.RuntimeException: Test error message";
private final String failedStatus = "FAILED";
private final String passedStatus = "PASSED";

@BeforeEach
public void setupMock() {
TestUtils.mockLaunch(client, null, suiteId, classId, methodIds);
TestUtils.mockBatchLogging(client);
ListenerParameters params = TestUtils.standardParameters();
ReportPortalListener.setReportPortal(ReportPortal.create(client, params, TestUtils.testExecutor()));
}

@Test
public void verify_retrieve_by_description() {
TestUtils.runClasses(FailedDescriptionFeatureTest.class);

ArgumentCaptor<StartTestItemRQ> startTestCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, timeout(PROCESSING_TIMEOUT).atLeastOnce()).startTestItem(same(suiteId), startTestCaptor.capture());

ArgumentCaptor<FinishTestItemRQ> finishTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class);
verify(client, timeout(PROCESSING_TIMEOUT).atLeastOnce()).finishTestItem(same(classId), finishTestCaptor.capture());

List<FinishTestItemRQ> finishTests = finishTestCaptor.getAllValues();
FinishTestItemRQ testCaseWithDescriptionAndAssertException = finishTests.stream()
.filter(f -> f.getDescription().startsWith(testWithDescriptionAndAssertException))
.findFirst()
.orElseThrow(NoSuchElementException::new);
FinishTestItemRQ testCaseWithDescriptionAndStepError = finishTests.stream()
.filter(f -> f.getDescription().startsWith(testWithDescriptionAndStepError))
.findFirst()
.orElseThrow(NoSuchElementException::new);
FinishTestItemRQ tesCaseWithDescriptionAndException = finishTests.stream()
.filter(f -> f.getDescription().startsWith(testWithDescriptionAndException))
.findFirst()
.orElseThrow(NoSuchElementException::new);
FinishTestItemRQ tesCaseWithDescriptionAndPassed = finishTests.stream()
.filter(f -> f.getDescription() == null)
.findFirst()
.orElseThrow(NoSuchElementException::new);

assertThat(testCaseWithDescriptionAndAssertException.getDescription(), startsWith(String.format(testErrorMessagePattern, testWithDescriptionAndAssertException, assertErrorMessage)));
assertThat(testCaseWithDescriptionAndAssertException.getStatus(), equalTo(failedStatus));

assertThat(testCaseWithDescriptionAndStepError.getDescription(), startsWith(String.format(testErrorMessagePattern, testWithDescriptionAndStepError, exceptionStepErrorMessage)));
assertThat(testCaseWithDescriptionAndStepError.getStatus(), equalTo(failedStatus));

assertThat(tesCaseWithDescriptionAndException.getDescription(), startsWith(String.format(testErrorMessagePattern, testWithDescriptionAndException, exceptionErrorMessage)));
assertThat(tesCaseWithDescriptionAndException.getStatus(), equalTo(failedStatus));

assertThat(tesCaseWithDescriptionAndPassed.getStatus(), equalTo(passedStatus));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.epam.reportportal.junit.features.callback;

import com.epam.reportportal.annotations.Step;
import java.util.NoSuchElementException;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FailedDescriptionFeatureTest {

private static final Logger LOGGER = LoggerFactory.getLogger(RetrieveByDescriptionFeatureTest.class);

@Test
public void testWithDescriptionAndException() {
throw new RuntimeException("Test error message");
}

@Test
public void testWithDescriptionAndAssertException() {
Assert.assertEquals(0, 1);
}

@Test
public void testWithDescriptionAndStepError() {
loginWithException();
Assert.assertTrue(true);
}

@Test
public void testWithDescriptionAndPassed() {
login();
Assert.assertTrue(true);
}

@Step
public void login() {
LOGGER.info("Login class method");
}

@Step
public void loginWithException() {
throw new NoSuchElementException("Test error message");
}
}

0 comments on commit a2eedac

Please sign in to comment.