gpt4 book ai didi

java - 抛出 Mockito NullPointerException 而不是自定义 ExitException

转载 作者:行者123 更新时间:2023-12-02 02:11:21 27 4
gpt4 key购买 nike

我正在尝试测试 ApplicationRunner 类的功能,该类当前依赖于一个简单的 CLI 类。使用mockito,我尝试模拟CLI 并确定引发自定义ExitException 的条件。 (当显示 CLIhelp() 方法时)。我的问题是,当测试使用 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 注释的 Autowiring 构造函数中指定它。
所以我宁愿把这一点放在一边。

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

关于java - 抛出 Mockito NullPointerException 而不是自定义 ExitException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49996832/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com