[英]Spy a method from parent class in a class with injected mocks with Mockito
I'm using Mockito in a Java project with Spring and Struts and I'm having problems with testing actions. 我在使用Spring和Struts的Java项目中使用Mockito,我在测试操作方面遇到了问题。
I'm not using the Struts2 jUnit plugin to save time with the tests using this approach: Strut 2.3.1.2 Unit test, how to remove Spring codependency vs NPE with getContext() . 我没有使用Struts2 jUnit插件来节省使用这种方法的测试时间: Strut 2.3.1.2单元测试,如何使用getContext()删除Spring相关性和NPE 。
The problem is when in my action, when getText() is called I've got a NullPointerException. 问题是在我的操作中,当调用getText()时,我有一个NullPointerException。
I'm trying to spy this method, that's inherited from ActionSupport but I don't find a way because the action is annotated with InjectMocks in the test. 我试图窥探这个方法,这个方法是从ActionSupport继承的,但是我没有找到方法,因为在测试中使用InjectMocks注释了该操作。
Here's a simplied example of the classes: 这是一个简单的类示例:
Parent: 家长:
public class ActionSupport {
public String getText(String aTextName){
return this.getTextProvider().getText(aTextName);
}
}
My action: 我的行动:
public class MyAction extends ActionSupport {
@Autowired private IMyService myService;
public String execute(){
getText("SomeText");
myService.something();
return SUCCESS;
}
}
Test: 测试:
@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
@Mock private IMyService myService;
@InjectMocks private MyAction myAction;
@Before
public void before() {
MockitoAnnotations.initMocks(this);
}
@Test
public void test() {
myAction.execute();
}
}
I'm getting this exception: 我得到了这个例外:
java.lang.NullPointerException
at com.opensymphony.xwork2.util.TextParseUtil.translateVariables(TextParseUtil.java:167)
at com.opensymphony.xwork2.util.TextParseUtil.translateVariables(TextParseUtil.java:126)
at com.opensymphony.xwork2.util.TextParseUtil.translateVariables(TextParseUtil.java:48)
at com.opensymphony.xwork2.util.LocalizedTextUtil.getDefaultMessage(LocalizedTextUtil.java:663)
at com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:534)
at com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:362)
at com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:208)
at com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:123)
at com.opensymphony.xwork2.ActionSupport.getText(ActionSupport.java:103)
at es.MyAction.execute(MyAction.java:148)
And if I annotate MyAction with @Spy maintaining @InjectMocks I've got an StackOverflowError . 如果我使用@Spy维护@InjectMocks来注释MyAction ,我会得到一个StackOverflowError 。
How can I spy only ActionSupport.getText() and let mockito inject mocks on my action? 我怎样才能监视ActionSupport.getText()并让mockito为我的动作注入模拟?
I would avoid using @InjectMocks
as it fails silently. 我会避免使用
@InjectMocks
因为它无声地失败。
Just add a constructor to your MyAction
still using @Autowired
ie constructor injection. 只需使用
@Autowired
即构造函数注入向MyAction
添加构造函数。 This also helps guarantee required dependencies. 这也有助于保证所需的依赖性。
You dont need both initMocks
and MockitoJUnitRunner
. 你不需要
initMocks
和MockitoJUnitRunner
。
public class MyAction extends ActionSupport {
private IMyService myService;
@Autowired
public MyAction(MyService myService) {
this.myService = myService;
}
public String execute(){
getText("SomeText");
myService.something();
return SUCCESS;
}
}
@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
@Mock
private IMyService myService;
private MyAction myAction;
@Before
public void setup() {
myAction = spy(new MyAction(myService));
}
@Test
public void test() {
assertThat(myAction.execute(), equalTo(Action.SUCCESS));
verify(myAction, times(1)).getText();
verify(myService, times(1)).something();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.