Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert exception assertions to AssertJ in classes starting with 'A' and 'B'. #2156

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

public class AffineCipherTest {

Expand Down Expand Up @@ -62,13 +62,9 @@ public void testEncodeAllTheLetters() {
@Ignore("Remove to run test")
@Test
public void testEncodeThrowsMeaningfulException() {
IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> affineCipher.encode("This is a test", 6, 17));

assertThat(expected)
.hasMessage("Error: keyA and alphabet size must be coprime.");
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> affineCipher.encode("This is a test", 6, 17))
.withMessage("Error: keyA and alphabet size must be coprime.");
}

@Ignore("Remove to run test")
Expand Down Expand Up @@ -116,13 +112,9 @@ public void testDecodeWithTooManySpaces() {
@Ignore("Remove to run test")
@Test
public void testDecodeThrowsMeaningfulException() {
IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> affineCipher.decode("Test", 13, 5));

assertThat(expected)
.hasMessage("Error: keyA and alphabet size must be coprime.");
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> affineCipher.decode("Test", 13, 5))
.withMessage("Error: keyA and alphabet size must be coprime.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

public class BaseConverterTest {

Expand Down Expand Up @@ -116,57 +116,41 @@ public void testLeadingZeros() {
@Ignore("Remove to run test")
@Test
public void testFirstBaseIsOne() {
IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> new BaseConverter(1, new int[]{1}));

assertThat(expected).hasMessage("Bases must be at least 2.");
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new BaseConverter(1, new int[]{1}))
.withMessage("Bases must be at least 2.");
}

@Ignore("Remove to run test")
@Test
public void testFirstBaseIsZero() {
IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> new BaseConverter(0, new int[]{1}));

assertThat(expected).hasMessage("Bases must be at least 2.");
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new BaseConverter(0, new int[]{1}))
.withMessage("Bases must be at least 2.");
}

@Ignore("Remove to run test")
@Test
public void testFirstBaseIsNegative() {
IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> new BaseConverter(-2, new int[]{1}));

assertThat(expected).hasMessage("Bases must be at least 2.");
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new BaseConverter(-2, new int[]{1}))
.withMessage("Bases must be at least 2.");
}

@Ignore("Remove to run test")
@Test
public void testNegativeDigit() {
IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> new BaseConverter(2, new int[]{1, -1, 1, 0, 1, 0}));

assertThat(expected).hasMessage("Digits may not be negative.");
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new BaseConverter(2, new int[]{1, -1, 1, 0, 1, 0}))
.withMessage("Digits may not be negative.");
}

@Ignore("Remove to run test")
@Test
public void testInvalidPositiveDigit() {
IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> new BaseConverter(2, new int[]{1, 2, 1, 0, 1, 0}));

assertThat(expected)
.hasMessage("All digits must be strictly less than the base.");
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new BaseConverter(2, new int[]{1, 2, 1, 0, 1, 0}))
.withMessage("All digits must be strictly less than the base.");
}

@Ignore("Remove to run test")
Expand All @@ -175,38 +159,29 @@ public void testSecondBaseIsOne() {
BaseConverter baseConverter =
new BaseConverter(2, new int[]{1, 0, 1, 0, 1, 0});

IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> baseConverter.convertToBase(1));

assertThat(expected).hasMessage("Bases must be at least 2.");
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> baseConverter.convertToBase(1))
.withMessage("Bases must be at least 2.");
}

@Ignore("Remove to run test")
@Test
public void testSecondBaseIsZero() {
BaseConverter baseConverter = new BaseConverter(10, new int[]{7});

IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> baseConverter.convertToBase(0));

assertThat(expected).hasMessage("Bases must be at least 2.");
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> baseConverter.convertToBase(0))
.withMessage("Bases must be at least 2.");
}

@Ignore("Remove to run test")
@Test
public void testSecondBaseIsNegative() {
BaseConverter baseConverter = new BaseConverter(2, new int[]{1});

IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> baseConverter.convertToBase(-7));

assertThat(expected).hasMessage("Bases must be at least 2.");
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> baseConverter.convertToBase(-7))
.withMessage("Bases must be at least 2.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import static java.util.Map.entry;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

public class AlphameticsTest {

Expand All @@ -21,15 +21,17 @@ public void testThreeLetters() throws UnsolvablePuzzleException {
public void testUniqueValue() {
Alphametics alphametics = new Alphametics("A == B");

assertThrows(UnsolvablePuzzleException.class, alphametics::solve);
assertThatExceptionOfType(UnsolvablePuzzleException.class)
.isThrownBy(alphametics::solve);
}

@Ignore("Remove to run test")
@Test
public void testLeadingZero() {
Alphametics alphametics = new Alphametics("ACA + DD == BD");

assertThrows(UnsolvablePuzzleException.class, alphametics::solve);
assertThatExceptionOfType(UnsolvablePuzzleException.class)
.isThrownBy(alphametics::solve);
}

@Ignore("Remove to run test")
Expand Down
82 changes: 26 additions & 56 deletions exercises/practice/bank-account/src/test/java/BankAccountTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;

import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -66,13 +66,9 @@ public void canWithdrawMoneySequentially() throws BankAccountActionInvalidExcept
public void cannotWithdrawMoneyFromEmptyAccount() {
bankAccount.open();

BankAccountActionInvalidException expected =
assertThrows(
BankAccountActionInvalidException.class,
() -> bankAccount.withdraw(5));

assertThat(expected)
.hasMessage("Cannot withdraw money from an empty account");
assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(() -> bankAccount.withdraw(5))
.withMessage("Cannot withdraw money from an empty account");
}

@Ignore("Remove to run test")
Expand All @@ -81,29 +77,19 @@ public void cannotWithdrawMoreMoneyThanYouHave() throws BankAccountActionInvalid
bankAccount.open();
bankAccount.deposit(6);

BankAccountActionInvalidException expected =
assertThrows(
BankAccountActionInvalidException.class,
() -> bankAccount.withdraw(7));

assertThat(expected)
.hasMessage(
"Cannot withdraw more money than is currently in the account");

assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(() -> bankAccount.withdraw(7))
.withMessage("Cannot withdraw more money than is currently in the account");
}

@Ignore("Remove to run test")
@Test
public void cannotDepositNegativeAmount() {
bankAccount.open();

BankAccountActionInvalidException expected =
assertThrows(
BankAccountActionInvalidException.class,
() -> bankAccount.deposit(-1));

assertThat(expected)
.hasMessage("Cannot deposit or withdraw negative amount");
assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(() -> bankAccount.deposit(-1))
.withMessage("Cannot deposit or withdraw negative amount");
}

@Ignore("Remove to run test")
Expand All @@ -112,13 +98,9 @@ public void cannotWithdrawNegativeAmount() throws BankAccountActionInvalidExcept
bankAccount.open();
bankAccount.deposit(105);

BankAccountActionInvalidException expected =
assertThrows(
BankAccountActionInvalidException.class,
() -> bankAccount.withdraw(-5));

assertThat(expected)
.hasMessage("Cannot deposit or withdraw negative amount");
assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(() -> bankAccount.withdraw(-5))
.withMessage("Cannot deposit or withdraw negative amount");
}

@Ignore("Remove to run test")
Expand All @@ -128,12 +110,9 @@ public void cannotGetBalanceOfClosedAccount() throws BankAccountActionInvalidExc
bankAccount.deposit(10);
bankAccount.close();

BankAccountActionInvalidException expected =
assertThrows(
BankAccountActionInvalidException.class,
bankAccount::getBalance);

assertThat(expected).hasMessage("Account closed");
assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(bankAccount::getBalance)
.withMessage("Account closed");
}

@Ignore("Remove to run test")
Expand All @@ -142,12 +121,9 @@ public void cannotDepositMoneyIntoClosedAccount() {
bankAccount.open();
bankAccount.close();

BankAccountActionInvalidException expected =
assertThrows(
BankAccountActionInvalidException.class,
() -> bankAccount.deposit(5));

assertThat(expected).hasMessage("Account closed");
assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(() -> bankAccount.deposit(5))
.withMessage("Account closed");
}

@Ignore("Remove to run test")
Expand All @@ -157,23 +133,17 @@ public void cannotWithdrawMoneyFromClosedAccount() throws BankAccountActionInval
bankAccount.deposit(20);
bankAccount.close();

BankAccountActionInvalidException expected =
assertThrows(
BankAccountActionInvalidException.class,
() -> bankAccount.withdraw(5));

assertThat(expected).hasMessage("Account closed");
assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(() -> bankAccount.withdraw(5))
.withMessage("Account closed");
}

@Ignore("Remove to run test")
@Test
public void bankAccountIsClosedBeforeItIsOpened() {
BankAccountActionInvalidException expected =
assertThrows(
BankAccountActionInvalidException.class,
bankAccount::getBalance);

assertThat(expected).hasMessage("Account closed");
assertThatExceptionOfType(BankAccountActionInvalidException.class)
.isThrownBy(bankAccount::getBalance)
.withMessage("Account closed");
}

@Ignore("Remove to run test")
Expand Down
Loading