diff --git a/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoMockTest.java b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoMockTest.java index 7a1667b..c4e6b78 100644 --- a/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoMockTest.java +++ b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoMockTest.java @@ -1,10 +1,10 @@ package com.alibaba.testable.demo; import com.alibaba.testable.core.annotation.TestableMock; +import com.alibaba.testable.core.tool.TestableConst; import com.alibaba.testable.demo.model.BlackBox; import org.junit.jupiter.api.Test; -import java.io.IOException; import java.util.concurrent.Executors; import static com.alibaba.testable.core.matcher.InvokeVerifier.verify; @@ -19,7 +19,7 @@ class DemoMockTest { private DemoMock demoMock = new DemoMock(); - @TestableMock(targetMethod = CONSTRUCTOR) + @TestableMock(targetMethod = TestableConst.CONSTRUCTOR) private BlackBox createBlackBox(String text) { return new BlackBox("mock_" + text); } diff --git a/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoTemplateTest.java b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoTemplateTest.java index 27bb2fd..e185efd 100644 --- a/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoTemplateTest.java +++ b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoTemplateTest.java @@ -1,7 +1,7 @@ package com.alibaba.testable.demo; import com.alibaba.testable.core.annotation.TestableMock; -import com.alibaba.testable.core.tool.TestableTool; +import com.alibaba.testable.core.tool.TestableConst; import org.junit.jupiter.api.Test; import java.util.*; @@ -29,7 +29,7 @@ class DemoTemplateTest { return new HashMap() {{ put(key, (V)(value.toString() + "_mock_map")); }}; } - @TestableMock(targetMethod = TestableTool.CONSTRUCTOR) + @TestableMock(targetMethod = TestableConst.CONSTRUCTOR) private HashSet newHashSet() { HashSet set = new HashSet<>(); set.add((T)"insert_mock"); @@ -55,7 +55,7 @@ class DemoTemplateTest { // return new HashMap() {{ put(key, value.toString() + "_mock_map"); }}; //} // - //@TestableMock(targetMethod = TestableTool.CONSTRUCTOR) + //@TestableMock(targetMethod = TestableConst.CONSTRUCTOR) //private HashSet newHashSet() { // HashSet set = new HashSet<>(); // set.add("insert_mock"); diff --git a/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/DemoMockTest.kt b/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/DemoMockTest.kt index 8b92f04..dd495fb 100644 --- a/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/DemoMockTest.kt +++ b/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/DemoMockTest.kt @@ -2,6 +2,7 @@ package com.alibaba.testable.demo import com.alibaba.testable.core.annotation.TestableMock import com.alibaba.testable.core.matcher.InvokeVerifier.verify +import com.alibaba.testable.core.tool.TestableConst import com.alibaba.testable.core.tool.TestableTool.* import com.alibaba.testable.demo.model.BlackBox import com.alibaba.testable.demo.model.ColorBox @@ -15,7 +16,7 @@ import java.util.concurrent.Executors */ internal class DemoMockTest { - @TestableMock(targetMethod = CONSTRUCTOR) + @TestableMock(targetMethod = TestableConst.CONSTRUCTOR) private fun createBlackBox(text: String) = BlackBox("mock_$text") @TestableMock diff --git a/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/DemoTemplateTest.kt b/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/DemoTemplateTest.kt index 0f37974..26808fc 100644 --- a/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/DemoTemplateTest.kt +++ b/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/DemoTemplateTest.kt @@ -3,7 +3,7 @@ package com.alibaba.testable.demo import com.alibaba.testable.core.annotation.MockWith import com.alibaba.testable.core.annotation.TestableMock import com.alibaba.testable.core.model.MockDiagnose -import com.alibaba.testable.core.tool.TestableTool +import com.alibaba.testable.core.tool.TestableConst import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test import java.util.* @@ -27,7 +27,7 @@ internal class DemoTemplateTest { return mutableMapOf(key to (value.toString() + "_mock_map") as V) } - @TestableMock(targetMethod = TestableTool.CONSTRUCTOR) + @TestableMock(targetMethod = TestableConst.CONSTRUCTOR) private fun newHashSet(): HashSet<*> { val set = HashSet() set.add("insert_mock") diff --git a/docs/zh-cn/doc/use-mock.md b/docs/zh-cn/doc/use-mock.md index 2bbc564..26f8195 100644 --- a/docs/zh-cn/doc/use-mock.md +++ b/docs/zh-cn/doc/use-mock.md @@ -87,8 +87,8 @@ private BlackBox secretBox(BlackBox ignore) { ```java // 要覆写的构造函数签名为`BlackBox(String)` // 无需在Mock方法参数列表增加额外参数,由于使用了`targetMethod`参数,Mock方法的名称随意起 -// 此处的`CONSTRUCTOR`为`TestableTool`辅助类提供的常量,值为"" -@TestableMock(targetMethod = CONSTRUCTOR) +// 此处的`TestableConst.CONSTRUCTOR`为`TestableMock`提供的辅助常量,值为"" +@TestableMock(targetMethod = TestableConst.CONSTRUCTOR) private BlackBox createBlackBox(String text) { return new BlackBox("mock_" + text); } diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/constant/ConstPool.java b/testable-agent/src/main/java/com/alibaba/testable/agent/constant/ConstPool.java index 150ee7c..8b0786a 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/constant/ConstPool.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/constant/ConstPool.java @@ -8,7 +8,6 @@ public class ConstPool { public static final String DOT = "."; public static final String SLASH = "/"; - public static final String CONSTRUCTOR = ""; public static final String TEST_POSTFIX = "Test"; public static final String TESTABLE_INJECT_REF = "_testableInternalRef"; diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java index cd84836..d8a9656 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java @@ -4,6 +4,7 @@ import com.alibaba.testable.agent.constant.ConstPool; import com.alibaba.testable.agent.model.MethodInfo; import com.alibaba.testable.agent.util.BytecodeUtil; import com.alibaba.testable.agent.util.ClassUtil; +import com.alibaba.testable.core.tool.TestableConst; import com.alibaba.testable.core.util.LogUtil; import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.*; @@ -39,7 +40,7 @@ public class SourceClassHandler extends BaseClassHandler { Set memberInjectMethods = new HashSet(); Set newOperatorInjectMethods = new HashSet(); for (MethodInfo mi : injectMethods) { - if (mi.getName().equals(ConstPool.CONSTRUCTOR)) { + if (mi.getName().equals(TestableConst.CONSTRUCTOR)) { newOperatorInjectMethods.add(mi); } else { memberInjectMethods.add(mi); @@ -68,7 +69,7 @@ public class SourceClassHandler extends BaseClassHandler { node.owner, node.getOpcode(), rangeStart, i); i = rangeStart; } - } else if (ConstPool.CONSTRUCTOR.equals(node.name)) { + } else if (TestableConst.CONSTRUCTOR.equals(node.name)) { // it's a new operation String newOperatorInjectMethodName = getNewOperatorInjectMethodName(newOperatorInjectMethods, node); if (newOperatorInjectMethodName != null) { @@ -126,7 +127,7 @@ public class SourceClassHandler extends BaseClassHandler { case Opcodes.INVOKEVIRTUAL: case Opcodes.INVOKEINTERFACE: stackLevel += stackEffectOfInvocation(instructions[i]) + 1; - if (((MethodInsnNode)instructions[i]).name.equals(ConstPool.CONSTRUCTOR)) { + if (((MethodInsnNode)instructions[i]).name.equals(TestableConst.CONSTRUCTOR)) { // constructor must be INVOKESPECIAL and implicitly pop 1 more stack stackLevel++; } diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java index 4c8f21d..ebcffe0 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java @@ -4,6 +4,7 @@ import com.alibaba.testable.agent.constant.ConstPool; import com.alibaba.testable.agent.tool.ImmutablePair; import com.alibaba.testable.agent.util.AnnotationUtil; import com.alibaba.testable.agent.util.ClassUtil; +import com.alibaba.testable.core.tool.TestableConst; import org.objectweb.asm.tree.*; import java.util.ArrayList; @@ -133,7 +134,7 @@ public class TestClassHandler extends BaseClassHandler { for (AnnotationNode an : mn.visibleAnnotations) { String method = AnnotationUtil.getAnnotationParameter (an, ConstPool.FIELD_TARGET_METHOD, null, String.class); - if (ConstPool.CONSTRUCTOR.equals(method)) { + if (TestableConst.CONSTRUCTOR.equals(method)) { return true; } } diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java index 7a7eb1e..dfab51d 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java @@ -9,6 +9,7 @@ import com.alibaba.testable.agent.model.MethodInfo; import com.alibaba.testable.agent.tool.ComparableWeakRef; import com.alibaba.testable.agent.util.AnnotationUtil; import com.alibaba.testable.agent.util.ClassUtil; +import com.alibaba.testable.core.tool.TestableConst; import com.alibaba.testable.core.util.LogUtil; import com.alibaba.testable.core.model.MockDiagnose; import org.objectweb.asm.ClassReader; @@ -98,7 +99,7 @@ public class TestableClassTransformer implements ClassFileTransformer { String targetClass = ClassUtil.toSlashSeparateFullClassName(methodDescPair.left); String targetMethod = AnnotationUtil.getAnnotationParameter( an, ConstPool.FIELD_TARGET_METHOD, mn.name, String.class); - if (targetMethod.equals(ConstPool.CONSTRUCTOR)) { + if (targetMethod.equals(TestableConst.CONSTRUCTOR)) { String sourceClassName = ClassUtil.getSourceClassName(cn.name); methodInfos.add(new MethodInfo(sourceClassName, targetMethod, mn.name, mn.desc)); } else { diff --git a/testable-core/src/main/java/com/alibaba/testable/core/tool/TestableConst.java b/testable-core/src/main/java/com/alibaba/testable/core/tool/TestableConst.java new file mode 100644 index 0000000..567d713 --- /dev/null +++ b/testable-core/src/main/java/com/alibaba/testable/core/tool/TestableConst.java @@ -0,0 +1,13 @@ +package com.alibaba.testable.core.tool; + +/** + * @author flin + */ +public class TestableConst { + + /** + * Name of the constructor method + */ + public static final String CONSTRUCTOR = ""; + +} diff --git a/testable-core/src/main/java/com/alibaba/testable/core/tool/TestableTool.java b/testable-core/src/main/java/com/alibaba/testable/core/tool/TestableTool.java index 47c5d38..126bae6 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/tool/TestableTool.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/tool/TestableTool.java @@ -5,11 +5,6 @@ package com.alibaba.testable.core.tool; */ public class TestableTool { - /** - * Name of the constructor method - */ - public static final String CONSTRUCTOR = ""; - /** * Name of current test case method */