alibaba / alibaba/testable-mock

@MockContainer 注解对于继承类的void方法不能正常生效

Open
#322 0 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

被mock类:
/**
* 演示使用@MockContainer注解实现Mock容器类的多重继承
* Demonstrate multiple inherit of mock container class with @MockContainer annotation
*/
public class DemoMultipleInherit {

private String prefix() {
return "ori_";
}

private String middle() {
return "gin";
}

public void ss(){
System.out.println("abd");
}

private String suffix(int some, String more, Object[] parameters) {
return "_al";
}

public String entry() {
return prefix() + middle() + suffix(0, null, null);
}

}

mock类代码:

/**
* 演示使用@MockContainer注解实现Mock容器类的多重继承
* Demonstrate multiple inherit of mock container class with @MockContainer annotation
*/
class DemoMultipleInheritTest {

private DemoMultipleInherit demoMultipleInherit = new DemoMultipleInherit();

public static class PrefixMock {
@MockInvoke(targetClass = DemoMultipleInherit.class)
private String prefix() {
return "in_";
}
}

public static class SuffixMock {
@MockInvoke(targetClass = DemoMultipleInherit.class)
private String suffix(int some, String more, Object[] parameters) {
return "_it";
}

@MockInvoke(targetClass = DemoMultipleInherit.class)
private void ss(){
System.out.println("aaaddd");
}
}

@MockContainer(inherits = { PrefixMock.class, SuffixMock.class })
public static class Mock {
@MockInvoke(targetClass = DemoMultipleInherit.class)
private String middle() {
return "her";
}
}

@Test
public void should_use_mock_method_in_parent_class() {
assertEquals("in_her_it", demoMultipleInherit.entry());
demoMultipleInherit.ss();
verifyInvoked("prefix").withTimes(1);
verifyInvoked("middle").withTimes(1);
verifyInvoked("suffix").withTimes(1);
}
}

异常提示:
java.lang.VerifyError: Operand stack underflow
Exception Details:
Location:
com/alibaba/demo/inherit/DemoMultipleInheritTest$Mock.ss(Lcom/alibaba/demo/inherit/DemoMultipleInherit;)V @7: areturn
Reason:
Attempt to pop empty stack.
Current Frame:
bci: @7
flags: { }
locals: { 'com/alibaba/demo/inherit/DemoMultipleInheritTest$Mock', 'com/alibaba/demo/inherit/DemoMultipleInherit' }
stack: { }
Bytecode:
0x0000000: b800 392b b600 3fb0

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the case in DemoMultipleInheritTest using DemoMultipleInherit, especially the inherited SuffixMock.ss method. Inspect the generated DemoMultipleInheritTest$Mock.ss bytecode and its handling of void methods, then add a regression test showing that ss() runs without VerifyError while the existing mock methods still work.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.