GEODE-2628: fix StatisticsImplTest use of Mockito 2.7.11
Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/c41d788b Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/c41d788b Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/c41d788b Branch: refs/heads/feature/GEODE-3071 Commit: c41d788b0a27993cb609c27b42bfd33303a1e7e3 Parents: 740a4ef Author: Kirk Lund <kl...@apache.org> Authored: Tue Jun 13 10:57:54 2017 -0700 Committer: Kirk Lund <kl...@apache.org> Committed: Wed Jun 14 11:06:32 2017 -0700 ---------------------------------------------------------------------- .../internal/statistics/StatisticsImplTest.java | 63 ++++++++++++-------- 1 file changed, 37 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/c41d788b/geode-core/src/test/java/org/apache/geode/internal/statistics/StatisticsImplTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/internal/statistics/StatisticsImplTest.java b/geode-core/src/test/java/org/apache/geode/internal/statistics/StatisticsImplTest.java index ea8d285..2747252 100644 --- a/geode-core/src/test/java/org/apache/geode/internal/statistics/StatisticsImplTest.java +++ b/geode-core/src/test/java/org/apache/geode/internal/statistics/StatisticsImplTest.java @@ -22,6 +22,7 @@ import java.util.function.IntSupplier; import java.util.function.LongSupplier; import org.apache.logging.log4j.Logger; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -35,27 +36,38 @@ import org.apache.geode.test.junit.categories.UnitTest; */ @Category(UnitTest.class) public class StatisticsImplTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); + private Logger originalLogger; private StatisticsImpl stats; + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void createStats() { - final StatisticsTypeImpl type = mock(StatisticsTypeImpl.class); + originalLogger = StatisticsImpl.logger; + + StatisticsTypeImpl type = mock(StatisticsTypeImpl.class); when(type.getIntStatCount()).thenReturn(5); when(type.getDoubleStatCount()).thenReturn(5); when(type.getLongStatCount()).thenReturn(5); - final String textId = ""; - final long numbericId = 0; - final long uniqueId = 0; - final int osStatFlags = 0; - final boolean atomicIncrements = false; - final StatisticsManager system = mock(StatisticsManager.class); + + String textId = ""; + long numbericId = 0; + long uniqueId = 0; + int osStatFlags = 0; + boolean atomicIncrements = false; + StatisticsManager system = mock(StatisticsManager.class); + stats = new LocalStatisticsImpl(type, textId, numbericId, uniqueId, atomicIncrements, osStatFlags, system); } + @After + public void tearDown() { + StatisticsImpl.logger = originalLogger; + } + @Test public void invokeIntSuppliersShouldUpdateStats() { IntSupplier supplier1 = mock(IntSupplier.class); @@ -108,23 +120,22 @@ public class StatisticsImplTest { @Test public void invokeSuppliersShouldLogErrorOnlyOnce() { - final Logger originalLogger = StatisticsImpl.logger; - try { - final Logger logger = mock(Logger.class); - StatisticsImpl.logger = logger; - IntSupplier supplier1 = mock(IntSupplier.class); - when(supplier1.getAsInt()).thenThrow(NullPointerException.class); - stats.setIntSupplier(4, supplier1); - assertEquals(1, stats.invokeSuppliers()); - verify(logger, times(1)).warn(anyString(), anyString(), anyInt(), - isA(NullPointerException.class)); - assertEquals(1, stats.invokeSuppliers()); - // Make sure the logger isn't invoked again - verify(logger, times(1)).warn(anyString(), anyString(), anyInt(), - isA(NullPointerException.class)); - } finally { - StatisticsImpl.logger = originalLogger; - } + Logger logger = mock(Logger.class); + StatisticsImpl.logger = logger; + IntSupplier supplier1 = mock(IntSupplier.class); + when(supplier1.getAsInt()).thenThrow(NullPointerException.class); + stats.setIntSupplier(4, supplier1); + assertEquals(1, stats.invokeSuppliers()); + + // String message, Object p0, Object p1, Object p2 + verify(logger, times(1)).warn(anyString(), isNull(), anyInt(), + isA(NullPointerException.class)); + + assertEquals(1, stats.invokeSuppliers()); + + // Make sure the logger isn't invoked again + verify(logger, times(1)).warn(anyString(), isNull(), anyInt(), + isA(NullPointerException.class)); } @Test