Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#1218): add events on TestListener #1313

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public CitrusRuntimeException handleError(String testName, String packageName, S
try {
testListeners.onTestStart(dummyTest);
testListeners.onTestFailure(dummyTest, exception);
testListeners.onTestFinish(dummyTest);
testListeners.onTestExecutionEnd(dummyTest);
} catch (Exception e) {
logger.warn("Executing error handler listener failed!", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,105 @@

/**
* Test listener interface. Listeners invoked on test start, finish, failure, skip, success.
*
*/
public interface TestListener {
/**
* Invoked when test gets started
* @param test
* Invoked when test when a test starts execution
*/
void onTestStart(TestCase test);

/**
* Invoked when test gets finished
* @param test
* @deprecated use on {@link #onTestExecutionEnd(TestCase)}
*/
@Deprecated(forRemoval = true)
default void onTestFinish(TestCase test) {
// Do nothing
}

/**
* Invoked when test execution has ended (after final actions execution and
* before {@link org.citrusframework.container.AfterTest} execution)
*
* @see #onTestEnd(TestCase)
*/
default void onTestExecutionStart(TestCase test) {
onTestFinish(test);
}


/**
* Invoked when test execution has ended (after final actions execution and
* before {@link org.citrusframework.container.AfterTest} execution)
*
* @see #onTestEnd(TestCase)
*/
void onTestFinish(TestCase test);
default void onTestExecutionEnd(TestCase test) {
onTestFinish(test);
}

/**
* Invoked when test finished with success
* @param test
* Invoked at the very end of test execution
*
* @see #onTestFinish(TestCase)
*/
default void onTestEnd(TestCase test) {
phos-web marked this conversation as resolved.
Show resolved Hide resolved
// Default implementation does nothing
}

/**
* Invoked when a test finishes successfully
*/
void onTestSuccess(TestCase test);

/**
* Invoked when test finished with failure
* @param test
* Invoked when a test finishes with failure
*/
void onTestFailure(TestCase test, Throwable cause);

/**
* Invoked when test is skipped
* @param test
* Invoked when a test is skipped
*/
void onTestSkipped(TestCase test);

/**
* Invoked when final actions start, only if any exist
*/
default void onFinalActionsStart(TestCase test) {
// Default implementation does nothing
}

/**
* Invoked after final actions have completely finished, only if any exist
*/
default void onFinalActionsEnd(TestCase test) {
// Default implementation does nothing
}

/**
* Invoked when {@link org.citrusframework.container.BeforeTest} execution starts, only if any exist
*/
default void onBeforeTestStart(TestCase test) {
// Default implementation does nothing
}

/**
* Invoked when {@link org.citrusframework.container.BeforeTest} execution ends, only if any exist
*/
default void onBeforeTestEnd(TestCase test) {
// Default implementation does nothing
}

/**
* Invoked when {@link org.citrusframework.container.AfterTest} execution starts, only if any exist
*/
default void onAfterTestStart(TestCase test) {
// Default implementation does nothing
}

/**
* Invoked when {@link org.citrusframework.container.AfterTest} execution ends, only if any exist
*/
default void onAfterTestEnd(TestCase test) {
// Default implementation does nothing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,29 @@ public void onTestFailure(TestCase test, Throwable cause) {
}
}

/**
* @deprecated use on {@link #onTestExecutionEnd(TestCase)}
*/
@Deprecated(forRemoval = true)
public void onTestFinish(TestCase test) {
for (TestListener listener : testListeners) {
listener.onTestFinish(test);
}
}

public void onTestExecutionStart(TestCase test) {
for (TestListener listener : testListeners) {
listener.onTestExecutionStart(test);
}

}

public void onTestExecutionEnd(TestCase test) {
for (TestListener listener : testListeners) {
listener.onTestExecutionEnd(test);
}
}

public void onTestSkipped(TestCase test) {
for (TestListener listener : testListeners) {
listener.onTestSkipped(test);
Expand All @@ -55,6 +72,48 @@ public void onTestStart(TestCase test) {
}
}

public void onTestFinalization(TestCase test) {
for (TestListener listener : testListeners) {
listener.onTestEnd(test);
}
}

public void onFinalActionsStart(TestCase test) {
for (TestListener listener : testListeners) {
listener.onFinalActionsStart(test);
}
}

public void onFinalActionsEnd(TestCase test) {
for (TestListener listener : testListeners) {
listener.onFinalActionsEnd(test);
}
}

public void onBeforeTestEnd(TestCase test) {
for (TestListener listener : testListeners) {
listener.onBeforeTestEnd(test);
}
}

public void onAfterTestEnd(TestCase test) {
for (TestListener listener : testListeners) {
listener.onAfterTestEnd(test);
}
}

public void onBeforeTestStart(TestCase test) {
for (TestListener listener : testListeners) {
listener.onBeforeTestStart(test);
}
}

public void onAfterTestStart(TestCase test) {
for (TestListener listener : testListeners) {
listener.onAfterTestStart(test);
}
}

public void onTestSuccess(TestCase test) {
for (TestListener listener : testListeners) {
listener.onTestSuccess(test);
Expand All @@ -70,7 +129,6 @@ public void addTestListener(TestListener listener) {

/**
* Obtains the testListeners.
* @return
*/
public List<TestListener> getTestListeners() {
return testListeners;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void onTestStart(TestCase test) {
}

@Override
public void onTestFinish(TestCase test) {
public void onTestExecutionEnd(TestCase test) {
if (nonNull(test.getTestResult())) {
testResults.addResult(test.getTestResult());
}
Expand Down Expand Up @@ -108,35 +108,27 @@ public void addTestReporter(TestReporter testReporter) {

/**
* Obtains the testReporters.
*
* @return
*/
public List<TestReporter> getTestReporters() {
return unmodifiableList(testReporters);
}

/**
* Obtains the autoClear.
*
* @return
*/
public boolean isAutoClear() {
return autoClear;
}

/**
* Specifies the autoClear.
*
* @param autoClear
*/
public void setAutoClear(boolean autoClear) {
this.autoClear = autoClear;
}

/**
* Gets the testResults.
*
* @return
*/
public TestResults getTestResults() {
return testResults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public void handleErrorGracefullyHandlesErrorInTestFinishListener() {
var testListenerMock = attachTestListenerMockToFixture();

var cause = new CitrusRuntimeException("thrown with a purpose!");
doThrow(cause).when(testListenerMock).onTestFinish(any(TestContext.EmptyTestCase.class));
doThrow(cause).when(testListenerMock).onTestExecutionEnd(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));
verify(testListenerMock).onTestExecutionEnd(any(TestContext.EmptyTestCase.class));
}

private TestListener attachTestListenerMockToFixture() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@ private void verifyGenerateReport(Runnable invocation) {
}

@Test
public void onTestFinishAddsTestResultToList() {
public void onTestExecutionEndAddsTestResultToList() {
var testResultMock = mock(TestResult.class);
doReturn(testResultMock).when(testCaseMock).getTestResult();

fixture.onTestFinish(testCaseMock);
fixture.onTestExecutionEnd(testCaseMock);

var testResults = fixture.getTestResults().asList();
assertEquals(1, testResults.size());
assertEquals(testResultMock, testResults.get(0));
}

@Test
public void onTestFinishIgnoresNullTestResult() {
public void onTestExecutionEndIgnoresNullTestResult() {
doReturn(null).when(testCaseMock).getTestResult();

fixture.onTestFinish(testCaseMock);
fixture.onTestExecutionEnd(testCaseMock);

var testResults = fixture.getTestResults().asList();
assertEquals(0, testResults.size());
Expand Down
Loading
Loading