Skip to content

Instantly share code, notes, and snippets.

@evonsdesigns
Created March 13, 2019 16:02
Show Gist options
  • Save evonsdesigns/a841aeb4befa1f9ba3c8c7135a938c6f to your computer and use it in GitHub Desktop.
Save evonsdesigns/a841aeb4befa1f9ba3c8c7135a938c6f to your computer and use it in GitHub Desktop.
package examples;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@RunWith(PowerMockRunner.class)
@PrepareForTest({LoggerFactory.class})
public class YourClassWithLogsTest {
@Before
public void hideLogMessages() {
mockStatic(LoggerFactory.class);
Logger logger = PowerMockito.mock(Logger.class);
PowerMockito.when(LoggerFactory.getLogger(YourClassWithLogs.class)).thenReturn(logger);
}
@Test
public void testMyMethod() {
// ...
}
// Rest of your tests
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment