Skip to content

Commit

Permalink
Convert exception assertions to AssertJ in 3 exercises (#2194)
Browse files Browse the repository at this point in the history
  • Loading branch information
kerolloz authored Nov 2, 2022
1 parent b415b67 commit 31eba37
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,11 +10,9 @@ public class CircularBufferTest {
public void readingFromEmptyBufferShouldThrowException() {
CircularBuffer<Integer> 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")
Expand All @@ -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<Integer> buffer = new CircularBuffer<>(2);

buffer.write(1);
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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");
}
}

47 changes: 17 additions & 30 deletions exercises/practice/go-counting/src/test/java/GoCountingTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -73,51 +72,39 @@ 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")
@Test
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")
@Test
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")
@Test
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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -149,42 +143,29 @@ 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")
@Test
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() {
Expand Down

0 comments on commit 31eba37

Please sign in to comment.