0

我有一个我正在尝试测试的方法,比如说methodToTest()。在这个方法里面是一个我认为是Logger变量的调用,比如:

logger.debug("this is a logger debug example");

问题是 logger 变量是在类之外声明的(它似乎是继承的),我没有它的来源。

当我创建一个测试类的实例,然后调用该methodToTest()方法时,我得到一个NullPointerExceptionatlogger.debug语句。

我怎样才能完全模拟记录器?它在测试本身期间没有用。

when(logger.debug("")).doNothing();

给我一个错误,即使我Logger在测试类中创建了一个变量。

我在想我也许应该使用间谍,但我不确定如何,然后不确定如何处理我目前的when().thenReturn();陈述。

根据@kamil 的建议,

f.set( LM2DImportMultiTaskWorker.class, null );

代码抛出以下错误:

java.lang.IllegalArgumentException: Can not set org.apache.log4j.Logger field com.navteq.cf.foundation.process.OperationSkeleton.logger to java.lang.Class
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
    at java.lang.reflect.Field.set(Unknown Source)
    at com.navteq.rdf.base.task.LM2DImportMultiTaskWorkerTest.testGetCountFromDB(LM2DImportMultiTaskWorkerTest.java:101)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
    at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
    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)
4

2 回答 2

1

您可以像这样使用反射:

Field f = Yourclass.class.getDeclaredField("logger");
f.setAccessible(true);
f.set(Yourclass.class, yourMock );

当你需要搜索你的领域到超类时,你可以做一些查找循环:

Class clazz = Yourclass.class;

while ( clazz != null ) {
    Field f = null;
        try {
            f = clazz.getDeclaredField( "logger" );
        } catch ( NoSuchFieldException e ) {
            clazz = clazz.getSuperclass();
        }
    if ( f != null ) {
        f.setAccessible( true );

        //here we must hack final modifier for field
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);

        //here pass your object which should contain logger field
        f.set( yourObject,  mock(mockSomeClass) );
        break;
    }
}
于 2014-06-30T16:25:53.357 回答
0

假设您可以更改代码,我认为最简单/最干净的方法是将 Logging 接口注入您正在测试的类。然后要么模拟它,要么使用该方法的无操作来制作一个虚假的日志记录实现.debug

于 2014-06-30T16:25:15.483 回答