我想使用Powermockito,JUnit和EclEmma。首先我遇到了问题,因为PowermockRunner,EclEmma无法跟踪覆盖范围。我切换到了PowerMock的JUnit规则。现在覆盖范围跟踪工作正常。但是现在我遇到了问题,我无法在Eclipse中进行调试。有时Eclipse显示“无法插入断点缺席行号信息”错误,但大多数断点都被忽略。
这是一个示例测试:
@PrepareForTest({ Slf4jLoggerAdapter.class, LoggerFactory.class })
public class Slf4jLoggerAdapterTest
{
/**
* JUnit Rule
*/
@Rule
public PowerMockRule rule = new PowerMockRule();
/**
* Test Debug Functions
*
* @throws Exception
*/
@Test
public void testDebug() throws Exception
{
LoggerFactory factory = mock(LoggerFactory.class);
PowerMockito.mockStatic(LoggerFactory.class);
Mockito.when(LoggerFactory.getInstance()).thenReturn(factory);
Mockito.when(factory.getLoggerLevel()).thenReturn(LoggerLevel.DEBUG);
Slf4jLoggerAdapter la = PowerMockito.spy(new Slf4jLoggerAdapter("Test"));
Marker marker = MarkerFactory.getMarker("testDebug");
IOException exception = new IOException("Test");
la.debug("Testmessage");
verify(factory).debug(null, "Testmessage", null);
la.debug(marker, "Testmessage");
verify(factory).debug(marker, "Testmessage", null);
la.debug("Testmessage {}", "Test");
verify(factory).log(LoggerLevel.DEBUG, null, "Testmessage Test", null);
la.debug("Testmessage {} {}", "Test", "Test");
verify(factory).log(LoggerLevel.DEBUG, null, "Testmessage Test Test", null);
la.debug("Testmessage", exception);
verify(factory).debug(null, "Testmessage", exception);
la.debug(marker, "Testmessage {}", "Test");
verify(factory).log(LoggerLevel.DEBUG, marker, "Testmessage Test", null);
la.debug(marker, "Testmessage {} {}", "Test", "Test");
verify(factory).log(LoggerLevel.DEBUG, marker, "Testmessage Test Test", null);
assertEquals(Boolean.TRUE, Boolean.valueOf(la.isDebugEnabled()));
assertEquals(Boolean.TRUE, Boolean.valueOf(la.isDebugEnabled(marker)));
}
}