mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-02-10 20:00:18 +08:00
fix mocking bug of static method without parameter
This commit is contained in:
parent
508610b0c8
commit
6b126223e8
@ -5,7 +5,8 @@ import com.alibaba.testable.demo.model.Box;
|
|||||||
import com.alibaba.testable.demo.model.Color;
|
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 {
|
public class DemoInherit {
|
||||||
|
|
||||||
|
@ -9,7 +9,8 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author flin
|
* 演示Mock方法调用校验器
|
||||||
|
* Demonstrate mock method invocation verifier
|
||||||
*/
|
*/
|
||||||
public class DemoMatcher {
|
public class DemoMatcher {
|
||||||
|
|
||||||
|
@ -6,6 +6,10 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示基本的Mock功能
|
||||||
|
* Demonstrate basic mock functionality
|
||||||
|
*/
|
||||||
public class DemoMock {
|
public class DemoMock {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -20,7 +24,7 @@ public class DemoMock {
|
|||||||
* method with member method invoke
|
* method with member method invoke
|
||||||
*/
|
*/
|
||||||
public String outerFunc(String s) throws Exception {
|
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();
|
return callFromDifferentMethod();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String staticFunc() {
|
||||||
|
return "_STATIC_TAIL";
|
||||||
|
}
|
||||||
|
|
||||||
private String innerFunc(String s) throws Exception {
|
private String innerFunc(String s) throws Exception {
|
||||||
return Files.readAllLines(Paths.get("/a-not-exist-file")).stream().collect(Collectors.joining());
|
return Files.readAllLines(Paths.get("/a-not-exist-file")).stream().collect(Collectors.joining());
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
package com.alibaba.testable.demo;
|
package com.alibaba.testable.demo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author flin
|
* 演示私有成员访问功能
|
||||||
|
* Demonstrate private member access functionality
|
||||||
*/
|
*/
|
||||||
public class DemoPrivateAccess {
|
public class DemoPrivateAccess {
|
||||||
|
|
||||||
|
@ -2,6 +2,10 @@ package com.alibaba.testable.demo;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示模板方法的Mock场景
|
||||||
|
* Demonstrate scenario of mocking template method
|
||||||
|
*/
|
||||||
public class DemoTemplate {
|
public class DemoTemplate {
|
||||||
|
|
||||||
private <T> List<T> getList(T value) {
|
private <T> List<T> getList(T value) {
|
||||||
|
@ -30,6 +30,11 @@ class DemoMockTest {
|
|||||||
return "mock_" + text;
|
return "mock_" + text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MockMethod
|
||||||
|
private String staticFunc(DemoMock self) {
|
||||||
|
return "_MOCK_TAIL";
|
||||||
|
}
|
||||||
|
|
||||||
@MockMethod
|
@MockMethod
|
||||||
private String trim(String self) {
|
private String trim(String self) {
|
||||||
return "trim_string";
|
return "trim_string";
|
||||||
@ -70,7 +75,7 @@ class DemoMockTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_member_method() throws Exception {
|
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");
|
verify("innerFunc").with("hello");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,8 +9,8 @@ import java.util.*;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 演示模板方法可以被Mock
|
* 演示模板方法的Mock场景
|
||||||
* Demonstrate template method can be mocked
|
* Demonstrate scenario of mocking template method
|
||||||
*/
|
*/
|
||||||
class DemoTemplateTest {
|
class DemoTemplateTest {
|
||||||
|
|
||||||
|
@ -122,6 +122,9 @@ public class SourceClassHandler extends BaseClassHandler {
|
|||||||
|
|
||||||
private int getMemberMethodStart(AbstractInsnNode[] instructions, int rangeEnd) {
|
private int getMemberMethodStart(AbstractInsnNode[] instructions, int rangeEnd) {
|
||||||
int stackLevel = getInitialStackLevel((MethodInsnNode)instructions[rangeEnd]);
|
int stackLevel = getInitialStackLevel((MethodInsnNode)instructions[rangeEnd]);
|
||||||
|
if (stackLevel < 0) {
|
||||||
|
return rangeEnd;
|
||||||
|
}
|
||||||
for (int i = rangeEnd - 1; i >= 0; i--) {
|
for (int i = rangeEnd - 1; i >= 0; i--) {
|
||||||
stackLevel += getStackLevelChange(instructions[i]);
|
stackLevel += getStackLevelChange(instructions[i]);
|
||||||
if (stackLevel < 0) {
|
if (stackLevel < 0) {
|
||||||
|
Loading…
Reference in New Issue
Block a user