在测试方法中模拟logger.debug()调用

时间:2014-06-30 16:15:14

标签: java mockito spy

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

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

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

当我创建测试类的实例,然后调用methodToTest()方法时,我会在NullPointerException语句中获得logger.debug

如何完全模拟记录器?它在测试过程中没有用处。

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

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

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

Per @ 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)

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

假设您可以更改代码,我认为最简单/最干净的方法是将Logging接口注入您正在测试的类。然后模拟它或使用.debug方法的no-op进行伪记录实现。