Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Dec 5, 2023
1 parent 719e76e commit 2d56c33
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.epam.reportportal.service.ReportPortal;
import com.epam.reportportal.service.ReportPortalClient;
import com.epam.reportportal.spock.ReportPortalSpockListener;
import com.epam.reportportal.spock.features.FailsWithAnnotationFail;
import com.epam.reportportal.spock.features.fail.FailsWithAnnotationFail;
import com.epam.reportportal.spock.utils.TestExtension;
import com.epam.reportportal.spock.utils.TestUtils;
import com.epam.reportportal.util.test.CommonUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2021 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.epam.reportportal.spock.fail;

import com.epam.reportportal.listeners.ItemStatus;
import com.epam.reportportal.listeners.ItemType;
import com.epam.reportportal.listeners.LogLevel;
import com.epam.reportportal.service.ReportPortal;
import com.epam.reportportal.service.ReportPortalClient;
import com.epam.reportportal.spock.ReportPortalSpockListener;
import com.epam.reportportal.spock.features.fail.FailsInDifferentMethod;
import com.epam.reportportal.spock.utils.TestExtension;
import com.epam.reportportal.spock.utils.TestUtils;
import com.epam.reportportal.util.test.CommonUtils;
import com.epam.ta.reportportal.ws.model.FinishExecutionRQ;
import com.epam.ta.reportportal.ws.model.FinishTestItemRQ;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
import com.epam.ta.reportportal.ws.model.log.SaveLogRQ;
import okhttp3.MultipartBody;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
import org.mockito.ArgumentCaptor;

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

import static com.epam.reportportal.spock.utils.TestUtils.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

public class TestFailsWithStackTrace {

private final String launchId = CommonUtils.namedId("launch_");
private final String classId = CommonUtils.namedId("class_");
private final List<String> methodIds = Stream.generate(() -> CommonUtils.namedId("method_")).limit(1).collect(Collectors.toList());

private final ReportPortalClient client = mock(ReportPortalClient.class);

@BeforeEach
public void setupMock() {
TestUtils.mockLaunch(client, launchId, classId, methodIds);
TestUtils.mockBatchLogging(client);
TestExtension.listener = new ReportPortalSpockListener(ReportPortal.create(client, standardParameters(), testExecutor()));
}

@Test
@SuppressWarnings("unchecked")
public void verify_fail_with_failed_reporting() {
TestExecutionSummary result = runClasses(FailsInDifferentMethod.class);

assertThat(result.getTotalFailureCount(), equalTo(1L));

verify(client).startLaunch(any());
ArgumentCaptor<StartTestItemRQ> startCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client).startTestItem(any(StartTestItemRQ.class));
verify(client, times(1)).startTestItem(same(classId), startCaptor.capture());

List<StartTestItemRQ> items = startCaptor.getAllValues();

items.forEach(i -> {
assertThat(i.getType(), equalTo(ItemType.STEP.name()));
assertThat(i.isHasStats(), equalTo(Boolean.TRUE));
});

ArgumentCaptor<FinishTestItemRQ> finishStepCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class);
methodIds.forEach(s -> verify(client).finishTestItem(eq(s), finishStepCaptor.capture()));

List<String> finishItemStatuses = finishStepCaptor.getAllValues()
.stream()
.map(FinishExecutionRQ::getStatus)
.collect(Collectors.toList());

assertThat(finishItemStatuses, hasSize(1));
assertThat(finishItemStatuses.get(0), equalTo(ItemStatus.FAILED.name()));
verify(client).finishTestItem(eq(classId), any());
verify(client).finishLaunch(eq(launchId), any());

ArgumentCaptor<List<MultipartBody.Part>> logCaptor = ArgumentCaptor.forClass(List.class);
verify(client, atLeast(1)).log(logCaptor.capture());
List<SaveLogRQ> rqs = toSaveLogRQ(logCaptor.getAllValues()).stream()
.filter(rq -> LogLevel.ERROR.name().equals(rq.getLevel()))
.collect(Collectors.toList());
assertThat(rqs, hasSize(1));
assertThat(rqs.get(0).getMessage(), containsString("FailsInDifferentMethod.anotherFailedMethod"));

//noinspection unchecked
verify(client, atLeast(1)).log(any(List.class));
verifyNoMoreInteractions(client);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.epam.reportportal.service.ReportPortal;
import com.epam.reportportal.service.ReportPortalClient;
import com.epam.reportportal.spock.ReportPortalSpockListener;
import com.epam.reportportal.spock.features.HelloSpockSpecUnrollFailed;
import com.epam.reportportal.spock.features.fail.HelloSpockSpecUnrollFailed;
import com.epam.reportportal.spock.utils.TestExtension;
import com.epam.reportportal.spock.utils.TestUtils;
import com.epam.reportportal.util.test.CommonUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2023 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.epam.reportportal.spock.features.fail

import spock.lang.Specification

class FailsInDifferentMethod extends Specification {

def anotherFailedMethod() {
throw new IllegalStateException("Some test flow failure")
}

def failedMethod() {
anotherFailedMethod()
}

def "failing test 1"() {
given:
failedMethod()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 EPAM Systems
* Copyright 2023 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.epam.reportportal.spock.features
package com.epam.reportportal.spock.features.fail

import spock.lang.FailsWith
import spock.lang.Specification
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 EPAM Systems
* Copyright 2023 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.epam.reportportal.spock.features
package com.epam.reportportal.spock.features.fail

import spock.lang.Rollup
import spock.lang.Specification
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 EPAM Systems
* Copyright 2023 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.epam.reportportal.spock.features
package com.epam.reportportal.spock.features.fail


import spock.lang.Specification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.epam.reportportal.service.ReportPortal;
import com.epam.reportportal.service.ReportPortalClient;
import com.epam.reportportal.spock.ReportPortalSpockListener;
import com.epam.reportportal.spock.features.HelloSpockSpecFailed;
import com.epam.reportportal.spock.features.fail.HelloSpockSpecFailed;
import com.epam.reportportal.spock.utils.TestExtension;
import com.epam.reportportal.spock.utils.TestUtils;
import com.epam.reportportal.util.test.CommonUtils;
Expand Down

0 comments on commit 2d56c33

Please sign in to comment.