fix mocking bug of static method without parameter

This commit is contained in:
金戟 2020-12-23 21:12:40 +08:00
parent 508610b0c8
commit 6b126223e8
8 changed files with 30 additions and 7 deletions

View File

@ -5,7 +5,8 @@ import com.alibaba.testable.demo.model.Box;
import com.alibaba.testable.demo.model.Color;
/**
* @author flin
* 演示父类变量引用子类对象时的Mock场景
* Demonstrate scenario of mocking method from sub-type object referred by parent-type variable
*/
public class DemoInherit {

View File

@ -9,7 +9,8 @@ import java.util.HashSet;
import java.util.List;
/**
* @author flin
* 演示Mock方法调用校验器
* Demonstrate mock method invocation verifier
*/
public class DemoMatcher {

View File

@ -6,6 +6,10 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
/**
* 演示基本的Mock功能
* Demonstrate basic mock functionality
*/
public class DemoMock {
/**
@ -20,7 +24,7 @@ public class DemoMock {
* method with member method invoke
*/
public String outerFunc(String s) throws Exception {
return "{ \"res\": \"" + innerFunc(s) + "\"}";
return "{ \"res\": \"" + innerFunc(s) + staticFunc() + "\"}";
}
/**
@ -48,6 +52,10 @@ public class DemoMock {
return callFromDifferentMethod();
}
private static String staticFunc() {
return "_STATIC_TAIL";
}
private String innerFunc(String s) throws Exception {
return Files.readAllLines(Paths.get("/a-not-exist-file")).stream().collect(Collectors.joining());
}

View File

@ -1,7 +1,8 @@
package com.alibaba.testable.demo;
/**
* @author flin
* 演示私有成员访问功能
* Demonstrate private member access functionality
*/
public class DemoPrivateAccess {

View File

@ -2,6 +2,10 @@ package com.alibaba.testable.demo;
import java.util.*;
/**
* 演示模板方法的Mock场景
* Demonstrate scenario of mocking template method
*/
public class DemoTemplate {
private <T> List<T> getList(T value) {

View File

@ -30,6 +30,11 @@ class DemoMockTest {
return "mock_" + text;
}
@MockMethod
private String staticFunc(DemoMock self) {
return "_MOCK_TAIL";
}
@MockMethod
private String trim(String self) {
return "trim_string";
@ -70,7 +75,7 @@ class DemoMockTest {
@Test
void should_able_to_mock_member_method() throws Exception {
assertEquals("{ \"res\": \"mock_hello\"}", demoMock.outerFunc("hello"));
assertEquals("{ \"res\": \"mock_hello_MOCK_TAIL\"}", demoMock.outerFunc("hello"));
verify("innerFunc").with("hello");
}

View File

@ -9,8 +9,8 @@ import java.util.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* 演示模板方法可以被Mock
* Demonstrate template method can be mocked
* 演示模板方法的Mock场景
* Demonstrate scenario of mocking template method
*/
class DemoTemplateTest {

View File

@ -122,6 +122,9 @@ public class SourceClassHandler extends BaseClassHandler {
private int getMemberMethodStart(AbstractInsnNode[] instructions, int rangeEnd) {
int stackLevel = getInitialStackLevel((MethodInsnNode)instructions[rangeEnd]);
if (stackLevel < 0) {
return rangeEnd;
}
for (int i = rangeEnd - 1; i >= 0; i--) {
stackLevel += getStackLevelChange(instructions[i]);
if (stackLevel < 0) {