forked from citrusframework/citrus
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestContextUnitTest.java
87 lines (63 loc) · 3.25 KB
/
TestContextUnitTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.citrusframework.context;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.report.TestListener;
import org.citrusframework.report.TestListeners;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
public class TestContextUnitTest {
private TestContext fixture;
@BeforeTest
void beforeTestClass() {
fixture = new TestContext();
}
@Test
public void handleErrorGracefullyHandlesErrorInTestStartListener() {
var testListenerMock = attachTestListenerMockToFixture();
var cause = new CitrusRuntimeException("thrown with a purpose!");
doThrow(cause).when(testListenerMock).onTestStart(any(TestContext.EmptyTestCase.class));
invokeHandleErrorOnFixture(cause);
verify(testListenerMock).onTestStart(any(TestContext.EmptyTestCase.class));
verifyNoMoreInteractions(testListenerMock);
}
@Test
public void handleErrorGracefullyHandlesErrorInTestFailureListener() {
var testListenerMock = attachTestListenerMockToFixture();
var cause = new CitrusRuntimeException("thrown with a purpose!");
doThrow(cause).when(testListenerMock).onTestFailure(any(TestContext.EmptyTestCase.class), any(CitrusRuntimeException.class));
invokeHandleErrorOnFixture(cause);
verify(testListenerMock).onTestStart(any(TestContext.EmptyTestCase.class));
verify(testListenerMock).onTestFailure(any(TestContext.EmptyTestCase.class), any(CitrusRuntimeException.class));
verifyNoMoreInteractions(testListenerMock);
}
@Test
public void handleErrorGracefullyHandlesErrorInTestFinishListener() {
var testListenerMock = attachTestListenerMockToFixture();
var cause = new CitrusRuntimeException("thrown with a purpose!");
doThrow(cause).when(testListenerMock).onTestFinish(any(TestContext.EmptyTestCase.class));
invokeHandleErrorOnFixture(cause);
verify(testListenerMock).onTestStart(any(TestContext.EmptyTestCase.class));
verify(testListenerMock).onTestFailure(any(TestContext.EmptyTestCase.class), any(CitrusRuntimeException.class));
verify(testListenerMock).onTestFinish(any(TestContext.EmptyTestCase.class));
}
private TestListener attachTestListenerMockToFixture() {
var testListeners = new TestListeners();
var testListenerMock = mock(TestListener.class);
testListeners.addTestListener(testListenerMock);
fixture.setTestListeners(testListeners);
return testListenerMock;
}
private void invokeHandleErrorOnFixture(CitrusRuntimeException cause) {
var testName = "test name";
var packageName = "package name";
var message = "additional message";
var citrusRuntimeException = fixture.handleError(testName, packageName, message, cause);
assertEquals(citrusRuntimeException.getMessage(), message);
assertEquals(citrusRuntimeException.getCause(), cause);
}
}