【问题标题】:nullpointer exception for logger记录器的空指针异常
【发布时间】:2013-05-28 06:37:11
【问题描述】:

下面的代码不工作

@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。我还必须涵盖记录器...所以我在测试类的初始化之前放置记录器代码..

  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 

使用我们的记录器模拟代码工作正常..但我也需要工作记录器..

【问题讨论】:

    标签: logging junit4 cobertura spring-test


    【解决方案1】:

    在调试时,反射工具是它产生问题的地方。所以我确实使用了测试类的 setter 方法来注入这些对象并运行它工作正常..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      相关资源
      最近更新 更多