[英]Mockito NullPointerException thrown instead of custom ExitException
我正在嘗試測試ApplicationRunner
類的功能,該類當前依賴於簡單的CLI
類。 我正在嘗試使用ExitException
模擬CLI
並確定引發自定義ExitException
的條件。 (顯示CLI
的help()
方法時)。 我的問題是,測試在parse
函數時會拋出NullPointerException
而不是ExitException
。
測試代碼:
public class ApplicationRunnerTest {
@Mock
private APUCLI cli;
private ApplicationRunner runner;
@Before
public void prepareDependencies(){
MockitoAnnotations.initMocks(this);
runner = new ApplicationRunner();
}
@Test(expected = ExitException.class)
public void verifyHelpOptionsRun() throws Exception{
String[] args = new String[]{};
runner.run(args);
when(cli.parse(args)).thenReturn(null);
verify(cli, times(1)).parse(args);
verify(cli, times(1)).help();
}
}
cli類被嘲笑:
@Component("Cli")
public class APUCLI implements CLI {
@Override
public ParsedArgs parse(String args[]) {
System.out.println("Parsing CLI");
return null;
}
@Override
public void help() throws ExitException{
System.out.println("something");
throw new ExitException(1);
}
}
APU運行程序代碼。
@Component("Runner")
public class ApplicationRunner implements APURunner{
@Inject
@Named("Cli")
private CLI commandLineInterface;
@Override
public void run(String[] args) throws ExitException{
ParsedArgs parsedArgs = getParsedArgs(args);
if(parsedArgs == null){
callHelp();
}
System.out.println("Running");
}
private ParsedArgs getParsedArgs(String[] args){
return commandLineInterface.parse(args);
}
private void callHelp() throws ExitException{
commandLineInterface.help();
}
}
錯誤:
java.lang.Exception: Unexpected exception, expected<Systems.biology.laboratory.ExitException> but was<java.lang.NullPointerException>
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.NullPointerException
at Systems.biology.laboratory.runners.ApplicationRunner.getParsedArgs(ApplicationRunner.java:33)
at Systems.biology.laboratory.runners.ApplicationRunner.run(ApplicationRunner.java:23)
at Systems.biology.laboratory.runners.ApplicationRunnerTest.verifyHelpOptionsRun(ApplicationRunnerTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
... 15 more
您有兩個問題(至少):
1)您模擬一個APUCLI
實例:
@Mock
private APUCLI cli;
但是您永遠不會將其設置為受測試的ApplicationRununer
實例。 因此,在ApplicationRununer
實例中定義的APUCLI
字段為null
。
您可以提供一個允許設置依賴項的構造函數。
因此,您可以替換:
@Inject
@Named("Cli")
private CLI commandLineInterface;
創建人:
private CLI commandLineInterface;
@Inject
public ApplicationRunner(CLI commandLineInterface){
this.commandLineInterface = commandLineInterface;
}
您可以這樣創建它:
@Before
public void prepareDependencies(){
MockitoAnnotations.initMocks(this);
runner = new ApplicationRunner(cli);
}
關於@Named
限定詞,很抱歉,我不確定在帶有javax Inject
注釋的自動裝配構造函數中指定它的方式。
因此,我寧願將這一點放在一邊。
2)在執行要測試的方法后,設置模擬行為。 為時已晚,如果要在執行測試方法時考慮它,則應在完成之前進行。
@Test(expected = ExitException.class)
public void verifyHelpOptionsRun() throws Exception{
String[] args = new String[]{};
// mock record first
when(cli.parse(args)).thenReturn(null);
// execute the method to test
runner.run(args);
// do some checks (if required)
verify(cli, times(1)).parse(args);
verify(cli, times(1)).help();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.