记录器的nullpointer异常

时间:2013-05-28 06:37:11

标签: logging junit4 cobertura spring-test

以下代码无效

@BeforeClass    
public static void setUp() {
            service=new BusManager();
            logger = mock(Logger.class);
            when(logger.isDebugEnabled()).thenReturn(true);
            doAnswer(new MakePersistentCallback()).when(logger).debug(argStr.capture());
            mockStatic(Logger.class);
            when(Logger.getLogger(argClass.capture())).thenReturn(logger);
            entityMngr = mock(IEntityMgr.class);

            applicationBusManager = mock(ApplicationBusManager.class);
            antityManagerFactory = mock(EntityManagerFactory.class);
            applicationEntityMgr=mock(ApplicationEntityMgr.class);
            userApplicationEntityMgr=mock(UserApplicationEntityMgr.class);
            disBusManager=mock(DisBusManager.class);



            ReflectionTestUtils.setField(service,"entityManagerFactory",entityManagerFactory);
            ReflectionTestUtils.setField(service,"applicationBusManager",applicationBusManager); 
            ReflectionTestUtils.setField(service,"disBusManager",disBusManager); 



            when(entityManagerFactory.getEntityManager("applicationEntityMgr")).thenReturn(applicationEntityMgr);

            when(entityManagerFactory.getEntityManager("applicationEntityMgr")).thenReturn(applicationEntityMgr);


        }

它显示以下错误。我的目标是给cobertura报告以及junits.i也必须覆盖记录器...所以我在测试类的初始化之前放置了记录器代码..

  java.lang.NullPointerException
        at org.apache.commons.logging.impl.Log4JLogger.isDebugEnabled(Log4JLogger.java:299)
        at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:109)
        at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:81)
        at 

withour logger mock代码工作正常..但我还需要工作记录器..

1 个答案:

答案 0 :(得分:0)

调试时,reflectionutils是创建问题的行。所以我确实使用测试类的setter方法来注入这些对象并运行它工作正常..