Skip to content

Commit

Permalink
feat(#1218): add further methods to TestListener
Browse files Browse the repository at this point in the history
Co-authored-by: Thorsten Schlathoelter<[email protected]>
  • Loading branch information
phos-web committed Feb 12, 2025
1 parent cb889b2 commit 2f903a4
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 77 deletions.
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) {
// 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 @@ -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

0 comments on commit 2f903a4

Please sign in to comment.