Created
May 11, 2017 20:11
-
-
Save javathought/e925f3846a3b1d8ea4ece7609937f250 to your computer and use it in GitHub Desktop.
unit test Log calls in Java with PowerMock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* call logs | |
*/ | |
public class LogCaller { | |
private static Logger LOG = LoggerFactory.getLogger(LogCaller.class); | |
public void testLogging() { | |
LOG.info("Hello World"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.InjectMocks; | |
import org.powermock.core.classloader.annotations.PrepareForTest; | |
import org.powermock.modules.junit4.PowerMockRunner; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import static org.mockito.Matchers.any; | |
import static org.mockito.Mockito.verify; | |
import static org.mockito.Mockito.when; | |
import static org.powermock.api.mockito.PowerMockito.mock; | |
import static org.powermock.api.mockito.PowerMockito.mockStatic; | |
/** | |
* Test log is called | |
*/ | |
@RunWith(PowerMockRunner.class) | |
@PrepareForTest({LoggerFactory.class}) | |
public class LogCallerTest { | |
private static Logger mockLOG; | |
@InjectMocks | |
LogCaller underTest; | |
@BeforeClass | |
public static void setup() { | |
mockStatic(LoggerFactory.class); | |
mockLOG = mock(Logger.class); | |
when(LoggerFactory.getLogger(any(Class.class))).thenReturn(mockLOG); | |
} | |
@Test | |
public void testLogging() { | |
underTest.testLogging(); | |
verify(mockLOG).info("Hello World"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cheers