当整个maven项目在junit测试下运行时,logger.info抛出空指针异常 [英] logger.info is throwing null pointer exception when whole maven project is run under junit test

查看:12
本文介绍了当整个maven项目在junit测试下运行时,logger.info抛出空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是示例代码

class Temp
{
    public static int someMethod()
    {
        Logger logger = LoggerFactory.getLogger(Temp.class);
        logger.info("Some information");//NullPointerException
        return 0;
    }
}

class ClassToTest
{
    public int methodToTest()
    {
        Temp tempInstance = new Temp();
        int i = temp.someMethod();
        return i;
    }
}

class TestAClass
{
    ClassToTest classToTestInstance;
    @Before
    public void setUp()
    {
        classToTestInstance = new ClassToTest();
    }
    @Test
    public void testMethodToTest()
    {
        int i = classToTest.methodToTest();
    }
}

这是一个场景。这个测试用例可能会通过。但在实际代码中,当我运行单独的测试用例或运行整个测试类时,测试用例通过,但当我在junit测试下运行maven项目时,它会失败,并出现NullPointerException。我不能张贴实际的代码,因为它是一个专有代码。如果有人遇到过此类问题,请指点我。

运行实际代码时获得的堆栈跟踪:

java.lang.NullPointerException
    at org.slf4j.impl.Log4jLoggerAdapter.info(Log4jLoggerAdapter.java:304)
    at Temp
    at ClassToTest
    at TestAClass
    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:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at mockit.integration.junit4.internal.JUnit4TestRunnerDecorator.executeTestMethod(JUnit4TestRunnerDecorator.java:142)
    at mockit.integration.junit4.internal.JUnit4TestRunnerDecorator.invokeExplosively(JUnit4TestRunnerDecorator.java:71)
    at mockit.integration.junit4.internal.MockFrameworkMethod.invokeExplosively(MockFrameworkMethod.java:40)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java)
    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.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

推荐答案

此问题是由于loggerTemp类中。如果Temp类的实例是在之前的另一个测试类中创建的,因此它无法加载Temp类的logger,因为它已经被加载。因此,当我们在junit测试下运行整个项目时,它会失败。因为loggerFOR TEMP类在其他测试类中的某个位置已加载。此问题通过mockingloggerJMockit解决。 @次优感谢您抽出时间。

这篇关于当整个maven项目在junit测试下运行时,logger.info抛出空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆