alibaba / alibaba/testable-mock

直接调用的方法被Mock后不生效,间接调用的生效

Open
#288 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
1.8k
Forks
306
PR merge metrics
No merged PRs in 30d

Description

版本:  0.7.7

**被测试类**
public class MockDemo {
public String callCommonFunc() {
return commonFunc();
}
public String commonFunc(){
return MockDemo.class.getSimpleName();
}
}

**测试类**
@EnablePrivateAccess
class MockDemoTest {
@MockDiagnose(LogLevel.VERBOSE)
public static class Mock {
// @MockInvoke(targetClass = MockDemo.class, targetMethod = "commonFunc")
// public String commonFunc() {
// return "mock";
// }
@MockInvoke
public String commonFunc(MockDemo self) {
System.out.println(self);
return "mock";
}
@MockInvoke(targetClass = MockDemo.class)
public String callCommonFunc() {
return "mock";
}
}
@Test
public void mock(){
MockDemo u = new MockDemo();
assertEquals("mock", u.callCommonFunc());
assertEquals("mock", u.commonFunc());
}
}

**错误日志**
[DIAGNOSE] Found source class io/MockDemo
[VERBOSE] Found method
[VERBOSE] Line 10, constructing "java.lang.Object()"
[VERBOSE] Found method callCommonFunc
[VERBOSE] Line 12, invoking "io.MockDemo::commonFunc() : java.lang.String"
[VERBOSE] Line 12, mock method "commonFunc" used
[VERBOSE] Line 12, invoking "io.MockDemoTest$Mock::commonFunc(io.MockDemo) : java.lang.String"
[VERBOSE] Found method commonFunc
[VERBOSE] Line 16, invoking "java.lang.Class::getSimpleName() : java.lang.String"
[DIAGNOSE] Found mock class io/MockDemoTest$Mock
[VERBOSE] Mock method "commonFunc" as "Lio.MockDemo;::commonFunc() : java.lang.String"
[VERBOSE] Mock method "callCommonFunc" as "io.MockDemo::callCommonFunc() : java.lang.String"
[DIAGNOSE] Found 2 mock methods
io.MockDemo@3d3e5463

org.opentest4j.AssertionFailedError:
Expected :mock
Actual :MockDemo

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by running the mock() test in MockDemoTest with the shown MockDemo and @MockInvoke methods on version 0.7.7. Inspect the verbose @MockDiagnose output for direct and indirect calls, then trace the mocking entry point that handles commonFunc. Done means both assertions pass, including the direct commonFunc() call returning "mock".

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.