ile final sınıfının alt sınıfını kodlayamıyorum Kodumu test etmek için JUNIT with Powermockito ve EclEmma kullanıyorum. Şimdi bir sorun var. İşte PowerMockRule
public class Main
{
private static final Logger slf4jLogger = LoggerFactory.getLogger(Main.class);
private static final Marker marker = MarkerFactory.getMarker("Test");
/**
* @param args
*/
public static void main(String[] args)
{
System.out.println(slf4jLogger);
slf4jLogger.debug(marker, "Hallo");
slf4jLogger.trace(marker, "Hallo");
slf4jLogger.info(marker, "Hallo");
slf4jLogger.warn(marker, "Hallo");
slf4jLogger.error(marker, "Hallo");
}
}
benim Testi: Bu Class test etmek zorunda
@PrepareForTest({ LoggerFactory.class, Main.class })
public class MainTest
{
@Rule
public PowerMockRule rule = new PowerMockRule();
@Rule
public ExpectedException thrown = ExpectedException.none();
/**
* Test static Fields
*
* @throws Exception
*/
@Test
public void testMain() throws Exception
{
Logger logger = PowerMockito.mock(Logger.class);
PowerMockito.mockStatic(LoggerFactory.class);
PowerMockito.when(LoggerFactory.getLogger(Mockito.any(Class.class))).thenReturn(logger);
Main.main(null);
Mockito.verify(logger).debug(Mockito.any(Marker.class), Mockito.eq("Hallo"));
Mockito.verify(logger).trace(Mockito.any(Marker.class), Mockito.eq("Hallo"));
Mockito.verify(logger).info(Mockito.any(Marker.class), Mockito.eq("Hallo"));
Mockito.verify(logger).warn(Mockito.any(Marker.class), Mockito.eq("Hallo"));
Mockito.verify(logger).error(Mockito.any(Marker.class), Mockito.eq("Hallo"));
}
}
Ama şu Hata vardır:
java.lang.IllegalArgumentException: Cannot subclass final class class org.slf4j.LoggerFactory at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447) at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217) at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378) at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318) at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:123) at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:57) at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111) at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:59) at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70) ...
Ben powermock-modülüne istemiyoruz -javaagent. Bunu yapmanın bir yolu var mı?