From bd9a87c137477b26047f9e033e5fa6f049a534ba Mon Sep 17 00:00:00 2001 From: Aprian Diaz Novandi Date: Wed, 31 Jan 2018 15:58:52 +0100 Subject: [PATCH] Simplify unit test logic (#3548) --- .../baeldung/guava/GuavaMemoizerUnitTest.java | 60 +++++++++---------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java b/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java index 0ae1f438e3..1f934347b4 100644 --- a/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java @@ -38,7 +38,7 @@ public class GuavaMemoizerUnitTest { int n = 95; // when - BigInteger factorial = new Factorial().getFactorial(n); + BigInteger factorial = Factorial.getFactorial(n); // then BigInteger expectedFactorial = new BigInteger("10329978488239059262599702099394727095397746340117372869212250571234293987594703124871765375385424468563282236864226607350415360000000000000000000000"); @@ -47,52 +47,46 @@ public class GuavaMemoizerUnitTest { @Test public void givenMemoizedSupplier_whenGet_thenSubsequentGetsAreFast() { + // given Supplier memoizedSupplier; memoizedSupplier = Suppliers.memoize(CostlySupplier::generateBigNumber); - Instant start = Instant.now(); - BigInteger bigNumber = memoizedSupplier.get(); - Long durationInMs = Duration.between(start, Instant.now()).toMillis(); - assertThat(bigNumber, is(equalTo(new BigInteger("12345")))); - assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D))); + // when + BigInteger expectedValue = new BigInteger("12345"); + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D); - start = Instant.now(); - bigNumber = memoizedSupplier.get().add(BigInteger.ONE); - durationInMs = Duration.between(start, Instant.now()).toMillis(); - assertThat(bigNumber, is(equalTo(new BigInteger("12346")))); - assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D))); - - start = Instant.now(); - bigNumber = memoizedSupplier.get().add(BigInteger.TEN); - durationInMs = Duration.between(start, Instant.now()).toMillis(); - assertThat(bigNumber, is(equalTo(new BigInteger("12355")))); - assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D))); + // then + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D); + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D); } @Test public void givenMemoizedSupplierWithExpiration_whenGet_thenSubsequentGetsBeforeExpiredAreFast() throws InterruptedException { + // given Supplier memoizedSupplier; memoizedSupplier = Suppliers.memoizeWithExpiration(CostlySupplier::generateBigNumber, 3, TimeUnit.SECONDS); - Instant start = Instant.now(); - BigInteger bigNumber = memoizedSupplier.get(); - Long durationInMs = Duration.between(start, Instant.now()).toMillis(); - assertThat(bigNumber, is(equalTo(new BigInteger("12345")))); - assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D))); - - start = Instant.now(); - bigNumber = memoizedSupplier.get().add(BigInteger.ONE); - durationInMs = Duration.between(start, Instant.now()).toMillis(); - assertThat(bigNumber, is(equalTo(new BigInteger("12346")))); - assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D))); + // when + BigInteger expectedValue = new BigInteger("12345"); + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D); + // then + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D); + // add one more second until memoized Supplier is evicted from memory TimeUnit.SECONDS.sleep(1); + assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D); + } - start = Instant.now(); - bigNumber = memoizedSupplier.get().add(BigInteger.TEN); - durationInMs = Duration.between(start, Instant.now()).toMillis(); - assertThat(bigNumber, is(equalTo(new BigInteger("12355")))); - assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D))); + private void assertSupplierGetExecutionResultAndDuration(Supplier supplier, + T expectedValue, + double expectedDurationInMs) { + Instant start = Instant.now(); + T value = supplier.get(); + Long durationInMs = Duration.between(start, Instant.now()).toMillis(); + double marginOfErrorInMs = 100D; + + assertThat(value, is(equalTo(expectedValue))); + assertThat(durationInMs.doubleValue(), is(closeTo(expectedDurationInMs, marginOfErrorInMs))); } }