remove diagnose parameter in MockWith annotation

This commit is contained in:
金戟 2021-03-26 20:20:45 +08:00
parent 00055a4626
commit 20b346ed34
5 changed files with 13 additions and 26 deletions

View File

@ -3,12 +3,14 @@ Self-Help Troubleshooting
Compared with `Mockito` and other mock tools where developers have to manually inject mock classes, `TestableMock` uses method name and parameter type matching to automatically find invocations that require mock. While this mechanism brings convenience, it may also cause unexpected mock replacement.
To troubleshoot mock-related issues, just add the `@MockWith` annotation to the test class, and configure the parameter `diagnose` to `MockDiagnose.ENABLE`, so the detailed mock method replacement process will be printed when the test is run.
To troubleshoot mock-related issues, just add the `@MockDiagnose` annotation to the mock class, and set the value `LogLevel.ENABLE`, so the detailed mock method replacement process will be printed when the test is run.
```java
@MockWith(diagnose = MockDiagnose.ENABLE)
class DemoTest {
...
@MockDiagnose(LogLevel.ENABLE)
public static class Mock {
...
}
}
```
@ -38,15 +40,19 @@ The log shows all the mocked invocation and corresponding code line numbers in t
- Self troubleshooting:
- If there is no output, please check whether the `pom.xml` or `build.gradle` configuration correctly introduces `TestableMock` dependencies
- If only the first line of `Handling test class` is output, please check whether the test class is in the same package of the class under test, and the name is "<ClassUnderTest>+Test" (required for `0.4.x` version)
- If only the first line of `Handling mock class` is output, please check whether the mock class is created at correct place
- If `Handling mock class` and `Handling test class` are output, please check whether the test class is in the same package of the class under test, and the name is "<ClassUnderTest>+Test", otherwise `@MockWith` annotation should be used
- If `Handling source class` and `Handling method xxx` are output, but there is no mock replacement happen at the expected code line, please check whether the mock method definition matches the target method
For situations where expected mocking is not take effect, you could set the diagnosis level to `MockDiagnose.VERBOSE` for further investigation information.
For situations where expected mocking is not take effect, you could set the diagnosis level to `LogLevel.VERBOSE` for further investigation information.
```java
@MockWith(diagnose = MockDiagnose.VERBOSE)
class DemoTest {
...
@MockDiagnose(LogLevel.VERBOSE)
public static class Mock {
...
}
}
```

View File

@ -91,5 +91,3 @@ class DemoTest {
- `Mock method "<Mock方法名>" as "<方法签名>"` 在测试类中扫描到的**普通Mock方法**及其签名
- `Line XX, constructing "<类型>" as "<方法签名>"` 在被测类中扫描掉的**构造方法调用**及其签名
- `Line XX, invoking "<方法名>" as "<方法签名>"` 在被测类中扫描到的**成员方法调用**及其签名
> 在`0.4.x`版本使用测试类添加`@MockWith`注解的`diagnose`参数来启用诊断信息的方法在`0.5`版本中依然可用,但将在未来版本中移除,请优先使用`@MockDiagnose`注解替代。

View File

@ -229,7 +229,6 @@ public class TestableClassTransformer implements ClassFileTransformer {
private String parseMockWithAnnotation(ClassNode cn, ClassType expectedType) {
if (cn.visibleAnnotations != null) {
for (AnnotationNode an : cn.visibleAnnotations) {
DiagnoseUtil.setupByAnnotation(an);
if (toJavaStyleClassName(an.desc).equals(ConstPool.MOCK_WITH)) {
ClassType type = AnnotationUtil.getAnnotationParameter(an, FIELD_TREAT_AS, ClassType.GuessByName,
ClassType.class);

View File

@ -6,13 +6,11 @@ import com.alibaba.testable.core.util.LogUtil;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import static com.alibaba.testable.agent.constant.ConstPool.MOCK_WITH;
import static com.alibaba.testable.agent.util.ClassUtil.toJavaStyleClassName;
public class DiagnoseUtil {
private static final String FIELD_VALUE = "value";
private static final String FIELD_DIAGNOSE = "diagnose";
public static void setupByClass(ClassNode cn) {
if (cn == null || cn.visibleAnnotations == null) {
@ -25,13 +23,6 @@ public class DiagnoseUtil {
}
}
public static void setupByAnnotation(AnnotationNode an) {
// to be remove in v0.6
if (toJavaStyleClassName(an.desc).equals(MOCK_WITH)) {
setupDiagnose(an, FIELD_DIAGNOSE);
}
}
private static void setupDiagnose(AnnotationNode an, String fieldDiagnose) {
LogLevel level = AnnotationUtil.getAnnotationParameter(an, fieldDiagnose, null, LogLevel.class);
if (level != null) {

View File

@ -28,11 +28,4 @@ public @interface MockWith {
*/
ClassType treatAs() default ClassType.GuessByName;
/**
* switch of mock diagnose information of current test class
* @deprecated to be removed in v0.6, use @MockDiagnose annotation instead
* @return enable or disable
*/
LogLevel diagnose() default LogLevel.DISABLE;
}