public class MyService {
/**
* method with common method invoke
*/
public String should_mock_common_method() {
return "anything".trim() + "__" + "anything".substring(1, 2) + "__" + "abc".startsWith("ab");
}
}
@MockDiagnose(LogLevel.VERBOSE)
public class MyServiceTest {
MyService myService = new MyService();
public static class Mock {
@MockInvoke(targetClass = String.class)
private String substring(int i, int j) {
return "sub_string";
}
@MockInvoke(targetClass = String.class)
private String trim() {
return "trim_string";
}
@MockInvoke(targetClass = String.class)
private boolean startsWith(String s) {
return false;
}
}
@Test
public void test_should_mock_common_method() {
assertEquals("trim_string__sub_string__false", myService.should_mock_common_method());
verifyInvoked("trim").withTimes(1);
verifyInvoked("sub").withTimes(1);
verifyInvoked("startsWith").withTimes(1);
}
}
运行日志:
[DIAGNOSE] Found test class com/lzb/mock/MyServiceTest
[VERBOSE] Test case "test_should_mock_common_method"
[DIAGNOSE] Found 1 test cases
[WARN] Failed to transform class com/lzb/mock/MyService
[WARN] java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
[WARN] com.alibaba.testable.agent.model.BsmArg.(BsmArg.java:23)
版本号:0.7.5 包路径: com.lzb.mock.MyService com.lzb.mock.MyServiceTest
运行日志: [DIAGNOSE] Found test class com/lzb/mock/MyServiceTest [VERBOSE] Test case "test_should_mock_common_method" [DIAGNOSE] Found 1 test cases [WARN] Failed to transform class com/lzb/mock/MyService [WARN] java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 [WARN] com.alibaba.testable.agent.model.BsmArg.(BsmArg.java:23)