【问题标题】:Java NullPointerException with Mocked Object带有模拟对象的 Java NullPointerException
【发布时间】:2018-10-10 19:10:58
【问题描述】:

我正在为一个类编写一个单元测试,这个类使用我编写的 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 的新手,因此我们将不胜感激。

【问题讨论】:

    标签: java unit-testing nullpointerexception mocking mockito


    【解决方案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"); 
    

    【讨论】:

    • 谢谢,这就是问题所在。
    【解决方案2】:

    您试图模拟调用 getConfigToml 所返回的方法。也就是说,您需要模拟 getConfigToml 应该返回的对象,然后在该对象上调用 getString。

    【讨论】:

      猜你喜欢
      • 2017-08-21
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多