From 31eba377cc144a77264aefc9359e427b665277a0 Mon Sep 17 00:00:00 2001 From: Kerollos Magdy Date: Thu, 3 Nov 2022 01:26:53 +0200 Subject: [PATCH] Convert exception assertions to AssertJ in 3 exercises (#2194) --- .../src/test/java/CircularBufferTest.java | 45 +++++++--------- .../src/test/java/GoCountingTest.java | 47 +++++++---------- .../LargestSeriesProductCalculatorTest.java | 51 ++++++------------- 3 files changed, 50 insertions(+), 93 deletions(-) diff --git a/exercises/practice/circular-buffer/src/test/java/CircularBufferTest.java b/exercises/practice/circular-buffer/src/test/java/CircularBufferTest.java index 6a263f648..1b24d13da 100644 --- a/exercises/practice/circular-buffer/src/test/java/CircularBufferTest.java +++ b/exercises/practice/circular-buffer/src/test/java/CircularBufferTest.java @@ -1,5 +1,5 @@ import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertThrows; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import org.junit.Ignore; import org.junit.Test; @@ -10,11 +10,9 @@ public class CircularBufferTest { public void readingFromEmptyBufferShouldThrowException() { CircularBuffer buffer = new CircularBuffer<>(1); - BufferIOException expected = - assertThrows(BufferIOException.class, buffer::read); - - assertThat(expected) - .hasMessage("Tried to read from empty buffer"); + assertThatExceptionOfType(BufferIOException.class) + .isThrownBy(buffer::read) + .withMessage("Tried to read from empty buffer"); } @Ignore("Remove to run test") @@ -34,16 +32,14 @@ public void canReadItemOnlyOnce() throws BufferIOException { buffer.write(1); assertThat(buffer.read()).isEqualTo(1); - BufferIOException expected = - assertThrows(BufferIOException.class, buffer::read); - - assertThat(expected) - .hasMessage("Tried to read from empty buffer"); + assertThatExceptionOfType(BufferIOException.class) + .isThrownBy(buffer::read) + .withMessage("Tried to read from empty buffer"); } @Ignore("Remove to run test") @Test - public void readsItemsInOrderWritten() throws BufferIOException { + public void readsItemsInOrderWritten() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(2); buffer.write(1); @@ -59,11 +55,9 @@ public void fullBufferCantBeWrittenTo() throws BufferIOException { buffer.write(1); - BufferIOException expected = - assertThrows(BufferIOException.class, () -> buffer.write(2)); - - assertThat(expected) - .hasMessage("Tried to write to full buffer"); + assertThatExceptionOfType(BufferIOException.class) + .isThrownBy(() -> buffer.write(2)) + .withMessage("Tried to write to full buffer"); } @Ignore("Remove to run test") @@ -98,11 +92,9 @@ public void cantReadClearedItems() throws BufferIOException { buffer.write(1); buffer.clear(); - BufferIOException expected = - assertThrows(BufferIOException.class, buffer::read); - - assertThat(expected) - .hasMessage("Tried to read from empty buffer"); + assertThatExceptionOfType(BufferIOException.class) + .isThrownBy(buffer::read) + .withMessage("Tried to read from empty buffer"); } @Ignore("Remove to run test") @@ -178,11 +170,8 @@ public void initialClearDoesNotAffectWrappingAround() throws BufferIOException { assertThat(buffer.read()).isEqualTo(3); assertThat(buffer.read()).isEqualTo(4); - BufferIOException expected = - assertThrows(BufferIOException.class, buffer::read); - - assertThat(expected) - .hasMessage("Tried to read from empty buffer"); + assertThatExceptionOfType(BufferIOException.class) + .isThrownBy(buffer::read) + .withMessage("Tried to read from empty buffer"); } } - diff --git a/exercises/practice/go-counting/src/test/java/GoCountingTest.java b/exercises/practice/go-counting/src/test/java/GoCountingTest.java index 60a714ba9..7e8e95a6d 100644 --- a/exercises/practice/go-counting/src/test/java/GoCountingTest.java +++ b/exercises/practice/go-counting/src/test/java/GoCountingTest.java @@ -1,6 +1,5 @@ -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; import org.junit.Ignore; import org.junit.Test; @@ -13,10 +12,10 @@ public class GoCountingTest { String board5x5 = " B \n" + - " B B \n" + - "B W B\n" + - " W W \n" + - " W "; + " B B \n" + + "B W B\n" + + " W W \n" + + " W "; @Test public void blackCorner5x5BoardTest() { @@ -73,12 +72,9 @@ public void stoneNotTerritory5x5Board() { public void invalidXTooLow5x5Board() { GoCounting gocounting = new GoCounting(board5x5); - IllegalArgumentException expected = - assertThrows( - IllegalArgumentException.class, - () -> gocounting.getTerritory(-1, 1)); - - assertThat(expected).hasMessage("Invalid coordinate"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> gocounting.getTerritory(-1, 1)) + .withMessage("Invalid coordinate"); } @Ignore("Remove to run test") @@ -86,12 +82,9 @@ public void invalidXTooLow5x5Board() { public void invalidXTooHigh5x5Board() { GoCounting gocounting = new GoCounting(board5x5); - IllegalArgumentException expected = - assertThrows( - IllegalArgumentException.class, - () -> gocounting.getTerritory(5, 1)); - - assertThat(expected).hasMessage("Invalid coordinate"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> gocounting.getTerritory(5, 1)) + .withMessage("Invalid coordinate"); } @Ignore("Remove to run test") @@ -99,12 +92,9 @@ public void invalidXTooHigh5x5Board() { public void invalidYTooLow5x5Board() { GoCounting gocounting = new GoCounting(board5x5); - IllegalArgumentException expected = - assertThrows( - IllegalArgumentException.class, - () -> gocounting.getTerritory(1, -1)); - - assertThat(expected).hasMessage("Invalid coordinate"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> gocounting.getTerritory(1, -1)) + .withMessage("Invalid coordinate"); } @Ignore("Remove to run test") @@ -112,12 +102,9 @@ public void invalidYTooLow5x5Board() { public void invalidYTooHigh5x5Board() { GoCounting gocounting = new GoCounting(board5x5); - IllegalArgumentException expected = - assertThrows( - IllegalArgumentException.class, - () -> gocounting.getTerritory(1, 5)); - - assertThat(expected).hasMessage("Invalid coordinate"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> gocounting.getTerritory(1, 5)) + .withMessage("Invalid coordinate"); } @Ignore("Remove to run test") diff --git a/exercises/practice/largest-series-product/src/test/java/LargestSeriesProductCalculatorTest.java b/exercises/practice/largest-series-product/src/test/java/LargestSeriesProductCalculatorTest.java index ff044fcbf..374ad088e 100644 --- a/exercises/practice/largest-series-product/src/test/java/LargestSeriesProductCalculatorTest.java +++ b/exercises/practice/largest-series-product/src/test/java/LargestSeriesProductCalculatorTest.java @@ -1,6 +1,5 @@ -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; import org.junit.Ignore; import org.junit.Test; @@ -75,8 +74,8 @@ public void testCorrectlyCalculatesLargestProductOfLengthFiveWithNumbersInOrder( @Ignore("Remove to run test") @Test public void testCorrectlyCalculatesLargestProductInLongStringToSearchV1() { - LargestSeriesProductCalculator calculator - = new LargestSeriesProductCalculator("73167176531330624919225119674426574742355349194934"); + LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator( + "73167176531330624919225119674426574742355349194934"); long expectedProduct = 23520; @@ -112,14 +111,9 @@ public void testCorrectlyCalculatesLargestProductOfZeroIfAllSeriesOfGivenLengthC public void testSeriesLengthLongerThanLengthOfStringToTestIsRejected() { LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("123"); - IllegalArgumentException expected = - assertThrows( - IllegalArgumentException.class, - () -> calculator.calculateLargestProductForSeriesLength(4)); - - assertThat(expected) - .hasMessage( - "Series length must be less than or equal to the length of the string to search."); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> calculator.calculateLargestProductForSeriesLength(4)) + .withMessage("Series length must be less than or equal to the length of the string to search."); } @Ignore("Remove to run test") @@ -149,26 +143,17 @@ public void testCorrectlyCalculatesLargestProductOfLength0ForNonEmptyStringToSea public void testEmptyStringToSearchAndSeriesOfNonZeroLengthIsRejected() { LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator(""); - IllegalArgumentException expected = - assertThrows( - IllegalArgumentException.class, - () -> calculator.calculateLargestProductForSeriesLength(1)); - - assertThat(expected) - .hasMessage( - "Series length must be less than or equal to the length of the string to search."); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> calculator.calculateLargestProductForSeriesLength(1)) + .withMessage("Series length must be less than or equal to the length of the string to search."); } @Ignore("Remove to run test") @Test public void testStringToSearchContainingNonDigitCharacterIsRejected() { - IllegalArgumentException expected = - assertThrows( - IllegalArgumentException.class, - () -> new LargestSeriesProductCalculator("1234a5")); - - assertThat(expected) - .hasMessage("String to search may only contain digits."); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new LargestSeriesProductCalculator("1234a5")) + .withMessage("String to search may only contain digits."); } @Ignore("Remove to run test") @@ -176,15 +161,11 @@ public void testStringToSearchContainingNonDigitCharacterIsRejected() { public void testNegativeSeriesLengthIsRejected() { LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("12345"); - IllegalArgumentException expected = - assertThrows( - IllegalArgumentException.class, - () -> calculator.calculateLargestProductForSeriesLength(-1)); - - assertThat(expected) - .hasMessage("Series length must be non-negative."); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> calculator.calculateLargestProductForSeriesLength(-1)) + .withMessage("Series length must be non-negative."); } - + @Ignore("Remove to run test") @Test public void testForIntegerOverflow() {