while debug the reflectionutils is the line where it is creating problem.So i did use the setter methods of the testing class to inject these objects and run it is working fine..
nullpointer exception for logger
-
30-05-2022 - |
Question
the below code is not working
@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);
}
it is showing the following error.my aim is to give the cobertura report also along with junits.i have to cover logger also...so i put logger code prior to intialization of the testing class..
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 the code is working fine..but i need to work logger also..
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow