alibaba / testable-mock

换种思路写Mock,让单元测试更简单
https://alibaba.github.io/testable-mock/
MIT License
1.83k stars 310 forks source link

报错:com.alibaba.testable.agent.model.BsmArg.<init>(BsmArg.java:23) #271

Closed lizebin0918 closed 2 years ago

lizebin0918 commented 2 years ago

版本号:0.7.5 包路径: com.lzb.mock.MyService com.lzb.mock.MyServiceTest

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)

liudaolunboluo commented 2 years ago

如果是高于JDK9的版本可以见这个https://github.com/alibaba/testable-mock/issues/272