mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-24 19:31:17 +08:00
simplify mock context init
This commit is contained in:
parent
5ef06c4bde
commit
d95cba6d37
@ -18,7 +18,7 @@ public class TestClassHandler extends BaseClassWithContextHandler {
|
||||
|
||||
private static final String CLASS_MOCK_CONTEXT_UTIL = "com/alibaba/testable/core/util/MockContextUtil";
|
||||
private static final String METHOD_INIT = "init";
|
||||
private static final String DESC_METHOD_INIT = "(Ljava/lang/String;Ljava/lang/String;)V";
|
||||
private static final String DESC_METHOD_INIT = "()V";
|
||||
private static final String METHOD_CLEAN = "clean";
|
||||
private static final String DESC_METHOD_CLEAN = "()V";
|
||||
private static final String THIS = "this";
|
||||
@ -50,7 +50,7 @@ public class TestClassHandler extends BaseClassWithContextHandler {
|
||||
}
|
||||
for (MethodNode mn : cn.methods) {
|
||||
handleTestableUtil(mn);
|
||||
handleTestCaseMethod(cn, mn, framework);
|
||||
handleTestCaseMethod(mn, framework);
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,19 +94,17 @@ public class TestClassHandler extends BaseClassWithContextHandler {
|
||||
cn.methods.add(afterTestMethod);
|
||||
}
|
||||
|
||||
private void handleTestCaseMethod(ClassNode cn, MethodNode mn, Framework framework) {
|
||||
private void handleTestCaseMethod(MethodNode mn, Framework framework) {
|
||||
TestCaseMethodType type = framework.checkMethodType(mn);
|
||||
if (type.equals(TestCaseMethodType.TEST)) {
|
||||
injectMockContextInit(cn.name, mn);
|
||||
injectMockContextInit(mn);
|
||||
} else if (type.equals(TestCaseMethodType.AFTER_TEST)) {
|
||||
injectMockContextClean(mn);
|
||||
}
|
||||
}
|
||||
|
||||
private void injectMockContextInit(String testClassName, MethodNode mn) {
|
||||
private void injectMockContextInit(MethodNode mn) {
|
||||
InsnList il = new InsnList();
|
||||
il.add(new LdcInsnNode(testClassName));
|
||||
il.add(new LdcInsnNode(mn.name));
|
||||
il.add(new MethodInsnNode(INVOKESTATIC, CLASS_MOCK_CONTEXT_UTIL, METHOD_INIT, DESC_METHOD_INIT, false));
|
||||
mn.instructions.insertBefore(mn.instructions.getFirst(), il);
|
||||
}
|
||||
|
@ -9,10 +9,17 @@ public class MockContextUtil {
|
||||
|
||||
public static InheritableThreadLocal<MockContext> context = new TransmittableThreadLocal<MockContext>();
|
||||
|
||||
/**
|
||||
* [0]Thread → [1]MockContextUtil → [2]TestClass
|
||||
*/
|
||||
public static final int INDEX_OF_TEST_CLASS = 2;
|
||||
|
||||
/**
|
||||
* Should be invoked at the beginning of each test case method
|
||||
*/
|
||||
public static void init(String testClassName, String testCaseName) {
|
||||
public static void init() {
|
||||
String testClassName = Thread.currentThread().getStackTrace()[INDEX_OF_TEST_CLASS].getClassName();
|
||||
String testCaseName = Thread.currentThread().getStackTrace()[INDEX_OF_TEST_CLASS].getMethodName();
|
||||
context.set(new MockContext(testClassName, testCaseName));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user