Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test-util: added Abstract{Unit,Spring}Test classes and UnitTestMixin
This helps with test method lifecycle: - printing header/footer of the test - providing typical when/then methods and storing thread-local context It's better to use one of Abstract*Test classes instead of mixin interface directly.
- Loading branch information
Showing
5 changed files
with
186 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
infra/test-util/src/main/java/com/evolveum/midpoint/test/util/AbstractSpringTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (C) 2010-2020 Evolveum and contributors | ||
* | ||
* This work is dual-licensed under the Apache License 2.0 | ||
* and European Union Public License. See LICENSE file for details. | ||
*/ | ||
package com.evolveum.midpoint.test.util; | ||
|
||
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; | ||
import org.testng.ITestResult; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.BeforeMethod; | ||
|
||
import com.evolveum.midpoint.util.logging.Trace; | ||
import com.evolveum.midpoint.util.logging.TraceManager; | ||
|
||
/** | ||
* Base test class for tests integrated with Spring providing {@link UnitTestMixin} implementation. | ||
* Can be extended by any unit test class that would otherwise extend | ||
* {@link AbstractTestNGSpringContextTests}. | ||
*/ | ||
public abstract class AbstractSpringTest extends AbstractTestNGSpringContextTests | ||
implements UnitTestMixin { | ||
|
||
private static final ThreadLocal<ITestResult> TEST_CONTEXT_THREAD_LOCAL = new ThreadLocal<>(); | ||
|
||
/** | ||
* Hides parent's logger, but that one is from commons-logging and we don't want that. | ||
*/ | ||
protected final Trace logger = TraceManager.getTrace(getClass()); | ||
|
||
@BeforeMethod | ||
public void startTestContext(ITestResult testResult) { | ||
Class<?> testClass = testResult.getMethod().getTestClass().getRealClass(); | ||
String testMethodName = testResult.getMethod().getMethodName(); | ||
TestUtil.displayTestTitle(testClass.getSimpleName() + "." + testMethodName); | ||
|
||
TEST_CONTEXT_THREAD_LOCAL.set(testResult); | ||
} | ||
|
||
@AfterMethod | ||
public void finishTestContext(ITestResult testResult) { | ||
TEST_CONTEXT_THREAD_LOCAL.remove(); | ||
|
||
displayDefaultTestFooter(testResult); | ||
} | ||
|
||
@Override | ||
public String getTestNameShort() { | ||
return TEST_CONTEXT_THREAD_LOCAL.get().getMethod().getMethodName(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
infra/test-util/src/main/java/com/evolveum/midpoint/test/util/AbstractUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (C) 2010-2020 Evolveum and contributors | ||
* | ||
* This work is dual-licensed under the Apache License 2.0 | ||
* and European Union Public License. See LICENSE file for details. | ||
*/ | ||
package com.evolveum.midpoint.test.util; | ||
|
||
import org.testng.ITestResult; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.BeforeMethod; | ||
|
||
import com.evolveum.midpoint.util.logging.Trace; | ||
import com.evolveum.midpoint.util.logging.TraceManager; | ||
|
||
/** | ||
* Base test class providing basic {@link UnitTestMixin} implementation. | ||
* Can be extended by any unit test class that otherwise doesn't extend anything. | ||
*/ | ||
public abstract class AbstractUnitTest implements UnitTestMixin { | ||
|
||
private static final ThreadLocal<ITestResult> TEST_CONTEXT_THREAD_LOCAL = new ThreadLocal<>(); | ||
|
||
protected final Trace logger = TraceManager.getTrace(getClass()); | ||
|
||
@BeforeMethod | ||
public void startTestContext(ITestResult testResult) { | ||
Class<?> testClass = testResult.getMethod().getTestClass().getRealClass(); | ||
String testMethodName = testResult.getMethod().getMethodName(); | ||
TestUtil.displayTestTitle(testClass.getSimpleName() + "." + testMethodName); | ||
|
||
TEST_CONTEXT_THREAD_LOCAL.set(testResult); | ||
} | ||
|
||
@AfterMethod | ||
public void finishTestContext(ITestResult testResult) { | ||
TEST_CONTEXT_THREAD_LOCAL.remove(); | ||
|
||
displayDefaultTestFooter(testResult); | ||
} | ||
|
||
@Override | ||
public String getTestNameShort() { | ||
return TEST_CONTEXT_THREAD_LOCAL.get().getMethod().getMethodName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
infra/test-util/src/main/java/com/evolveum/midpoint/test/util/UnitTestMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (C) 2010-2020 Evolveum and contributors | ||
* | ||
* This work is dual-licensed under the Apache License 2.0 | ||
* and European Union Public License. See LICENSE file for details. | ||
*/ | ||
package com.evolveum.midpoint.test.util; | ||
|
||
import org.testng.ITestResult; | ||
|
||
/** | ||
* Mixin with various utility methods - typically delegating to {@link TestUtil}. | ||
*/ | ||
public interface UnitTestMixin { | ||
|
||
/** | ||
* Returns short test name - typically just a method name (without class). | ||
*/ | ||
String getTestNameShort(); | ||
|
||
/** | ||
* Displays "when" subsection header with test name. | ||
* Even better, use {@link #when(String)} and provide human readable description. | ||
*/ | ||
default void when() { | ||
when(null); | ||
} | ||
|
||
/** | ||
* Displays "when" subsection header with test name and provided description (nullable). | ||
*/ | ||
default void when(String description) { | ||
TestUtil.displayWhen(getTestNameShort(), description); | ||
} | ||
|
||
/** | ||
* Displays "then" subsection header with test name. | ||
* Even better, use {@link #then(String)} and provide human readable description. | ||
*/ | ||
default void then() { | ||
then(null); | ||
} | ||
|
||
/** | ||
* Displays "then" subsection header with test name and provided description (nullable). | ||
*/ | ||
default void then(String description) { | ||
TestUtil.displayThen(getTestNameShort(), description); | ||
} | ||
|
||
// TODO introduce "expect" as well? sometimes we use when/then combined section | ||
// in such a case instead of "given - when/then" we should have "given - expect" | ||
|
||
// TODO inline after merge to master | ||
default void displayWhen() { | ||
when(); | ||
} | ||
|
||
// TODO inline after merge to master | ||
default void displayWhen(String description) { | ||
when(description); | ||
} | ||
|
||
// TODO inline after merge to master | ||
default void displayThen() { | ||
then(); | ||
} | ||
|
||
// TODO inline after merge to master | ||
default void displayThen(String description) { | ||
then(description); | ||
} | ||
|
||
default void displayDefaultTestFooter(ITestResult testResult) { | ||
long testMsDuration = testResult.getEndMillis() - testResult.getStartMillis(); | ||
TestUtil.displayFooter(testResult.getMethod().getMethodName() + " FINISHED in " + testMsDuration + " ms"); | ||
} | ||
} |