带有模拟对象的Java NullPointerException

时间:2018-10-10 19:10:59

标签: java unit-testing nullpointerexception mocking mockito

我正在为一个类编写单元测试,该类使用我编写的ConfigurationManager从TOML对象(因此从TOML文件)获取配置。

代码

这是相关的ConfigurationManager代码:

public class ConfigurationManager {
    // DEFAULT_LOGGING_LEVEL should represent level to use in production environment.
    private final static Level DEFAULT_LOGGING_LEVEL = Level.FINE;
    private final static String LOG_LEVEL_ENV_PROPERTY = "LOG_LEVEL";
    private final static String TOML_FILE_NAME = "config.toml";
    private final static String LOCAL_ENV_PROPERTY = "local";
    private final static String ENV_PROPERTY = "MY_ENV";
    private static Logger CM_LOGGER;

    private Environment environment;

    public ConfigurationManager() {
        environment = new Environment();
        CM_LOGGER = new LoggerUtil(ConfigurationManager.class.getName(), getLoggingLevel()).getLogger();
    }

    public File getTomlFile() throws URISyntaxException, FileNotFoundException {
        URI uri = getConfigURI();
        File tomlFile = new File(uri);
        if (!tomlFile.exists()) {
            String err = TOML_FILE_NAME + " does not exist!";
            CM_LOGGER.severe(err);
            throw new FileNotFoundException(err);
        }
        return tomlFile;
    }

    /**
     * @return A URI representing the path to the config
     * @throws URISyntaxException if getConfigURI encounters bad URI syntax.
     */
    private URI getConfigURI() throws URISyntaxException {
        return getClass().getClassLoader().getResource(TOML_FILE_NAME).toURI();
    }

    /**
     * Method for getting the app configuration as a toml object.
     *
     * @return A toml object built from the config.toml file.
     * @throws URISyntaxException    if getConfigURI encounters bad URI syntax.
     * @throws FileNotFoundException if getTomlFile can't find the config.toml file.
     */
    public Toml getConfigToml() throws URISyntaxException, FileNotFoundException {
        return new Toml().read(getTomlFile());
    }
}

这是调用此配置管理器的代码:

public class Listener {
    // Initializations
    private static final String CONFIG_LISTENER_THREADS = "listenerThreads";
    private static final String DEFAULT_LISTENER_THREADS = "1";

    /**
     * Constructor for getting app properties and initializing executor service with thread pool based on app prop.
     */
    @PostConstruct
    void init() throws FileNotFoundException, URISyntaxException {
        ConfigurationManager configurationManager = new ConfigurationManager();
            int listenerThreads = Integer.parseInt(configurationManager.getConfigToml()
                    .getString(CONFIG_LISTENER_THREADS, DEFAULT_LISTENER_THREADS));  
        this.executorService = Executors.newFixedThreadPool(listenerThreads);
        LOGGER.config("Listener executorService threads: " + listenerThreads);
    }
...
}

这是该代码的测试(请参见注释以查看在何处触发NPE)

public class ListenerTests {

    @Mock
    ConfigurationManager mockCM;

    @InjectMocks
    Listener listener = new Listener();

    @Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testListener_ShouldInit() throws FileNotFoundException, URISyntaxException {
        when(mockCM.getConfigToml().getString(any(), any())).thenReturn("5"); // !!!NPE TRIGGERED BY THIS LINE!!!
        listener.init();
        verify(mockCM, times(1)).getConfigToml().getString(any(), any());
    }
}

问题

我得到一个NullPointerException,如下所示

java.lang.NullPointerException
    at com.bose.source_account_listener.ListenerTests.testListener_ShouldInit(ListenerTests.java:37)
    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.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)

我对这里的问题有一个猜测,但是我不确定如何验证我的猜测。我的猜测是我正在模拟ConfigurationManager,但是模拟不知道ConfigurationManager可以生成具有getString方法的TOML文件,因此最终得到了NPE。这使我认为我的模拟ConfigurationManager需要模拟TOML,但是我不确定是这种情况还是正确的解决方案。

一般来说,我对Mockito和Java还是陌生的,所以将不胜感激。

2 个答案:

答案 0 :(得分:1)

您需要模拟通话的每一部分,而不仅仅是完整通话。 mockCM.getConfigToml()没有模拟响应,因此它返回null,并在null对象上调用toString。您可以选择返回非模拟的“ Toml”或模拟Toml,然后使用when配置对其进行设置。

Toml tomlMock = Mockito.mock(Toml.class);
when(mockCM.getConfigToml()).thenReturn(tmolMock);
when(tmolMock.getString(any(), any())).thenReturn("5"); 

答案 1 :(得分:1)

您正试图模拟通过调用getConfigToml返回的方法。也就是说,您需要模拟对象getConfigToml返回的内容,然后对该对象调用getString。