Mockito NullPoInterException抛出而不是自定义ExiteXception



我正在尝试测试当前取决于简单CLI类的ApplicationRunner类的功能。使用Mockito,我正在尝试模拟CLI并确定抛出自定义ExitException的条件。(显示CLI S 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 Runner代码。

@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();
}

相关内容

  • 没有找到相关文章

最新更新