diff --git a/POLICIES.md b/POLICIES.md index e49bf1cc9..63579190c 100644 --- a/POLICIES.md +++ b/POLICIES.md @@ -17,7 +17,7 @@ Our policies are not set-in-stone. They represent directions chosen at a point i | Track Event | Policies to review | |:------------|:-----------------| -| Exercise added/updated | [Prefer instance methods](#prefer-instance-methods); [Avoid using final](#avoid-using-final); [Adhere to best practices](#adhere-to-best-practices); [Starter implementations](#starter-implementations); [Ignore noninitial tests](#ignore-noninitial-tests); [Multiple file submissions](#multiple-file-submissions); [Name test class after class under test](#name-test-class-after-class-under-test); [Add hint for the first exercises without starter implementation](#add-hint-for-the-first-exercises-without-starter-implementation); [Reference tutorial in the first exercises](#reference-tutorial-in-the-first-exercises); [Avoid returning null](#avoid-returning-null); [Use ExpectedException](#use-expectedexception); [Using other assertion libraries](#using-other-assertion-libraries) +| Exercise added/updated | [Prefer instance methods](#prefer-instance-methods); [Avoid using final](#avoid-using-final); [Adhere to best practices](#adhere-to-best-practices); [Starter implementations](#starter-implementations); [Ignore noninitial tests](#ignore-noninitial-tests); [Multiple file submissions](#multiple-file-submissions); [Name test class after class under test](#name-test-class-after-class-under-test); [Add hint for the first exercises without starter implementation](#add-hint-for-the-first-exercises-without-starter-implementation); [Reference tutorial in the first exercises](#reference-tutorial-in-the-first-exercises); [Avoid returning null](#avoid-returning-null); [Use assertThrows](#use-assertthrows); [Using other assertion libraries](#using-other-assertion-libraries) | Track rearranged | [Starter implementations](#starter-implementations); [Multiple file submissions](#multiple-file-submissions) | | New issue observed in track | [Good first issues](#good-first-issues) | | "Good first issue" issue completed | [Good first issues](#good-first-issues) | @@ -149,14 +149,17 @@ References: [[1](https://github.com/exercism/java/issues/1389)] References: [[1](https://www.codebyamir.com/blog/stop-returning-null-in-java)] -### Use ExpectedException +### Use assertThrows > Some exercises expect exceptions to be thrown in the tests. -> The exercises on this track are all using JUnit's [`ExpectedException`](http://junit.org/junit4/javadoc/4.12/org/junit/rules/ExpectedException.html) `@Rule` feature to support that instead of `@Test(expected = SomeException.class)`. -> `ExpectedException` is more powerful than using the `@Test` annotation, since with `ExpectedException` you can also inspect the exception's error message and other properties. -> To be consistent, please use `ExpectedException` whenever you expect an exception to be thrown. - -> See [the triangle tests](https://github.com/exercism/java/blob/master/exercises/triangle/src/test/java/TriangleTest.java) for an example of where `ExpectedException` is used. +> The exercises on this track are all using [`org.junit.Assert.assertThrows`](https://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThrows(java.lang.Class,%20org.junit.function.ThrowingRunnable)) instead of `@Test(expected = SomeException.class)`. +> `assertThrows` is more powerful than using the `@Test` annotation. +> With this method you can assert that a given function call (specified, for instance, as a lambda expression or method reference) results in a particular type of exception being thrown. +> In addition it returns the exception that was thrown, so that further assertions can be made (e.g. to verify that the message and cause are correct). +> Furthermore, you can make assertions on the state of a domain object after the exception has been thrown. +> To be consistent, please use `assertThrows` whenever you expect an exception to be thrown. + +> See [the triangle tests](https://github.com/exercism/java/blob/master/exercises/triangle/src/test/java/TriangleTest.java) for an example of where `assertThrows` is used. References: [[1](https://github.com/junit-team/junit4/wiki/Exception-testing)] diff --git a/_template/build.gradle b/_template/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/_template/build.gradle +++ b/_template/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/accumulate/build.gradle b/exercises/accumulate/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/accumulate/build.gradle +++ b/exercises/accumulate/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/acronym/build.gradle b/exercises/acronym/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/acronym/build.gradle +++ b/exercises/acronym/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/affine-cipher/build.gradle b/exercises/affine-cipher/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/affine-cipher/build.gradle +++ b/exercises/affine-cipher/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/affine-cipher/src/test/java/AffineCipherTest.java b/exercises/affine-cipher/src/test/java/AffineCipherTest.java index 39d177a01..f6d02c999 100644 --- a/exercises/affine-cipher/src/test/java/AffineCipherTest.java +++ b/exercises/affine-cipher/src/test/java/AffineCipherTest.java @@ -1,22 +1,13 @@ -import org.junit.Before; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; - -import static org.junit.Assert.assertEquals; public class AffineCipherTest { - private AffineCipher affineCipher; - - @Before - public void genAffineCipher() { - affineCipher = new AffineCipher(); - } - - @Rule - public ExpectedException expectedException = ExpectedException.none(); + private AffineCipher affineCipher = new AffineCipher(); @Test public void testEncodeYes() { @@ -71,11 +62,14 @@ public void testEncodeAllTheLetters() { @Ignore("Remove to run test") @Test - public void testEncodeThrowsMeaningfulException() throws IllegalArgumentException { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Error: keyA and alphabet size must be coprime."); + public void testEncodeThrowsMeaningfulException() { + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> affineCipher.encode("This is a test", 6, 17)); - affineCipher.encode("This is a test", 6, 17); + assertThat(expected) + .hasMessage("Error: keyA and alphabet size must be coprime."); } @Ignore("Remove to run test") @@ -120,10 +114,13 @@ public void testDecodeWithTooManySpaces() { @Ignore("Remove to run test") @Test public void testDecodeThrowsMeaningfulException() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Error: keyA and alphabet size must be coprime."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> affineCipher.decode("Test", 13, 5)); - affineCipher.decode("Test", 13, 5); + assertThat(expected) + .hasMessage("Error: keyA and alphabet size must be coprime."); } } diff --git a/exercises/all-your-base/build.gradle b/exercises/all-your-base/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/all-your-base/build.gradle +++ b/exercises/all-your-base/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/all-your-base/src/test/java/BaseConverterTest.java b/exercises/all-your-base/src/test/java/BaseConverterTest.java index a677cbf76..c8b8e877c 100644 --- a/exercises/all-your-base/src/test/java/BaseConverterTest.java +++ b/exercises/all-your-base/src/test/java/BaseConverterTest.java @@ -1,17 +1,14 @@ +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.util.Arrays; -import static org.junit.Assert.assertArrayEquals; - public class BaseConverterTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void testSingleBitOneToDecimal() { BaseConverter baseConverter = new BaseConverter(2, new int[]{1}); @@ -218,57 +215,71 @@ public void testLeadingZeros() { @Ignore("Remove to run test") @Test public void testFirstBaseIsOne() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Bases must be at least 2."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new BaseConverter(1, new int[]{1})); - new BaseConverter(1, new int[]{1}); + assertThat(expected).hasMessage("Bases must be at least 2."); } @Ignore("Remove to run test") @Test public void testFirstBaseIsZero() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Bases must be at least 2."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new BaseConverter(0, new int[]{1})); - new BaseConverter(0, new int[]{}); + assertThat(expected).hasMessage("Bases must be at least 2."); } @Ignore("Remove to run test") @Test public void testFirstBaseIsNegative() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Bases must be at least 2."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new BaseConverter(-2, new int[]{1})); - new BaseConverter(-2, new int[]{}); + assertThat(expected).hasMessage("Bases must be at least 2."); } @Ignore("Remove to run test") @Test public void testNegativeDigit() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Digits may not be negative."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new BaseConverter(2, new int[]{1, -1, 1, 0, 1, 0})); - new BaseConverter(2, new int[]{1, -1, 1, 0, 1, 0}); + assertThat(expected).hasMessage("Digits may not be negative."); } @Ignore("Remove to run test") @Test public void testInvalidPositiveDigit() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("All digits must be strictly less than the base."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new BaseConverter(2, new int[]{1, 2, 1, 0, 1, 0})); - new BaseConverter(2, new int[]{1, 2, 1, 0, 1, 0}); + assertThat(expected) + .hasMessage("All digits must be strictly less than the base."); } @Ignore("Remove to run test") @Test public void testSecondBaseIsOne() { - BaseConverter baseConverter = new BaseConverter(2, new int[]{1, 0, 1, 0, 1, 0}); + BaseConverter baseConverter = + new BaseConverter(2, new int[]{1, 0, 1, 0, 1, 0}); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Bases must be at least 2."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> baseConverter.convertToBase(1)); - baseConverter.convertToBase(1); + assertThat(expected).hasMessage("Bases must be at least 2."); } @Ignore("Remove to run test") @@ -276,10 +287,12 @@ public void testSecondBaseIsOne() { public void testSecondBaseIsZero() { BaseConverter baseConverter = new BaseConverter(10, new int[]{7}); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Bases must be at least 2."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> baseConverter.convertToBase(0)); - baseConverter.convertToBase(0); + assertThat(expected).hasMessage("Bases must be at least 2."); } @Ignore("Remove to run test") @@ -287,10 +300,12 @@ public void testSecondBaseIsZero() { public void testSecondBaseIsNegative() { BaseConverter baseConverter = new BaseConverter(2, new int[]{1}); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Bases must be at least 2."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> baseConverter.convertToBase(-7)); - baseConverter.convertToBase(-7); + assertThat(expected).hasMessage("Bases must be at least 2."); } } diff --git a/exercises/allergies/build.gradle b/exercises/allergies/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/allergies/build.gradle +++ b/exercises/allergies/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/alphametics/build.gradle b/exercises/alphametics/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/alphametics/build.gradle +++ b/exercises/alphametics/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/alphametics/src/test/java/AlphameticsTest.java b/exercises/alphametics/src/test/java/AlphameticsTest.java index 4109cea15..569ed863e 100644 --- a/exercises/alphametics/src/test/java/AlphameticsTest.java +++ b/exercises/alphametics/src/test/java/AlphameticsTest.java @@ -1,16 +1,12 @@ import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.util.LinkedHashMap; - public class AlphameticsTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void testThreeLetters() throws UnsolvablePuzzleException { @@ -24,16 +20,18 @@ public void testThreeLetters() throws UnsolvablePuzzleException { @Ignore("Remove to run test") @Test - public void testUniqueValue() throws UnsolvablePuzzleException { - expectedException.expect(UnsolvablePuzzleException.class); - new Alphametics("A == B").solve(); + public void testUniqueValue() { + Alphametics alphametics = new Alphametics("A == B"); + + assertThrows(UnsolvablePuzzleException.class, alphametics::solve); } @Ignore("Remove to run test") @Test - public void testLeadingZero() throws UnsolvablePuzzleException { - expectedException.expect(UnsolvablePuzzleException.class); - new Alphametics("ACA + DD == BD").solve(); + public void testLeadingZero() { + Alphametics alphametics = new Alphametics("ACA + DD == BD"); + + assertThrows(UnsolvablePuzzleException.class, alphametics::solve); } @Ignore("Remove to run test") diff --git a/exercises/anagram/build.gradle b/exercises/anagram/build.gradle index 952e38529..96c8acd4c 100644 --- a/exercises/anagram/build.gradle +++ b/exercises/anagram/build.gradle @@ -7,7 +7,7 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" testImplementation "org.assertj:assertj-core:3.15.0" } diff --git a/exercises/anagram/src/test/java/AnagramTest.java b/exercises/anagram/src/test/java/AnagramTest.java index 7e833af7e..94e75ef17 100644 --- a/exercises/anagram/src/test/java/AnagramTest.java +++ b/exercises/anagram/src/test/java/AnagramTest.java @@ -5,7 +5,6 @@ import java.util.Arrays; import java.util.Collections; -import java.util.List; public class AnagramTest { diff --git a/exercises/armstrong-numbers/build.gradle b/exercises/armstrong-numbers/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/armstrong-numbers/build.gradle +++ b/exercises/armstrong-numbers/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/armstrong-numbers/src/main/java/ArmstrongNumbers.java b/exercises/armstrong-numbers/src/main/java/ArmstrongNumbers.java index f315ed95d..636c2575c 100644 --- a/exercises/armstrong-numbers/src/main/java/ArmstrongNumbers.java +++ b/exercises/armstrong-numbers/src/main/java/ArmstrongNumbers.java @@ -1,9 +1,9 @@ class ArmstrongNumbers { - boolean isArmstrongNumber(int numberToCheck) { + boolean isArmstrongNumber(int numberToCheck) { - throw new UnsupportedOperationException("Delete this statement and write your own implementation."); - - } + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + + } } diff --git a/exercises/atbash-cipher/build.gradle b/exercises/atbash-cipher/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/atbash-cipher/build.gradle +++ b/exercises/atbash-cipher/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/bank-account/build.gradle b/exercises/bank-account/build.gradle index 479746285..950f15a95 100644 --- a/exercises/bank-account/build.gradle +++ b/exercises/bank-account/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/bank-account/src/test/java/BankAccountTest.java b/exercises/bank-account/src/test/java/BankAccountTest.java index 598294c84..800cdfe6a 100644 --- a/exercises/bank-account/src/test/java/BankAccountTest.java +++ b/exercises/bank-account/src/test/java/BankAccountTest.java @@ -1,23 +1,15 @@ -import org.junit.Before; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.fail; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.util.Random; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - public class BankAccountTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private BankAccount bankAccount; - - @Before - public void setup() { - bankAccount = new BankAccount(); - } + private BankAccount bankAccount = new BankAccount(); @Test public void newlyOpenedAccountHasEmptyBalance() throws BankAccountActionInvalidException { @@ -72,13 +64,16 @@ public void canWithdrawMoneySequentially() throws BankAccountActionInvalidExcept @Ignore("Remove to run test") @Test - public void cannotWithdrawMoneyFromEmptyAccount() throws BankAccountActionInvalidException { + public void cannotWithdrawMoneyFromEmptyAccount() { bankAccount.open(); - expectedException.expect(BankAccountActionInvalidException.class); - expectedException.expectMessage("Cannot withdraw money from an empty account"); + BankAccountActionInvalidException expected = + assertThrows( + BankAccountActionInvalidException.class, + () -> bankAccount.withdraw(5)); - bankAccount.withdraw(5); + assertThat(expected) + .hasMessage("Cannot withdraw money from an empty account"); } @Ignore("Remove to run test") @@ -87,21 +82,29 @@ public void cannotWithdrawMoreMoneyThanYouHave() throws BankAccountActionInvalid bankAccount.open(); bankAccount.deposit(6); - expectedException.expect(BankAccountActionInvalidException.class); - expectedException.expectMessage("Cannot withdraw more money than is currently in the account"); + BankAccountActionInvalidException expected = + assertThrows( + BankAccountActionInvalidException.class, + () -> bankAccount.withdraw(7)); + + assertThat(expected) + .hasMessage( + "Cannot withdraw more money than is currently in the account"); - bankAccount.withdraw(7); } @Ignore("Remove to run test") @Test - public void cannotDepositNegativeAmount() throws BankAccountActionInvalidException { + public void cannotDepositNegativeAmount() { bankAccount.open(); - expectedException.expect(BankAccountActionInvalidException.class); - expectedException.expectMessage("Cannot deposit or withdraw negative amount"); + BankAccountActionInvalidException expected = + assertThrows( + BankAccountActionInvalidException.class, + () -> bankAccount.deposit(-1)); - bankAccount.deposit(-1); + assertThat(expected) + .hasMessage("Cannot deposit or withdraw negative amount"); } @Ignore("Remove to run test") @@ -110,10 +113,13 @@ public void cannotWithdrawNegativeAmount() throws BankAccountActionInvalidExcept bankAccount.open(); bankAccount.deposit(105); - expectedException.expect(BankAccountActionInvalidException.class); - expectedException.expectMessage("Cannot deposit or withdraw negative amount"); + BankAccountActionInvalidException expected = + assertThrows( + BankAccountActionInvalidException.class, + () -> bankAccount.withdraw(-5)); - bankAccount.withdraw(-5); + assertThat(expected) + .hasMessage("Cannot deposit or withdraw negative amount"); } @Ignore("Remove to run test") @@ -123,22 +129,26 @@ public void cannotGetBalanceOfClosedAccount() throws BankAccountActionInvalidExc bankAccount.deposit(10); bankAccount.close(); - expectedException.expect(BankAccountActionInvalidException.class); - expectedException.expectMessage("Account closed"); + BankAccountActionInvalidException expected = + assertThrows( + BankAccountActionInvalidException.class, + bankAccount::getBalance); - bankAccount.getBalance(); + assertThat(expected).hasMessage("Account closed"); } @Ignore("Remove to run test") @Test - public void cannotDepositMoneyIntoClosedAccount() throws BankAccountActionInvalidException { + public void cannotDepositMoneyIntoClosedAccount() { bankAccount.open(); bankAccount.close(); - expectedException.expect(BankAccountActionInvalidException.class); - expectedException.expectMessage("Account closed"); + BankAccountActionInvalidException expected = + assertThrows( + BankAccountActionInvalidException.class, + () -> bankAccount.deposit(5)); - bankAccount.deposit(5); + assertThat(expected).hasMessage("Account closed"); } @Ignore("Remove to run test") @@ -148,19 +158,23 @@ public void cannotWithdrawMoneyFromClosedAccount() throws BankAccountActionInval bankAccount.deposit(20); bankAccount.close(); - expectedException.expect(BankAccountActionInvalidException.class); - expectedException.expectMessage("Account closed"); + BankAccountActionInvalidException expected = + assertThrows( + BankAccountActionInvalidException.class, + () -> bankAccount.withdraw(5)); - bankAccount.withdraw(5); + assertThat(expected).hasMessage("Account closed"); } @Ignore("Remove to run test") @Test - public void bankAccountIsClosedBeforeItIsOpened() throws BankAccountActionInvalidException { - expectedException.expect(BankAccountActionInvalidException.class); - expectedException.expectMessage("Account closed"); + public void bankAccountIsClosedBeforeItIsOpened() { + BankAccountActionInvalidException expected = + assertThrows( + BankAccountActionInvalidException.class, + bankAccount::getBalance); - bankAccount.getBalance(); + assertThat(expected).hasMessage("Account closed"); } @Ignore("Remove to run test") @@ -171,6 +185,7 @@ public void canAdjustBalanceConcurrently() throws BankAccountActionInvalidExcept for (int i = 0; i < 10; i++) { adjustBalanceConcurrently(); + assertEquals(1000, bankAccount.getBalance()); } } @@ -195,7 +210,5 @@ private void adjustBalanceConcurrently() throws BankAccountActionInvalidExceptio for (Thread thread : threads) { thread.join(); } - - assertEquals(1000, bankAccount.getBalance()); } } diff --git a/exercises/beer-song/build.gradle b/exercises/beer-song/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/beer-song/build.gradle +++ b/exercises/beer-song/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/binary-search-tree/build.gradle b/exercises/binary-search-tree/build.gradle index 479746285..e13267163 100644 --- a/exercises/binary-search-tree/build.gradle +++ b/exercises/binary-search-tree/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/binary-search/build.gradle b/exercises/binary-search/build.gradle index 479746285..950f15a95 100644 --- a/exercises/binary-search/build.gradle +++ b/exercises/binary-search/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/binary-search/src/test/java/BinarySearchTest.java b/exercises/binary-search/src/test/java/BinarySearchTest.java index 2b4818fdc..4a2abfcdb 100644 --- a/exercises/binary-search/src/test/java/BinarySearchTest.java +++ b/exercises/binary-search/src/test/java/BinarySearchTest.java @@ -1,20 +1,16 @@ +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.util.Arrays; import java.util.Collections; import java.util.List; -import static org.junit.Assert.assertEquals; - public class BinarySearchTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void findsAValueInAnArrayWithOneElement() throws ValueNotFoundException { List listOfUnitLength = Collections.singletonList(6); @@ -86,32 +82,36 @@ public void findsAValueInAnArrayOfEvenLength() throws ValueNotFoundException { @Ignore("Remove to run test") @Test - public void identifiesThatAValueIsNotFoundInTheArray() throws ValueNotFoundException { + public void identifiesThatAValueIsNotFoundInTheArray() { List sortedList = Collections.unmodifiableList( Arrays.asList(1, 3, 4, 6, 8, 9, 11) ); BinarySearch search = new BinarySearch(sortedList); - expectedException.expect(ValueNotFoundException.class); - expectedException.expectMessage("Value not in array"); + ValueNotFoundException expected = + assertThrows( + ValueNotFoundException.class, + () -> search.indexOf(7)); - search.indexOf(7); + assertThat(expected).hasMessage("Value not in array"); } @Ignore("Remove to run test") @Test - public void aValueSmallerThanTheArraysSmallestValueIsNotFound() throws ValueNotFoundException { + public void aValueSmallerThanTheArraysSmallestValueIsNotFound() { List sortedList = Collections.unmodifiableList( Arrays.asList(1, 3, 4, 6, 8, 9, 11) ); BinarySearch search = new BinarySearch(sortedList); - expectedException.expect(ValueNotFoundException.class); - expectedException.expectMessage("Value not in array"); + ValueNotFoundException expected = + assertThrows( + ValueNotFoundException.class, + () -> search.indexOf(0)); - search.indexOf(0); + assertThat(expected).hasMessage("Value not in array"); } @Ignore("Remove to run test") @@ -123,10 +123,12 @@ public void aValueLargerThanTheArraysSmallestValueIsNotFound() throws ValueNotFo BinarySearch search = new BinarySearch(sortedList); - expectedException.expect(ValueNotFoundException.class); - expectedException.expectMessage("Value not in array"); + ValueNotFoundException expected = + assertThrows( + ValueNotFoundException.class, + () -> search.indexOf(13)); - search.indexOf(13); + assertThat(expected).hasMessage("Value not in array"); } @Ignore("Remove to run test") @@ -136,10 +138,12 @@ public void nothingIsFoundInAnEmptyArray() throws ValueNotFoundException { BinarySearch search = new BinarySearch(emptyList); - expectedException.expect(ValueNotFoundException.class); - expectedException.expectMessage("Value not in array"); + ValueNotFoundException expected = + assertThrows( + ValueNotFoundException.class, + () -> search.indexOf(1)); - search.indexOf(1); + assertThat(expected).hasMessage("Value not in array"); } @Ignore("Remove to run test") @@ -151,10 +155,12 @@ public void nothingIsFoundWhenTheLeftAndRightBoundCross() throws ValueNotFoundEx BinarySearch search = new BinarySearch(sortedList); - expectedException.expect(ValueNotFoundException.class); - expectedException.expectMessage("Value not in array"); + ValueNotFoundException expected = + assertThrows( + ValueNotFoundException.class, + () -> search.indexOf(0)); - search.indexOf(0); + assertThat(expected).hasMessage("Value not in array"); } } diff --git a/exercises/binary/build.gradle b/exercises/binary/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/binary/build.gradle +++ b/exercises/binary/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/bob/build.gradle b/exercises/bob/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/bob/build.gradle +++ b/exercises/bob/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/book-store/build.gradle b/exercises/book-store/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/book-store/build.gradle +++ b/exercises/book-store/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/bowling/.meta/src/reference/java/BowlingGame.java b/exercises/bowling/.meta/src/reference/java/BowlingGame.java index 1e7d038c1..9a69b0dae 100755 --- a/exercises/bowling/.meta/src/reference/java/BowlingGame.java +++ b/exercises/bowling/.meta/src/reference/java/BowlingGame.java @@ -2,83 +2,63 @@ import java.util.List; class BowlingGame { - private static final int NUMBER_OF_FRAMES = 10; - private static final int MAXIMUM_FRAME_SCORE = 10; - private List rolls = new ArrayList<>(); + + private Frame.Builder frame = Frame.builder(); + private List frames = new ArrayList<>(); void roll(int pins) { - rolls.add(pins); + checkGameIsNotOver(); + frame.withRoll(pins); + if (frame.isComplete()) { + frames.add(frame.build()); + frame = Frame.builder(); + } else if (isLastFrame()) { + frames.add(frame.build()); + } } int score() { - int score = 0; - int frameIndex = 0; - - for (int i = 1; i <= NUMBER_OF_FRAMES; i++) { - if (rolls.size() <= frameIndex) { - throw new IllegalStateException("Score cannot be taken until the end of the game"); - } - - if (isStrike(frameIndex)) { - if (rolls.size() <= frameIndex + 2) { - throw new IllegalStateException("Score cannot be taken until the end of the game"); - } + checkGameOver(); - int strikeBonus = strikeBonus(frameIndex); - if (strikeBonus > MAXIMUM_FRAME_SCORE && !isStrike(frameIndex + 1) || spareBonus(frameIndex) > 10) { - throw new IllegalStateException("Pin count exceeds pins on the lane"); - } - - score += 10 + strikeBonus; - frameIndex += i == NUMBER_OF_FRAMES ? 3 : 1; - } else if (isSpare(frameIndex)) { - if (rolls.size() <= frameIndex + 2) { - throw new IllegalStateException("Score cannot be taken until the end of the game"); - } - - score += 10 + spareBonus(frameIndex); - frameIndex += i == NUMBER_OF_FRAMES ? 3 : 2; - } else { - int frameScore = frameScore(frameIndex); - if (frameScore < 0) { - throw new IllegalStateException("Negative roll is invalid"); - } else if (frameScore > 10) { - throw new IllegalStateException("Pin count exceeds pins on the lane"); - } - - score += frameScore; - frameIndex += 2; - } - } - - if (!correctNumberOfRolls(frameIndex)) { - throw new IllegalStateException("Cannot roll after game is over"); + int score = 0; + for (int i = 0; i < 10; i++) { + Frame current = frames.get(i); + Frame next = i + 1 < frames.size() ? frames.get(i + 1) : Frame.EMPTY; + Frame nextNext = i + 2 < frames.size() ? frames.get(i + 2) : Frame.EMPTY; + score += current.score(next, nextNext); } return score; } - private boolean correctNumberOfRolls(int frameIndex) { - return frameIndex == rolls.size(); - } - - private boolean isStrike(int frameIndex) { - return rolls.get(frameIndex) == MAXIMUM_FRAME_SCORE; + private void checkGameIsNotOver() { + checkState(!isGameOver(), "Cannot roll after game is over"); } - private boolean isSpare(int frameIndex) { - return rolls.get(frameIndex) + rolls.get(frameIndex + 1) == MAXIMUM_FRAME_SCORE; + private void checkGameOver() { + checkState(isGameOver(), "Score cannot be taken until the end of the game"); } - private int strikeBonus(int frameIndex) { - return rolls.get(frameIndex + 1) + rolls.get(frameIndex + 2); + private boolean isGameOver() { + if (frames.size() < 10) { + return false; + } + if (frames.size() == 10) { + return !frames.get(9).isSpare(); + } + if (frames.size() == 11) { + return !frames.get(9).isStrike() || !frames.get(10).isStrike(); + } + return true; } - private int spareBonus(int frameIndex) { - return rolls.get(frameIndex + 2); + private boolean isLastFrame() { + return frames.size() == 11 || (frames.size() == 10 && !frames.get(9).isStrike()); } - private int frameScore(int frameIndex) { - return rolls.get(frameIndex) + rolls.get(frameIndex + 1); + private static void checkState(boolean expression, String message) { + if (!expression) { + throw new IllegalStateException(message); + } } } diff --git a/exercises/bowling/.meta/src/reference/java/Frame.java b/exercises/bowling/.meta/src/reference/java/Frame.java new file mode 100644 index 000000000..f5eb773da --- /dev/null +++ b/exercises/bowling/.meta/src/reference/java/Frame.java @@ -0,0 +1,101 @@ +import java.util.ArrayList; +import java.util.List; + +public class Frame { + public static final Frame EMPTY = new Frame(0, 0); + + private final int first; + private final int second; + + private Frame(int first) { + this(first, 0); + } + + private Frame(int first, int second) { + this.first = first; + this.second = second; + } + + public int frameScore() { + return first + second; + } + + public int score(Frame next, Frame nextNext) { + int score = frameScore(); + if (isStrike()) { + score += nextNextRoll(next, nextNext); + } + if (isSpare()) { + score += next.first; + } + return score; + } + + @Override + public String toString() { + return String.format("Frame[first=%d, second=%d]", first, second); + } + + private static int nextNextRoll(Frame next, Frame nextNext) { + return next.isStrike() ? nextNext.first : next.second; + } + + public boolean isStrike() { + return first == 10; + } + + public boolean isSpare() { + return frameScore() == 10; + } + + static Builder builder() { + return new Builder(); + } + + public static class Builder { + private List rolls = new ArrayList<>(); + + public Builder withRoll(int pins) { + checkNegativeRoll(pins); + checkPinCount(pins); + rolls.add(pins); + return this; + } + + public Frame build() { + switch (rolls.size()) { + case 2: + return new Frame(rolls.get(0), rolls.get(1)); + case 1: + return new Frame(rolls.get(0)); + } + throw new AssertionError("Frame has invalid number of rolls: " + rolls.size()); + } + + public boolean isComplete() { + return rolls.size() == 2 || isStrike(); + } + + private boolean isStrike() { + return rolls.size() == 1 && rolls.get(0) == 10; + } + + private int countPins() { + return rolls.stream().mapToInt(i -> i).sum(); + } + + private void checkPinCount(int pins) { + checkState(countPins() + pins <= 10, "Pin count exceeds pins on the lane"); + } + } + + private static void checkNegativeRoll(int bowl) { + checkState(bowl >= 0, "Negative roll is invalid"); + } + + private static void checkState(boolean expression, String message) { + if (!expression) { + throw new IllegalStateException(message); + } + } +} diff --git a/exercises/bowling/build.gradle b/exercises/bowling/build.gradle index 82d705dfd..2a0633879 100644 --- a/exercises/bowling/build.gradle +++ b/exercises/bowling/build.gradle @@ -7,12 +7,13 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { testLogging { - exceptionFormat = 'short' + exceptionFormat = 'full' showStandardStreams = true events = ["passed", "failed", "skipped"] } diff --git a/exercises/bowling/src/test/java/BowlingTest.java b/exercises/bowling/src/test/java/BowlingTest.java index 80c368f59..161df931c 100755 --- a/exercises/bowling/src/test/java/BowlingTest.java +++ b/exercises/bowling/src/test/java/BowlingTest.java @@ -1,21 +1,12 @@ -import org.junit.Before; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; - -import static org.junit.Assert.assertEquals; public class BowlingTest { - private BowlingGame game; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Before - public void setup() { - game = new BowlingGame(); - } + private BowlingGame game = new BowlingGame(); private void playGame(int[] rolls) { for (int pins : rolls) { @@ -153,12 +144,10 @@ public void allStrikesIsAPerfectGame() { public void rollsCanNotScoreNegativePoints() { int[] rolls = {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - playGame(rolls); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Negative roll is invalid"); + IllegalStateException expected = + assertThrows(IllegalStateException.class, () -> playGame(rolls)); - game.score(); + assertThat(expected).hasMessage("Negative roll is invalid"); } @Ignore("Remove to run test") @@ -166,12 +155,10 @@ public void rollsCanNotScoreNegativePoints() { public void aRollCanNotScoreMoreThan10Points() { int[] rolls = {11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - playGame(rolls); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Pin count exceeds pins on the lane"); + IllegalStateException expected = + assertThrows(IllegalStateException.class, () -> playGame(rolls)); - game.score(); + assertThat(expected).hasMessage("Pin count exceeds pins on the lane"); } @Ignore("Remove to run test") @@ -179,12 +166,10 @@ public void aRollCanNotScoreMoreThan10Points() { public void twoRollsInAFrameCanNotScoreMoreThan10Points() { int[] rolls = {5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - playGame(rolls); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Pin count exceeds pins on the lane"); + IllegalStateException expected = + assertThrows(IllegalStateException.class, () -> playGame(rolls)); - game.score(); + assertThat(expected).hasMessage("Pin count exceeds pins on the lane"); } @Ignore("Remove to run test") @@ -192,12 +177,10 @@ public void twoRollsInAFrameCanNotScoreMoreThan10Points() { public void bonusRollAfterAStrikeInTheLastFrameCanNotScoreMoreThan10Points() { int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 0}; - playGame(rolls); + IllegalStateException expected = + assertThrows(IllegalStateException.class, () -> playGame(rolls)); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Pin count exceeds pins on the lane"); - - game.score(); + assertThat(expected).hasMessage("Pin count exceeds pins on the lane"); } @Ignore("Remove to run test") @@ -205,12 +188,10 @@ public void bonusRollAfterAStrikeInTheLastFrameCanNotScoreMoreThan10Points() { public void twoBonusRollsAfterAStrikeInTheLastFrameCanNotScoreMoreThan10Points() { int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 5, 6}; - playGame(rolls); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Pin count exceeds pins on the lane"); + IllegalStateException expected = + assertThrows(IllegalStateException.class, () -> playGame(rolls)); - game.score(); + assertThat(expected).hasMessage("Pin count exceeds pins on the lane"); } @Ignore("Remove to run test") @@ -228,12 +209,10 @@ public void twoBonusRollsAfterAStrikeInTheLastFrameCanScoreMoreThan10PointsIfOne public void theSecondBonusRollsAfterAStrikeInTheLastFrameCanNotBeAStrikeIfTheFirstOneIsNotAStrike() { int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6, 10}; - playGame(rolls); + IllegalStateException expected = + assertThrows(IllegalStateException.class, () -> playGame(rolls)); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Pin count exceeds pins on the lane"); - - game.score(); + assertThat(expected).hasMessage("Pin count exceeds pins on the lane"); } @Ignore("Remove to run test") @@ -241,12 +220,10 @@ public void theSecondBonusRollsAfterAStrikeInTheLastFrameCanNotBeAStrikeIfTheFir public void secondBonusRollAfterAStrikeInTheLastFrameCanNotScoreMoreThan10Points() { int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 11}; - playGame(rolls); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Pin count exceeds pins on the lane"); + IllegalStateException expected = + assertThrows(IllegalStateException.class, () -> playGame(rolls)); - game.score(); + assertThat(expected).hasMessage("Pin count exceeds pins on the lane"); } @Ignore("Remove to run test") @@ -256,10 +233,11 @@ public void anUnstartedGameCanNotBeScored() { playGame(rolls); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Score cannot be taken until the end of the game"); + IllegalStateException expected = + assertThrows(IllegalStateException.class, game::score); - game.score(); + assertThat(expected) + .hasMessage("Score cannot be taken until the end of the game"); } @Ignore("Remove to run test") @@ -269,10 +247,11 @@ public void anIncompleteGameCanNotBeScored() { playGame(rolls); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Score cannot be taken until the end of the game"); + IllegalStateException expected = + assertThrows(IllegalStateException.class, game::score); - game.score(); + assertThat(expected) + .hasMessage("Score cannot be taken until the end of the game"); } @Ignore("Remove to run test") @@ -280,12 +259,10 @@ public void anIncompleteGameCanNotBeScored() { public void canNotRollIfGameAlreadyHasTenFrames() { int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - playGame(rolls); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Cannot roll after game is over"); + IllegalStateException expected = + assertThrows(IllegalStateException.class, () -> playGame(rolls)); - game.score(); + assertThat(expected).hasMessage("Cannot roll after game is over"); } @Ignore("Remove to run test") @@ -295,10 +272,11 @@ public void bonusRollsForAStrikeInTheLastFrameMustBeRolledBeforeScoreCanBeCalcul playGame(rolls); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Score cannot be taken until the end of the game"); + IllegalStateException expected = + assertThrows(IllegalStateException.class, game::score); - game.score(); + assertThat(expected) + .hasMessage("Score cannot be taken until the end of the game"); } @Ignore("Remove to run test") @@ -308,10 +286,11 @@ public void bothBonusRollsForAStrikeInTheLastFrameMustBeRolledBeforeScoreCanBeCa playGame(rolls); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Score cannot be taken until the end of the game"); + IllegalStateException expected = + assertThrows(IllegalStateException.class, game::score); - game.score(); + assertThat(expected) + .hasMessage("Score cannot be taken until the end of the game"); } @Ignore("Remove to run test") @@ -321,10 +300,11 @@ public void bonusRollForASpareInTheLastFrameMustBeRolledBeforeScoreCanBeCalculat playGame(rolls); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Score cannot be taken until the end of the game"); + IllegalStateException expected = + assertThrows(IllegalStateException.class, game::score); - game.score(); + assertThat(expected) + .hasMessage("Score cannot be taken until the end of the game"); } @Ignore("Remove to run test") @@ -332,12 +312,10 @@ public void bonusRollForASpareInTheLastFrameMustBeRolledBeforeScoreCanBeCalculat public void canNotRollAfterBonusRollForSpare() { int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 2, 2}; - playGame(rolls); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Cannot roll after game is over"); + IllegalStateException expected = + assertThrows(IllegalStateException.class, () -> playGame(rolls)); - game.score(); + assertThat(expected).hasMessage("Cannot roll after game is over"); } @Ignore("Remove to run test") @@ -345,11 +323,9 @@ public void canNotRollAfterBonusRollForSpare() { public void canNotRollAfterBonusRollForStrike() { int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 3, 2, 2}; - playGame(rolls); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Cannot roll after game is over"); + IllegalStateException expected = + assertThrows(IllegalStateException.class, () -> playGame(rolls)); - game.score(); + assertThat(expected).hasMessage("Cannot roll after game is over"); } } diff --git a/exercises/change/build.gradle b/exercises/change/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/change/build.gradle +++ b/exercises/change/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/change/src/test/java/ChangeCalculatorTest.java b/exercises/change/src/test/java/ChangeCalculatorTest.java index 71d29ce12..fa1e42068 100644 --- a/exercises/change/src/test/java/ChangeCalculatorTest.java +++ b/exercises/change/src/test/java/ChangeCalculatorTest.java @@ -1,17 +1,14 @@ -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; -import static org.junit.Assert.assertEquals; -public class ChangeCalculatorTest { +import org.junit.Ignore; +import org.junit.Test; - @Rule - public ExpectedException expectedException = ExpectedException.none(); +public class ChangeCalculatorTest { @Test public void testChangeThatCanBeGivenInASingleCoin() { @@ -101,10 +98,13 @@ public void testZeroChange() { public void testChangeLessThanSmallestCoinInCurrencyCannotBeRepresented() { ChangeCalculator changeCalculator = new ChangeCalculator(asList(5, 10)); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("The total 3 cannot be represented in the given currency."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> changeCalculator.computeMostEfficientChange(3)); - changeCalculator.computeMostEfficientChange(3); + assertThat(expected) + .hasMessage("The total 3 cannot be represented in the given currency."); } @Ignore("Remove to run test") @@ -112,10 +112,13 @@ public void testChangeLessThanSmallestCoinInCurrencyCannotBeRepresented() { public void testChangeLargerThanAllCoinsInCurrencyThatCannotBeRepresented() { ChangeCalculator changeCalculator = new ChangeCalculator(asList(5, 10)); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("The total 94 cannot be represented in the given currency."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> changeCalculator.computeMostEfficientChange(94)); - changeCalculator.computeMostEfficientChange(94); + assertThat(expected) + .hasMessage("The total 94 cannot be represented in the given currency."); } @Ignore("Remove to run test") @@ -123,10 +126,13 @@ public void testChangeLargerThanAllCoinsInCurrencyThatCannotBeRepresented() { public void testNegativeChangeIsRejected() { ChangeCalculator changeCalculator = new ChangeCalculator(asList(1, 2, 5)); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Negative totals are not allowed."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> changeCalculator.computeMostEfficientChange(-5)); - changeCalculator.computeMostEfficientChange(-5); + assertThat(expected) + .hasMessage("Negative totals are not allowed."); } } diff --git a/exercises/circular-buffer/build.gradle b/exercises/circular-buffer/build.gradle index 20f3abb34..950f15a95 100644 --- a/exercises/circular-buffer/build.gradle +++ b/exercises/circular-buffer/build.gradle @@ -7,8 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" - testImplementation "org.assertj:assertj-core:3.15.0" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/circular-buffer/src/test/java/CircularBufferTest.java b/exercises/circular-buffer/src/test/java/CircularBufferTest.java index 906ecf07a..6a263f648 100644 --- a/exercises/circular-buffer/src/test/java/CircularBufferTest.java +++ b/exercises/circular-buffer/src/test/java/CircularBufferTest.java @@ -1,22 +1,20 @@ import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertThrows; import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; public class CircularBufferTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test - public void readingFromEmptyBufferShouldThrowException() throws BufferIOException { + public void readingFromEmptyBufferShouldThrowException() { CircularBuffer buffer = new CircularBuffer<>(1); - expectedException.expect(BufferIOException.class); - expectedException.expectMessage("Tried to read from empty buffer"); - buffer.read(); + BufferIOException expected = + assertThrows(BufferIOException.class, buffer::read); + + assertThat(expected) + .hasMessage("Tried to read from empty buffer"); } @Ignore("Remove to run test") @@ -36,9 +34,11 @@ public void canReadItemOnlyOnce() throws BufferIOException { buffer.write(1); assertThat(buffer.read()).isEqualTo(1); - expectedException.expect(BufferIOException.class); - expectedException.expectMessage("Tried to read from empty buffer"); - buffer.read(); + BufferIOException expected = + assertThrows(BufferIOException.class, buffer::read); + + assertThat(expected) + .hasMessage("Tried to read from empty buffer"); } @Ignore("Remove to run test") @@ -58,9 +58,12 @@ public void fullBufferCantBeWrittenTo() throws BufferIOException { CircularBuffer buffer = new CircularBuffer<>(1); buffer.write(1); - expectedException.expect(BufferIOException.class); - expectedException.expectMessage("Tried to write to full buffer"); - buffer.write(2); + + BufferIOException expected = + assertThrows(BufferIOException.class, () -> buffer.write(2)); + + assertThat(expected) + .hasMessage("Tried to write to full buffer"); } @Ignore("Remove to run test") @@ -94,9 +97,12 @@ public void cantReadClearedItems() throws BufferIOException { buffer.write(1); buffer.clear(); - expectedException.expect(BufferIOException.class); - expectedException.expectMessage("Tried to read from empty buffer"); - buffer.read(); + + BufferIOException expected = + assertThrows(BufferIOException.class, buffer::read); + + assertThat(expected) + .hasMessage("Tried to read from empty buffer"); } @Ignore("Remove to run test") @@ -171,9 +177,12 @@ public void initialClearDoesNotAffectWrappingAround() throws BufferIOException { buffer.overwrite(4); assertThat(buffer.read()).isEqualTo(3); assertThat(buffer.read()).isEqualTo(4); - expectedException.expect(BufferIOException.class); - expectedException.expectMessage("Tried to read from empty buffer"); - buffer.read(); + + BufferIOException expected = + assertThrows(BufferIOException.class, buffer::read); + + assertThat(expected) + .hasMessage("Tried to read from empty buffer"); } } diff --git a/exercises/clock/build.gradle b/exercises/clock/build.gradle index 479746285..950f15a95 100644 --- a/exercises/clock/build.gradle +++ b/exercises/clock/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/collatz-conjecture/build.gradle b/exercises/collatz-conjecture/build.gradle index 479746285..950f15a95 100644 --- a/exercises/collatz-conjecture/build.gradle +++ b/exercises/collatz-conjecture/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/collatz-conjecture/src/test/java/CollatzCalculatorTest.java b/exercises/collatz-conjecture/src/test/java/CollatzCalculatorTest.java index dd57b1d41..1537df278 100644 --- a/exercises/collatz-conjecture/src/test/java/CollatzCalculatorTest.java +++ b/exercises/collatz-conjecture/src/test/java/CollatzCalculatorTest.java @@ -1,22 +1,13 @@ -import org.junit.Before; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; - -import static org.junit.Assert.assertEquals; public class CollatzCalculatorTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - private CollatzCalculator collatzCalculator; - - @Before - public void setUp() { - collatzCalculator = new CollatzCalculator(); - } + private CollatzCalculator collatzCalculator = new CollatzCalculator(); @Test public void testZeroStepsRequiredWhenStartingFrom1() { @@ -44,19 +35,25 @@ public void testAVeryLargeInput() { @Ignore("Remove to run test") @Test public void testZeroIsConsideredInvalidInput() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Only natural numbers are allowed"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> collatzCalculator.computeStepCount(0)); - collatzCalculator.computeStepCount(0); + assertThat(expected) + .hasMessage("Only natural numbers are allowed"); } @Ignore("Remove to run test") @Test public void testNegativeIntegerIsConsideredInvalidInput() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Only natural numbers are allowed"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> collatzCalculator.computeStepCount(-15)); - collatzCalculator.computeStepCount(-15); + assertThat(expected) + .hasMessage("Only natural numbers are allowed"); } } diff --git a/exercises/complex-numbers/build.gradle b/exercises/complex-numbers/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/complex-numbers/build.gradle +++ b/exercises/complex-numbers/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/crypto-square/build.gradle b/exercises/crypto-square/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/crypto-square/build.gradle +++ b/exercises/crypto-square/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/custom-set/build.gradle b/exercises/custom-set/build.gradle index 479746285..950f15a95 100644 --- a/exercises/custom-set/build.gradle +++ b/exercises/custom-set/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/darts/build.gradle b/exercises/darts/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/darts/build.gradle +++ b/exercises/darts/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/diamond/build.gradle b/exercises/diamond/build.gradle index 952e38529..96c8acd4c 100644 --- a/exercises/diamond/build.gradle +++ b/exercises/diamond/build.gradle @@ -7,7 +7,7 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" testImplementation "org.assertj:assertj-core:3.15.0" } diff --git a/exercises/diamond/src/test/java/DiamondPrinterTest.java b/exercises/diamond/src/test/java/DiamondPrinterTest.java index d9d894c84..2b7644e36 100644 --- a/exercises/diamond/src/test/java/DiamondPrinterTest.java +++ b/exercises/diamond/src/test/java/DiamondPrinterTest.java @@ -4,8 +4,6 @@ import org.junit.Before; import org.junit.Test; -import java.util.List; - public class DiamondPrinterTest { private DiamondPrinter diamondPrinter; diff --git a/exercises/difference-of-squares/build.gradle b/exercises/difference-of-squares/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/difference-of-squares/build.gradle +++ b/exercises/difference-of-squares/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/diffie-hellman/build.gradle b/exercises/diffie-hellman/build.gradle index 75cda882d..d74934516 100644 --- a/exercises/diffie-hellman/build.gradle +++ b/exercises/diffie-hellman/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/dnd-character/build.gradle b/exercises/dnd-character/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/dnd-character/build.gradle +++ b/exercises/dnd-character/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/dominoes/build.gradle b/exercises/dominoes/build.gradle index 479746285..950f15a95 100644 --- a/exercises/dominoes/build.gradle +++ b/exercises/dominoes/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/dominoes/src/main/java/ChainNotFoundException.java b/exercises/dominoes/src/main/java/ChainNotFoundException.java index 1881942da..f55e8f089 100644 --- a/exercises/dominoes/src/main/java/ChainNotFoundException.java +++ b/exercises/dominoes/src/main/java/ChainNotFoundException.java @@ -1,5 +1,5 @@ class ChainNotFoundException extends Exception { - public ChainNotFoundException(String message) { - super(message); - } + public ChainNotFoundException(String message) { + super(message); + } } diff --git a/exercises/dominoes/src/main/java/Domino.java b/exercises/dominoes/src/main/java/Domino.java index 4953f1d20..b3cf21c8f 100644 --- a/exercises/dominoes/src/main/java/Domino.java +++ b/exercises/dominoes/src/main/java/Domino.java @@ -9,22 +9,22 @@ class Domino { } int getLeft() { - return this.left; + return this.left; } int getRight() { - return this.right; + return this.right; } @Override public boolean equals(Object o) { - Domino otherDomino = (Domino) o; - return (this.getLeft() == otherDomino.getLeft() && this.getRight() == otherDomino.getRight()) || - (this.getLeft() == otherDomino.getRight() && this.getRight() == otherDomino.getLeft()); + Domino otherDomino = (Domino) o; + return (this.getLeft() == otherDomino.getLeft() && this.getRight() == otherDomino.getRight()) || + (this.getLeft() == otherDomino.getRight() && this.getRight() == otherDomino.getLeft()); } @Override public int hashCode() { - return Objects.hash(left, right); + return Objects.hash(left, right); } } diff --git a/exercises/dominoes/src/test/java/DominoesTest.java b/exercises/dominoes/src/test/java/DominoesTest.java index 7a0729097..0d27072c3 100644 --- a/exercises/dominoes/src/test/java/DominoesTest.java +++ b/exercises/dominoes/src/test/java/DominoesTest.java @@ -1,3 +1,7 @@ +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; import org.junit.Test; @@ -6,16 +10,8 @@ import java.util.Arrays; import java.util.Collections; -import static org.junit.Assert.assertEquals; - -import org.junit.Rule; -import org.junit.rules.ExpectedException; - public class DominoesTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void emtpyInputEmptyOutputTest() throws ChainNotFoundException { Dominoes dominoes = new Dominoes(); @@ -42,16 +38,18 @@ public void singletonInputSingletonOutput() throws ChainNotFoundException { @Ignore("Remove to run test") @Test - public void singletonCantBeChainedTest() throws ChainNotFoundException { + public void singletonCantBeChainedTest() { Dominoes dominoes = new Dominoes(); Domino[] dominoesArray = {new Domino(1, 2)}; List dominoesList = Arrays.asList(dominoesArray); - expectedException.expect(ChainNotFoundException.class); - expectedException.expectMessage("No domino chain found."); + ChainNotFoundException expected = + assertThrows( + ChainNotFoundException.class, + () -> dominoes.formChain(dominoesList)); - dominoes.formChain(dominoesList); + assertThat(expected).hasMessage("No domino chain found."); } @Ignore("Remove to run test") @@ -82,58 +80,66 @@ public void canReverseDominoesTest() throws ChainNotFoundException { @Ignore("Remove to run test") @Test - public void cantBeChainedTest() throws ChainNotFoundException { + public void cantBeChainedTest() { Dominoes dominoes = new Dominoes(); Domino[] dominoesArray = {new Domino(1, 2), new Domino(4, 1), new Domino(2, 3)}; List dominoesList = Arrays.asList(dominoesArray); - expectedException.expect(ChainNotFoundException.class); - expectedException.expectMessage("No domino chain found."); + ChainNotFoundException expected = + assertThrows( + ChainNotFoundException.class, + () -> dominoes.formChain(dominoesList)); - dominoes.formChain(dominoesList); + assertThat(expected).hasMessage("No domino chain found."); } @Ignore("Remove to run test") @Test - public void disconnectedSimpleTest() throws ChainNotFoundException { + public void disconnectedSimpleTest() { Dominoes dominoes = new Dominoes(); Domino[] dominoesArray = {new Domino(1, 1), new Domino(2, 2)}; List dominoesList = Arrays.asList(dominoesArray); - expectedException.expect(ChainNotFoundException.class); - expectedException.expectMessage("No domino chain found."); + ChainNotFoundException expected = + assertThrows( + ChainNotFoundException.class, + () -> dominoes.formChain(dominoesList)); - dominoes.formChain(dominoesList); + assertThat(expected).hasMessage("No domino chain found."); } @Ignore("Remove to run test") @Test - public void disconnectedDoubleLoopTest() throws ChainNotFoundException { + public void disconnectedDoubleLoopTest() { Dominoes dominoes = new Dominoes(); Domino[] dominoesArray = {new Domino(1, 2), new Domino(2, 1), new Domino(3, 4), new Domino(4, 3)}; List dominoesList = Arrays.asList(dominoesArray); - expectedException.expect(ChainNotFoundException.class); - expectedException.expectMessage("No domino chain found."); + ChainNotFoundException expected = + assertThrows( + ChainNotFoundException.class, + () -> dominoes.formChain(dominoesList)); - dominoes.formChain(dominoesList); + assertThat(expected).hasMessage("No domino chain found."); } @Ignore("Remove to run test") @Test - public void disconnectedSingleIsolatedTest() throws ChainNotFoundException { + public void disconnectedSingleIsolatedTest() { Dominoes dominoes = new Dominoes(); Domino[] dominoesArray = {new Domino(1, 2), new Domino(2, 3), new Domino(3, 1), new Domino(4, 4)}; List dominoesList = Arrays.asList(dominoesArray); - expectedException.expect(ChainNotFoundException.class); - expectedException.expectMessage("No domino chain found."); + ChainNotFoundException expected = + assertThrows( + ChainNotFoundException.class, + () -> dominoes.formChain(dominoesList)); - dominoes.formChain(dominoesList); + assertThat(expected).hasMessage("No domino chain found."); } @Ignore("Remove to run test") diff --git a/exercises/error-handling/build.gradle b/exercises/error-handling/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/error-handling/build.gradle +++ b/exercises/error-handling/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/error-handling/src/test/java/ErrorHandlingTest.java b/exercises/error-handling/src/test/java/ErrorHandlingTest.java index 87ff3678a..4f8947571 100644 --- a/exercises/error-handling/src/test/java/ErrorHandlingTest.java +++ b/exercises/error-handling/src/test/java/ErrorHandlingTest.java @@ -1,105 +1,119 @@ -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import org.junit.Ignore; +import org.junit.Test; + import java.util.Optional; public class ErrorHandlingTest { - private ErrorHandling errorHandling; - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Before - public void setUp() { - errorHandling = new ErrorHandling(); - } + private ErrorHandling errorHandling = new ErrorHandling(); @Test public void testThrowIllegalArgumentException() { - thrown.expect(IllegalArgumentException.class); - errorHandling.handleErrorByThrowingIllegalArgumentException(); + assertThrows( + IllegalArgumentException.class, + errorHandling::handleErrorByThrowingIllegalArgumentException); } @Ignore("Remove to run test") @Test public void testThrowIllegalArgumentExceptionWithDetailMessage() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("This is the detail message."); - errorHandling.handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage("This is the detail message."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> errorHandling + .handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage( + "This is the detail message.")); + + assertThat(expected).hasMessage("This is the detail message."); } @Ignore("Remove to run test") @Test public void testThrowAnyCheckedException() { - try { - errorHandling.handleErrorByThrowingAnyCheckedException(); - } catch (Exception e) { - assertFalse(e instanceof RuntimeException); - } + Exception expected = + assertThrows( + Exception.class, + errorHandling::handleErrorByThrowingAnyCheckedException); + assertThat(expected).isNotInstanceOf(RuntimeException.class); } @Ignore("Remove to run test") @Test public void testThrowAnyCheckedExceptionWithDetailMessage() { - try { - errorHandling.handleErrorByThrowingAnyCheckedExceptionWithDetailMessage("This is the detail message."); - } catch (Exception e) { - assertFalse(e instanceof RuntimeException); - assertEquals("This is the detail message.", e.getMessage()); - } + Exception expected = + assertThrows( + Exception.class, + () -> errorHandling + .handleErrorByThrowingAnyCheckedExceptionWithDetailMessage( + "This is the detail message.")); + assertThat(expected).isNotInstanceOf(RuntimeException.class); + assertThat(expected).hasMessage("This is the detail message."); } @Ignore("Remove to run test") @Test public void testThrowAnyUncheckedException() { - thrown.expect(RuntimeException.class); - errorHandling.handleErrorByThrowingAnyUncheckedException(); + assertThrows( + RuntimeException.class, + errorHandling::handleErrorByThrowingAnyUncheckedException); } @Ignore("Remove to run test") @Test public void testThrowAnyUncheckedExceptionWithDetailMessage() { - thrown.expect(RuntimeException.class); - thrown.expectMessage("This is the detail message."); - errorHandling.handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage("This is the detail message."); + RuntimeException expected = + assertThrows( + RuntimeException.class, + () -> errorHandling + .handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage( + "This is the detail message.")); + assertThat(expected).hasMessage("This is the detail message."); } @Ignore("Remove to run test") @Test - public void testThrowCustomCheckedException() throws CustomCheckedException { - thrown.expect(CustomCheckedException.class); - errorHandling.handleErrorByThrowingCustomCheckedException(); + public void testThrowCustomCheckedException() { + assertThrows( + CustomCheckedException.class, + errorHandling::handleErrorByThrowingCustomCheckedException); } @Ignore("Remove to run test") @Test - public void testThrowCustomCheckedExceptionWithDetailMessage() throws CustomCheckedException { - thrown.expect(CustomCheckedException.class); - thrown.expectMessage("This is the detail message."); - errorHandling.handleErrorByThrowingCustomCheckedExceptionWithDetailMessage("This is the detail message."); + public void testThrowCustomCheckedExceptionWithDetailMessage() { + CustomCheckedException expected = + assertThrows( + CustomCheckedException.class, + () -> errorHandling + .handleErrorByThrowingCustomCheckedExceptionWithDetailMessage( + "This is the detail message.")); + assertThat(expected).hasMessage("This is the detail message."); } @Ignore("Remove to run test") @Test public void testThrowCustomUncheckedException() { - thrown.expect(CustomUncheckedException.class); - errorHandling.handleErrorByThrowingCustomUncheckedException(); + assertThrows( + CustomUncheckedException.class, + errorHandling::handleErrorByThrowingCustomUncheckedException); } @Ignore("Remove to run test") @Test public void testThrowCustomUncheckedExceptionWithDetailMessage() { - thrown.expect(CustomUncheckedException.class); - thrown.expectMessage("This is the detail message."); - errorHandling.handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage("This is the detail message."); + CustomUncheckedException expected = + assertThrows( + CustomUncheckedException.class, + () -> errorHandling + .handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage( + "This is the detail message.")); + assertThat(expected).hasMessage("This is the detail message."); } @Ignore("Remove to run test") diff --git a/exercises/etl/build.gradle b/exercises/etl/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/etl/build.gradle +++ b/exercises/etl/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/flatten-array/build.gradle b/exercises/flatten-array/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/flatten-array/build.gradle +++ b/exercises/flatten-array/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/food-chain/build.gradle b/exercises/food-chain/build.gradle index 6c26f4b12..2189900ea 100644 --- a/exercises/food-chain/build.gradle +++ b/exercises/food-chain/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { testLogging { diff --git a/exercises/forth/build.gradle b/exercises/forth/build.gradle index 479746285..950f15a95 100644 --- a/exercises/forth/build.gradle +++ b/exercises/forth/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/forth/src/test/java/ForthEvaluatorTest.java b/exercises/forth/src/test/java/ForthEvaluatorTest.java index 6c426c455..69ad78141 100644 --- a/exercises/forth/src/test/java/ForthEvaluatorTest.java +++ b/exercises/forth/src/test/java/ForthEvaluatorTest.java @@ -1,25 +1,16 @@ -import org.junit.Before; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.util.Arrays; import java.util.Collections; -import static org.junit.Assert.assertEquals; - public class ForthEvaluatorTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - private ForthEvaluator forthEvaluator; - - @Before - public void setUp() { - forthEvaluator = new ForthEvaluator(); - } + private ForthEvaluator forthEvaluator = new ForthEvaluator(); @Test public void testNumbersAreJustPushedOntoTheStack() { @@ -39,19 +30,29 @@ public void testTwoNumbersCanBeAdded() { @Ignore("Remove to run test") @Test public void testErrorIfAdditionAttemptedWithNothingOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Addition requires that the stack contain at least 2 values"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("+"))); - forthEvaluator.evaluateProgram(Collections.singletonList("+")); + assertThat(expected) + .hasMessage( + "Addition requires that the stack contain at least 2 values"); } @Ignore("Remove to run test") @Test public void testErrorIfAdditionAttemptedWithOneNumberOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Addition requires that the stack contain at least 2 values"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("1 +"))); - forthEvaluator.evaluateProgram(Collections.singletonList("1 +")); + assertThat(expected) + .hasMessage( + "Addition requires that the stack contain at least 2 values"); } @Ignore("Remove to run test") @@ -65,19 +66,29 @@ public void testTwoNumbersCanBeSubtracted() { @Ignore("Remove to run test") @Test public void testErrorIfSubtractionAttemptedWithNothingOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Subtraction requires that the stack contain at least 2 values"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("-"))); - forthEvaluator.evaluateProgram(Collections.singletonList("-")); + assertThat(expected) + .hasMessage( + "Subtraction requires that the stack contain at least 2 values"); } @Ignore("Remove to run test") @Test public void testErrorIfSubtractionAttemptedWithOneNumberOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Subtraction requires that the stack contain at least 2 values"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("1 -"))); - forthEvaluator.evaluateProgram(Collections.singletonList("1 -")); + assertThat(expected) + .hasMessage( + "Subtraction requires that the stack contain at least 2 values"); } @Ignore("Remove to run test") @@ -91,19 +102,29 @@ public void testTwoNumbersCanBeMultiplied() { @Ignore("Remove to run test") @Test public void testErrorIfMultiplicationAttemptedWithNothingOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Multiplication requires that the stack contain at least 2 values"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("*"))); - forthEvaluator.evaluateProgram(Collections.singletonList("*")); + assertThat(expected) + .hasMessage( + "Multiplication requires that the stack contain at least 2 values"); } @Ignore("Remove to run test") @Test public void testErrorIfMultiplicationAttemptedWithOneNumberOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Multiplication requires that the stack contain at least 2 values"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("1 *"))); - forthEvaluator.evaluateProgram(Collections.singletonList("1 *")); + assertThat(expected) + .hasMessage( + "Multiplication requires that the stack contain at least 2 values"); } @Ignore("Remove to run test") @@ -125,28 +146,42 @@ public void testThatIntegerDivisionIsUsed() { @Ignore("Remove to run test") @Test public void testErrorIfDividingByZero() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Division by 0 is not allowed"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("4 0 /"))); - forthEvaluator.evaluateProgram(Collections.singletonList("4 0 /")); + assertThat(expected) + .hasMessage("Division by 0 is not allowed"); } @Ignore("Remove to run test") @Test public void testErrorIfDivisionAttemptedWithNothingOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Division requires that the stack contain at least 2 values"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("/"))); - forthEvaluator.evaluateProgram(Collections.singletonList("/")); + assertThat(expected) + .hasMessage( + "Division requires that the stack contain at least 2 values"); } @Ignore("Remove to run test") @Test public void testErrorIfDivisionAttemptedWithOneNumberOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Division requires that the stack contain at least 2 values"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("1 /"))); - forthEvaluator.evaluateProgram(Collections.singletonList("1 /")); + assertThat(expected) + .hasMessage( + "Division requires that the stack contain at least 2 values"); } @Ignore("Remove to run test") @@ -184,10 +219,15 @@ public void testDupCopiesTopValueOnTheStack() { @Ignore("Remove to run test") @Test public void testErrorIfDuplicatingAttemptedWithNothingOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Duplicating requires that the stack contain at least 1 value"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("dup"))); - forthEvaluator.evaluateProgram(Collections.singletonList("dup")); + assertThat(expected) + .hasMessage( + "Duplicating requires that the stack contain at least 1 value"); } @Ignore("Remove to run test") @@ -209,10 +249,15 @@ public void testDropRemovesTheTopValueOnTheStackIfItIsNotTheOnlyOne() { @Ignore("Remove to run test") @Test public void testErrorIfDroppingAttemptedWithNothingOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Dropping requires that the stack contain at least 1 value"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("drop"))); - forthEvaluator.evaluateProgram(Collections.singletonList("drop")); + assertThat(expected) + .hasMessage( + "Dropping requires that the stack contain at least 1 value"); } @Ignore("Remove to run test") @@ -234,19 +279,29 @@ public void testSwapSwapsTheTopTwosValueOnTheStackIfTheyAreNotTheOnlyOnes() { @Ignore("Remove to run test") @Test public void testErrorIfSwappingAttemptedWithNothingOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Swapping requires that the stack contain at least 2 values"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("swap"))); - forthEvaluator.evaluateProgram(Collections.singletonList("swap")); + assertThat(expected) + .hasMessage( + "Swapping requires that the stack contain at least 2 values"); } @Ignore("Remove to run test") @Test public void testErrorIfSwappingAttemptedWithOneNumberOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Swapping requires that the stack contain at least 2 values"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("1 swap"))); - forthEvaluator.evaluateProgram(Collections.singletonList("1 swap")); + assertThat(expected) + .hasMessage( + "Swapping requires that the stack contain at least 2 values"); } @Ignore("Remove to run test") @@ -268,19 +323,29 @@ public void testOverCopiesTheSecondElementIfThereAreMoreThanTwo() { @Ignore("Remove to run test") @Test public void testErrorIfOveringAttemptedWithNothingOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Overing requires that the stack contain at least 2 values"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("over"))); - forthEvaluator.evaluateProgram(Collections.singletonList("over")); + assertThat(expected) + .hasMessage( + "Overing requires that the stack contain at least 2 values"); } @Ignore("Remove to run test") @Test public void testErrorIfOveringAttemptedWithOneNumberOnTheStack() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Overing requires that the stack contain at least 2 values"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("1 over"))); - forthEvaluator.evaluateProgram(Collections.singletonList("1 over")); + assertThat(expected) + .hasMessage( + "Overing requires that the stack contain at least 2 values"); } @Ignore("Remove to run test") @@ -342,19 +407,26 @@ public void testCanDefineWordThatUsesWordWithTheSameName() { @Ignore("Remove to run test") @Test public void testCannotRedefineNumbers() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Cannot redefine numbers"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList(": 1 2 ;"))); - forthEvaluator.evaluateProgram(Collections.singletonList(": 1 2 ;")); + assertThat(expected).hasMessage("Cannot redefine numbers"); } @Ignore("Remove to run test") @Test public void testErrorIfEvaluatingAnUndefinedOperator() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("No definition available for operator \"foo\""); - - forthEvaluator.evaluateProgram(Collections.singletonList("foo")); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> forthEvaluator + .evaluateProgram(Collections.singletonList("foo"))); + + assertThat(expected) + .hasMessage("No definition available for operator \"foo\""); } @Ignore("Remove to run test") diff --git a/exercises/gigasecond/build.gradle b/exercises/gigasecond/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/gigasecond/build.gradle +++ b/exercises/gigasecond/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/go-counting/build.gradle b/exercises/go-counting/build.gradle index 479746285..950f15a95 100644 --- a/exercises/go-counting/build.gradle +++ b/exercises/go-counting/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/go-counting/src/test/java/GoCountingTest.java b/exercises/go-counting/src/test/java/GoCountingTest.java index 06ae47f36..60a714ba9 100644 --- a/exercises/go-counting/src/test/java/GoCountingTest.java +++ b/exercises/go-counting/src/test/java/GoCountingTest.java @@ -1,3 +1,7 @@ +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; import org.junit.Test; @@ -6,16 +10,8 @@ import java.util.HashSet; import java.util.Set; -import static org.junit.Assert.assertEquals; - -import org.junit.Rule; -import org.junit.rules.ExpectedException; - public class GoCountingTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - String board5x5 = " B \n" + " B B \n" + "B W B\n" + @@ -77,10 +73,12 @@ public void stoneNotTerritory5x5Board() { public void invalidXTooLow5x5Board() { GoCounting gocounting = new GoCounting(board5x5); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Invalid coordinate"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> gocounting.getTerritory(-1, 1)); - gocounting.getTerritory(-1, 1); + assertThat(expected).hasMessage("Invalid coordinate"); } @Ignore("Remove to run test") @@ -88,10 +86,12 @@ public void invalidXTooLow5x5Board() { public void invalidXTooHigh5x5Board() { GoCounting gocounting = new GoCounting(board5x5); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Invalid coordinate"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> gocounting.getTerritory(5, 1)); - gocounting.getTerritory(5, 1); + assertThat(expected).hasMessage("Invalid coordinate"); } @Ignore("Remove to run test") @@ -99,10 +99,12 @@ public void invalidXTooHigh5x5Board() { public void invalidYTooLow5x5Board() { GoCounting gocounting = new GoCounting(board5x5); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Invalid coordinate"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> gocounting.getTerritory(1, -1)); - gocounting.getTerritory(1, -1); + assertThat(expected).hasMessage("Invalid coordinate"); } @Ignore("Remove to run test") @@ -110,10 +112,12 @@ public void invalidYTooLow5x5Board() { public void invalidYTooHigh5x5Board() { GoCounting gocounting = new GoCounting(board5x5); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Invalid coordinate"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> gocounting.getTerritory(1, 5)); - gocounting.getTerritory(1, 5); + assertThat(expected).hasMessage("Invalid coordinate"); } @Ignore("Remove to run test") diff --git a/exercises/grade-school/build.gradle b/exercises/grade-school/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/grade-school/build.gradle +++ b/exercises/grade-school/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/grains/build.gradle b/exercises/grains/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/grains/build.gradle +++ b/exercises/grains/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/grains/src/test/java/GrainsTest.java b/exercises/grains/src/test/java/GrainsTest.java index 74d6ab9e4..0755a6381 100644 --- a/exercises/grains/src/test/java/GrainsTest.java +++ b/exercises/grains/src/test/java/GrainsTest.java @@ -1,26 +1,15 @@ -import org.junit.Before; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.math.BigInteger; -import static org.junit.Assert.assertEquals; - public class GrainsTest { - private static final String wrongSquareMessage = "square must be between 1 and 64"; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - private Grains grains; - - @Before - public void setup() { - grains = new Grains(); - } + private Grains grains = new Grains(); @Test public void countAtSquare1() { @@ -73,25 +62,37 @@ public void countAtSquare64() { @Ignore("Remove to run test") @Test public void errorOnNullBoardSize() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(wrongSquareMessage); - grains.grainsOnSquare(0); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> grains.grainsOnSquare(0)); + + assertThat(expected) + .hasMessage("square must be between 1 and 64"); } @Ignore("Remove to run test") @Test public void errorOnNegativeBoardSize() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(wrongSquareMessage); - grains.grainsOnSquare(-1); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> grains.grainsOnSquare(-1)); + + assertThat(expected) + .hasMessage("square must be between 1 and 64"); } @Ignore("Remove to run test") @Test public void errorOnExcessiveBoardSize() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(wrongSquareMessage); - grains.grainsOnSquare(65); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> grains.grainsOnSquare(65)); + + assertThat(expected) + .hasMessage("square must be between 1 and 64"); } @Ignore("Remove to run test") diff --git a/exercises/grep/build.gradle b/exercises/grep/build.gradle index 479746285..950f15a95 100644 --- a/exercises/grep/build.gradle +++ b/exercises/grep/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/hamming/build.gradle b/exercises/hamming/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/hamming/build.gradle +++ b/exercises/hamming/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/hamming/src/test/java/HammingTest.java b/exercises/hamming/src/test/java/HammingTest.java index 58ff48fca..ae76ee17f 100644 --- a/exercises/hamming/src/test/java/HammingTest.java +++ b/exercises/hamming/src/test/java/HammingTest.java @@ -1,16 +1,12 @@ -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import org.junit.Ignore; +import org.junit.Test; public class HammingTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void testNoDistanceBetweenEmptyStrands() { assertEquals(0, new Hamming("", "").getHammingDistance()); @@ -43,37 +39,49 @@ public void testDistanceInLongDifferentStrands() { @Ignore("Remove to run test") @Test public void testValidatesFirstStrandNotLonger() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("leftStrand and rightStrand must be of equal length."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new Hamming("AATG", "AAA")); - new Hamming("AATG", "AAA"); + assertThat(expected) + .hasMessage("leftStrand and rightStrand must be of equal length."); } @Ignore("Remove to run test") @Test public void testValidatesSecondStrandNotLonger() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("leftStrand and rightStrand must be of equal length."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new Hamming("ATA", "AGTG")); - new Hamming("ATA", "AGTG"); + assertThat(expected) + .hasMessage("leftStrand and rightStrand must be of equal length."); } @Ignore("Remove to run test") @Test public void testDisallowLeftEmptyStrand() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("left strand must not be empty."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new Hamming("", "G")); - new Hamming("", "G"); + assertThat(expected) + .hasMessage("left strand must not be empty."); } @Ignore("Remove to run test") @Test public void testDisallowRightEmptyStrand() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("right strand must not be empty."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new Hamming("G", "")); - new Hamming("G", ""); + assertThat(expected) + .hasMessage("right strand must not be empty."); } } diff --git a/exercises/hello-world/build.gradle b/exercises/hello-world/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/hello-world/build.gradle +++ b/exercises/hello-world/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/hexadecimal/build.gradle b/exercises/hexadecimal/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/hexadecimal/build.gradle +++ b/exercises/hexadecimal/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/house/build.gradle b/exercises/house/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/house/build.gradle +++ b/exercises/house/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/isbn-verifier/build.gradle b/exercises/isbn-verifier/build.gradle index 479746285..950f15a95 100644 --- a/exercises/isbn-verifier/build.gradle +++ b/exercises/isbn-verifier/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/isogram/build.gradle b/exercises/isogram/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/isogram/build.gradle +++ b/exercises/isogram/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/kindergarten-garden/build.gradle b/exercises/kindergarten-garden/build.gradle index 479746285..950f15a95 100644 --- a/exercises/kindergarten-garden/build.gradle +++ b/exercises/kindergarten-garden/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/kindergarten-garden/src/main/java/Plant.java b/exercises/kindergarten-garden/src/main/java/Plant.java index 17feb6b57..9145509a5 100644 --- a/exercises/kindergarten-garden/src/main/java/Plant.java +++ b/exercises/kindergarten-garden/src/main/java/Plant.java @@ -18,4 +18,4 @@ static Plant getPlant(char plantCode) { return null; } -} \ No newline at end of file +} diff --git a/exercises/knapsack/build.gradle b/exercises/knapsack/build.gradle index 019e5f323..ecc836fb5 100644 --- a/exercises/knapsack/build.gradle +++ b/exercises/knapsack/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/largest-series-product/build.gradle b/exercises/largest-series-product/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/largest-series-product/build.gradle +++ b/exercises/largest-series-product/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/largest-series-product/src/test/java/LargestSeriesProductCalculatorTest.java b/exercises/largest-series-product/src/test/java/LargestSeriesProductCalculatorTest.java index 9cf10a7d5..5a246d058 100644 --- a/exercises/largest-series-product/src/test/java/LargestSeriesProductCalculatorTest.java +++ b/exercises/largest-series-product/src/test/java/LargestSeriesProductCalculatorTest.java @@ -1,15 +1,12 @@ +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; - -import static org.junit.Assert.assertEquals; public class LargestSeriesProductCalculatorTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void testCorrectlyCalculatesLargestProductWhenSeriesLengthEqualsStringToSearchLength() { LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("29"); @@ -115,11 +112,14 @@ public void testCorrectlyCalculatesLargestProductOfZeroIfAllSeriesOfGivenLengthC public void testSeriesLengthLongerThanLengthOfStringToTestIsRejected() { LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("123"); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage( - "Series length must be less than or equal to the length of the string to search."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> calculator.calculateLargestProductForSeriesLength(4)); - calculator.calculateLargestProductForSeriesLength(4); + assertThat(expected) + .hasMessage( + "Series length must be less than or equal to the length of the string to search."); } @Ignore("Remove to run test") @@ -149,20 +149,26 @@ public void testCorrectlyCalculatesLargestProductOfLength0ForNonEmptyStringToSea public void testEmptyStringToSearchAndSeriesOfNonZeroLengthIsRejected() { LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator(""); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage( - "Series length must be less than or equal to the length of the string to search."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> calculator.calculateLargestProductForSeriesLength(1)); - calculator.calculateLargestProductForSeriesLength(1); + assertThat(expected) + .hasMessage( + "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() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("String to search may only contain digits."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new LargestSeriesProductCalculator("1234a5")); - new LargestSeriesProductCalculator("1234a5"); + assertThat(expected) + .hasMessage("String to search may only contain digits."); } @Ignore("Remove to run test") @@ -170,10 +176,13 @@ public void testStringToSearchContainingNonDigitCharacterIsRejected() { public void testNegativeSeriesLengthIsRejected() { LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("12345"); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Series length must be non-negative."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> calculator.calculateLargestProductForSeriesLength(-1)); - calculator.calculateLargestProductForSeriesLength(-1); + assertThat(expected) + .hasMessage("Series length must be non-negative."); } } diff --git a/exercises/leap/build.gradle b/exercises/leap/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/leap/build.gradle +++ b/exercises/leap/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/linked-list/build.gradle b/exercises/linked-list/build.gradle index 952e38529..96c8acd4c 100644 --- a/exercises/linked-list/build.gradle +++ b/exercises/linked-list/build.gradle @@ -7,7 +7,7 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" testImplementation "org.assertj:assertj-core:3.15.0" } diff --git a/exercises/list-ops/build.gradle b/exercises/list-ops/build.gradle index 479746285..950f15a95 100644 --- a/exercises/list-ops/build.gradle +++ b/exercises/list-ops/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/luhn/build.gradle b/exercises/luhn/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/luhn/build.gradle +++ b/exercises/luhn/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/markdown/build.gradle b/exercises/markdown/build.gradle index 479746285..950f15a95 100644 --- a/exercises/markdown/build.gradle +++ b/exercises/markdown/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/markdown/src/main/java/Markdown.java b/exercises/markdown/src/main/java/Markdown.java index 72caa27f2..6178f1beb 100644 --- a/exercises/markdown/src/main/java/Markdown.java +++ b/exercises/markdown/src/main/java/Markdown.java @@ -1,83 +1,10 @@ -class Markdown { - - String parse(String markdown) { +/* - String[] lines = markdown.split("\n"); +Since this exercise has a difficulty of > 4 it doesn't come +with any starter implementation. +This is so that you get to practice creating classes and methods +which is an important part of programming in Java. - String result = ""; +Please remove this comment when submitting your solution. - boolean activeList = false; - - for (int i = 0; i < lines.length; i++) { - - String theLine = parseHeader(lines[i]); - - if (theLine == null) { - theLine = parseListItem(lines[i]); - } - - if (theLine == null) - { - theLine = parseParagraph(lines[i]); - } - - if (theLine.matches("(
  • ).*") && !theLine.matches("().*") && !activeList) { - activeList = true; - result = result + "
      "; - result = result + theLine; - } - - else if (!theLine.matches("(
    • ).*") && activeList) { - activeList = false; - result = result + "
    "; - result = result + theLine; - } else { - result = result + theLine; - } - } - - if (activeList) { - result = result + ""; - } - - return result; - } - - private String parseHeader(String markdown) { - int count = 0; - - for (int i = 0; i < markdown.length() && markdown.charAt(i) == '#'; i++) - { - count++; - } - - if (count == 0) { return null; } - - return "" + markdown.substring(count + 1) + ""; - } - - private String parseListItem(String markdown) { - if (markdown.startsWith("*")) { - String skipAsterisk = markdown.substring(2); - String listItemString = parseSomeSymbols(skipAsterisk); - return "
  • " + listItemString + "
  • "; - } - - return null; - } - - private String parseParagraph(String markdown) { - return "

    " + parseSomeSymbols(markdown) + "

    "; - } - - private String parseSomeSymbols(String markdown) { - - String lookingFor = "__(.+)__"; - String update = "$1"; - String workingOn = markdown.replaceAll(lookingFor, update); - - lookingFor = "_(.+)_"; - update = "$1"; - return workingOn.replaceAll(lookingFor, update); - } -} \ No newline at end of file +*/ diff --git a/exercises/matching-brackets/build.gradle b/exercises/matching-brackets/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/matching-brackets/build.gradle +++ b/exercises/matching-brackets/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/matrix/build.gradle b/exercises/matrix/build.gradle index 479746285..950f15a95 100644 --- a/exercises/matrix/build.gradle +++ b/exercises/matrix/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/meetup/build.gradle b/exercises/meetup/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/meetup/build.gradle +++ b/exercises/meetup/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/minesweeper/build.gradle b/exercises/minesweeper/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/minesweeper/build.gradle +++ b/exercises/minesweeper/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/nth-prime/build.gradle b/exercises/nth-prime/build.gradle index 952e38529..96c8acd4c 100644 --- a/exercises/nth-prime/build.gradle +++ b/exercises/nth-prime/build.gradle @@ -7,7 +7,7 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" testImplementation "org.assertj:assertj-core:3.15.0" } diff --git a/exercises/nth-prime/src/test/java/PrimeCalculatorTest.java b/exercises/nth-prime/src/test/java/PrimeCalculatorTest.java index b59c4b245..38216cd88 100644 --- a/exercises/nth-prime/src/test/java/PrimeCalculatorTest.java +++ b/exercises/nth-prime/src/test/java/PrimeCalculatorTest.java @@ -1,21 +1,12 @@ import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertThrows; -import org.junit.Before; import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; public class PrimeCalculatorTest { - private PrimeCalculator primeCalculator; - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Before - public void setup() { - primeCalculator = new PrimeCalculator(); - } + private PrimeCalculator primeCalculator = new PrimeCalculator(); @Test public void testFirstPrime() { @@ -43,8 +34,9 @@ public void testBigPrime() { @Ignore("Remove to run test") @Test public void testUndefinedPrime() { - expectedException.expect(IllegalArgumentException.class); - primeCalculator.nth(0); + assertThrows( + IllegalArgumentException.class, + () -> primeCalculator.nth(0)); } } diff --git a/exercises/nucleotide-count/build.gradle b/exercises/nucleotide-count/build.gradle index 141356913..2a0633879 100644 --- a/exercises/nucleotide-count/build.gradle +++ b/exercises/nucleotide-count/build.gradle @@ -7,7 +7,7 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" testImplementation "org.assertj:assertj-core:3.15.0" } diff --git a/exercises/nucleotide-count/src/test/java/NucleotideCounterTest.java b/exercises/nucleotide-count/src/test/java/NucleotideCounterTest.java index 968b5260e..c6e236a9a 100644 --- a/exercises/nucleotide-count/src/test/java/NucleotideCounterTest.java +++ b/exercises/nucleotide-count/src/test/java/NucleotideCounterTest.java @@ -1,17 +1,13 @@ import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertThrows; import org.junit.Ignore; import org.junit.Test; -import org.junit.Rule; -import org.junit.rules.ExpectedException; import java.util.Map; public class NucleotideCounterTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void testEmptyDnaStringHasNoNucleotides() { NucleotideCounter nucleotideCounter = new NucleotideCounter(""); @@ -55,7 +51,8 @@ public void testDnaStringHasMultipleNucleotide() { @Ignore("Remove to run test") @Test public void testDnaStringHasInvalidNucleotides() { - expectedException.expect(IllegalArgumentException.class); - NucleotideCounter nucleotideCounter = new NucleotideCounter("AGXXACT"); + assertThrows( + IllegalArgumentException.class, + () -> new NucleotideCounter("AGXXACT")); } } diff --git a/exercises/ocr-numbers/build.gradle b/exercises/ocr-numbers/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/ocr-numbers/build.gradle +++ b/exercises/ocr-numbers/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/ocr-numbers/src/test/java/OpticalCharacterReaderTest.java b/exercises/ocr-numbers/src/test/java/OpticalCharacterReaderTest.java index 173ce0e30..c59790626 100644 --- a/exercises/ocr-numbers/src/test/java/OpticalCharacterReaderTest.java +++ b/exercises/ocr-numbers/src/test/java/OpticalCharacterReaderTest.java @@ -1,17 +1,14 @@ +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.util.Arrays; -import static org.junit.Assert.assertEquals; - public class OpticalCharacterReaderTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void testReaderRecognizesSingle0() { String parsedInput = new OpticalCharacterReader().parse(Arrays.asList( @@ -53,28 +50,38 @@ public void testReaderReturnsQuestionMarkForUnreadableButCorrectlySizedInput() { @Ignore("Remove to run test") @Test public void testReaderThrowsExceptionWhenNumberOfInputLinesIsNotAMultipleOf4() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Number of input rows must be a positive multiple of 4"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new OpticalCharacterReader() + .parse( + Arrays.asList( + " _ ", + "| |", + " "))); - new OpticalCharacterReader().parse(Arrays.asList( - " _ ", - "| |", - " " - )); + assertThat(expected) + .hasMessage( + "Number of input rows must be a positive multiple of 4"); } @Ignore("Remove to run test") @Test public void testReaderThrowsExceptionWhenNumberOfInputColumnsIsNotAMultipleOf3() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Number of input columns must be a positive multiple of 3"); - - new OpticalCharacterReader().parse(Arrays.asList( - " ", - " |", - " |", - " " - )); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new OpticalCharacterReader() + .parse( + Arrays.asList( + " ", + " |", + " |", + " "))); + + assertThat(expected) + .hasMessage( + "Number of input columns must be a positive multiple of 3"); } @Ignore("Remove to run test") diff --git a/exercises/octal/build.gradle b/exercises/octal/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/octal/build.gradle +++ b/exercises/octal/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/palindrome-products/build.gradle b/exercises/palindrome-products/build.gradle index 479746285..950f15a95 100644 --- a/exercises/palindrome-products/build.gradle +++ b/exercises/palindrome-products/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/palindrome-products/src/test/java/PalindromeCalculatorTest.java b/exercises/palindrome-products/src/test/java/PalindromeCalculatorTest.java index 8e18df8b6..5f094a3de 100644 --- a/exercises/palindrome-products/src/test/java/PalindromeCalculatorTest.java +++ b/exercises/palindrome-products/src/test/java/PalindromeCalculatorTest.java @@ -1,8 +1,12 @@ -import org.junit.Before; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + import org.junit.Ignore; import org.junit.Test; -import org.junit.Rule; -import org.junit.rules.ExpectedException; import java.util.Arrays; import java.util.Collections; @@ -10,21 +14,9 @@ import java.util.SortedMap; import java.util.stream.Collectors; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - public class PalindromeCalculatorTest { - private PalindromeCalculator palindromeCalculator; - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Before - public void setup() { - palindromeCalculator = new PalindromeCalculator(); - } + private PalindromeCalculator palindromeCalculator = new PalindromeCalculator(); @Test public void smallestPalindromeFromSingleDigitFactors() { @@ -173,22 +165,27 @@ public void emptyResultLargestNoPalindromeInRange() { @Ignore("Remove to run test") @Test public void errorSmallestMinIsMoreThanMax() { - - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("invalid input: min must be <= max"); - - SortedMap>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(10000, - 1); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> palindromeCalculator + .getPalindromeProductsWithFactors(10000, 1)); + + assertThat(expected) + .hasMessage("invalid input: min must be <= max"); } @Ignore("Remove to run test") @Test public void errorLargestMinIsMoreThanMax() { - - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("invalid input: min must be <= max"); - - SortedMap>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(2, 1); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> palindromeCalculator + .getPalindromeProductsWithFactors(2, 1)); + + assertThat(expected) + .hasMessage("invalid input: min must be <= max"); } diff --git a/exercises/pangram/build.gradle b/exercises/pangram/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/pangram/build.gradle +++ b/exercises/pangram/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/parallel-letter-frequency/build.gradle b/exercises/parallel-letter-frequency/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/parallel-letter-frequency/build.gradle +++ b/exercises/parallel-letter-frequency/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/pascals-triangle/build.gradle b/exercises/pascals-triangle/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/pascals-triangle/build.gradle +++ b/exercises/pascals-triangle/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/pascals-triangle/src/test/java/PascalsTriangleGeneratorTest.java b/exercises/pascals-triangle/src/test/java/PascalsTriangleGeneratorTest.java index 4093d8c5c..079c802e6 100644 --- a/exercises/pascals-triangle/src/test/java/PascalsTriangleGeneratorTest.java +++ b/exercises/pascals-triangle/src/test/java/PascalsTriangleGeneratorTest.java @@ -1,23 +1,12 @@ -import org.junit.Before; +import static org.junit.Assert.assertArrayEquals; + import org.junit.Test; import org.junit.Ignore; -import org.junit.Rule; -import org.junit.rules.ExpectedException; - - -import static org.junit.Assert.assertArrayEquals; public class PascalsTriangleGeneratorTest { - private PascalsTriangleGenerator pascalsTriangleGenerator; - - @Before - public void setUp() { - pascalsTriangleGenerator = new PascalsTriangleGenerator(); - } - - @Rule - public ExpectedException expectedException = ExpectedException.none(); + private PascalsTriangleGenerator pascalsTriangleGenerator = + new PascalsTriangleGenerator(); @Test public void testTriangleWithZeroRows() { diff --git a/exercises/perfect-numbers/build.gradle b/exercises/perfect-numbers/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/perfect-numbers/build.gradle +++ b/exercises/perfect-numbers/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/perfect-numbers/src/test/java/NaturalNumberTest.java b/exercises/perfect-numbers/src/test/java/NaturalNumberTest.java index 853489da6..393510d36 100644 --- a/exercises/perfect-numbers/src/test/java/NaturalNumberTest.java +++ b/exercises/perfect-numbers/src/test/java/NaturalNumberTest.java @@ -1,15 +1,12 @@ +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; - -import static org.junit.Assert.assertEquals; public class NaturalNumberTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void testSmallPerfectNumberIsClassifiedCorrectly() { assertEquals(Classification.PERFECT, new NaturalNumber(6).getClassification()); @@ -82,19 +79,25 @@ public void testThatOneIsCorrectlyClassifiedAsDeficient() { @Ignore("Remove to run test") @Test public void testThatNonNegativeIntegerIsRejected() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("You must supply a natural number (positive integer)"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new NaturalNumber(0)); - new NaturalNumber(0); + assertThat(expected) + .hasMessage("You must supply a natural number (positive integer)"); } @Ignore("Remove to run test") @Test public void testThatNegativeIntegerIsRejected() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("You must supply a natural number (positive integer)"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new NaturalNumber(-1)); - new NaturalNumber(-1); + assertThat(expected) + .hasMessage("You must supply a natural number (positive integer)"); } } diff --git a/exercises/phone-number/build.gradle b/exercises/phone-number/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/phone-number/build.gradle +++ b/exercises/phone-number/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/phone-number/src/test/java/PhoneNumberTest.java b/exercises/phone-number/src/test/java/PhoneNumberTest.java index faf37b19b..3160350f2 100644 --- a/exercises/phone-number/src/test/java/PhoneNumberTest.java +++ b/exercises/phone-number/src/test/java/PhoneNumberTest.java @@ -1,30 +1,11 @@ +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; - -import static org.junit.Assert.assertEquals; public class PhoneNumberTest { - private static String wrongLengthExceptionMessage = "incorrect number of digits"; - private static String moreThan11DigitsExceptionMessage = "more than 11 digits"; - private static String numberIs11DigitsButDoesNotStartWith1ExceptionMessage = - "11 digits must start with 1"; - private static String illegalCharacterExceptionMessage = - "letters not permitted"; - private static String illegalPunctuationExceptionMessage = - "punctuations not permitted"; - private static String areaCodeStartsWithZeroExceptionMessage = - "area code cannot start with zero"; - private static String areaCodeStartsWithOneExceptionMessage = - "area code cannot start with one"; - private static String exchangeCodeStartsWithZeroExceptionMessage = - "exchange code cannot start with zero"; - private static String exchangeCodeStartsWithOneExceptionMessage = - "exchange code cannot start with one"; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void cleansTheNumber() { @@ -61,17 +42,25 @@ public void cleansNumbersWithMultipleSpaces() { @Ignore("Remove to run test") @Test public void invalidWhen9Digits() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(wrongLengthExceptionMessage); - new PhoneNumber("123456789"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new PhoneNumber("123456789")); + + assertThat(expected) + .hasMessage("incorrect number of digits"); } @Ignore("Remove to run test") @Test public void invalidWhen11DigitsDoesNotStartWith1() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(numberIs11DigitsButDoesNotStartWith1ExceptionMessage); - new PhoneNumber("22234567890"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new PhoneNumber("22234567890")); + + assertThat(expected) + .hasMessage("11 digits must start with 1"); } @Ignore("Remove to run test") @@ -99,88 +88,132 @@ public void validWhen11DigitsAndStartingWith1EvenWithPunctuation() { @Ignore("Remove to run test") @Test public void invalidWhenMoreThan11Digits() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(moreThan11DigitsExceptionMessage); - new PhoneNumber("321234567890"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new PhoneNumber("321234567890")); + + assertThat(expected) + .hasMessage("more than 11 digits"); } @Ignore("Remove to run test") @Test public void invalidWithLetters() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(illegalCharacterExceptionMessage); - new PhoneNumber("123-abc-7890"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new PhoneNumber("123-abc-7890")); + + assertThat(expected) + .hasMessage("letters not permitted"); } @Ignore("Remove to run test") @Test public void invalidWithPunctuations() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(illegalPunctuationExceptionMessage); - new PhoneNumber("123-@:!-7890"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new PhoneNumber("123-@:!-7890")); + + assertThat(expected) + .hasMessage("punctuations not permitted"); } @Ignore("Remove to run test") @Test public void invalidIfAreaCodeStartsWith0() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(areaCodeStartsWithZeroExceptionMessage); - new PhoneNumber("(023) 456-7890"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new PhoneNumber("(023) 456-7890")); + + assertThat(expected) + .hasMessage("area code cannot start with zero"); } @Ignore("Remove to run test") @Test public void invalidIfAreaCodeStartsWith1() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(areaCodeStartsWithOneExceptionMessage); - new PhoneNumber("(123) 456-7890"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new PhoneNumber("(123) 456-7890")); + + assertThat(expected) + .hasMessage("area code cannot start with one"); } @Ignore("Remove to run test") @Test public void invalidIfExchangeCodeStartsWith0() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(exchangeCodeStartsWithZeroExceptionMessage); - new PhoneNumber("(223) 056-7890"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new PhoneNumber("(223) 056-7890")); + + assertThat(expected) + .hasMessage("exchange code cannot start with zero"); } @Ignore("Remove to run test") @Test public void invalidIfExchangeCodeStartsWith1() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(exchangeCodeStartsWithOneExceptionMessage); - new PhoneNumber("(223) 156-7890"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new PhoneNumber("(223) 156-7890")); + + assertThat(expected) + .hasMessage("exchange code cannot start with one"); } @Ignore("Remove to run test") @Test public void invalidIfAreaCodeStartsWith0OnValid11DigitNumber() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(areaCodeStartsWithZeroExceptionMessage); - new PhoneNumber("1 (023) 456-7890"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new PhoneNumber("1 (023) 456-7890")); + + assertThat(expected) + .hasMessage("area code cannot start with zero"); } @Ignore("Remove to run test") @Test public void invalidIfAreaCodeStartsWith1OnValid11DigitNumber() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(areaCodeStartsWithOneExceptionMessage); - new PhoneNumber("1 (123) 456-7890"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new PhoneNumber("1 (123) 456-7890")); + + assertThat(expected) + .hasMessage("area code cannot start with one"); } @Ignore("Remove to run test") @Test public void invalidIfExchangeCodeStartsWith0OnValid11DigitNumber() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(exchangeCodeStartsWithZeroExceptionMessage); - new PhoneNumber("1 (223) 056-7890"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new PhoneNumber("1 (223) 056-7890")); + + assertThat(expected) + .hasMessage("exchange code cannot start with zero"); } @Ignore("Remove to run test") @Test public void invalidIfExchangeCodeStartsWith1OnValid11DigitNumber() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(exchangeCodeStartsWithOneExceptionMessage); - new PhoneNumber("1 (223) 156-7890"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new PhoneNumber("1 (223) 156-7890")); + + assertThat(expected) + .hasMessage("exchange code cannot start with one"); } } diff --git a/exercises/pig-latin/build.gradle b/exercises/pig-latin/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/pig-latin/build.gradle +++ b/exercises/pig-latin/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/poker/build.gradle b/exercises/poker/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/poker/build.gradle +++ b/exercises/poker/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/prime-factors/build.gradle b/exercises/prime-factors/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/prime-factors/build.gradle +++ b/exercises/prime-factors/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/protein-translation/build.gradle b/exercises/protein-translation/build.gradle index 479746285..950f15a95 100644 --- a/exercises/protein-translation/build.gradle +++ b/exercises/protein-translation/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/protein-translation/src/main/java/ProteinTranslator.java b/exercises/protein-translation/src/main/java/ProteinTranslator.java index 3818bbc0f..138282abe 100644 --- a/exercises/protein-translation/src/main/java/ProteinTranslator.java +++ b/exercises/protein-translation/src/main/java/ProteinTranslator.java @@ -5,4 +5,4 @@ class ProteinTranslator { List translate(String rnaSequence) { throw new UnsupportedOperationException("Delete this statement and write your own implementation."); } -} \ No newline at end of file +} diff --git a/exercises/proverb/build.gradle b/exercises/proverb/build.gradle index 952e38529..96c8acd4c 100644 --- a/exercises/proverb/build.gradle +++ b/exercises/proverb/build.gradle @@ -7,7 +7,7 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" testImplementation "org.assertj:assertj-core:3.15.0" } diff --git a/exercises/pythagorean-triplet/build.gradle b/exercises/pythagorean-triplet/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/pythagorean-triplet/build.gradle +++ b/exercises/pythagorean-triplet/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/queen-attack/build.gradle b/exercises/queen-attack/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/queen-attack/build.gradle +++ b/exercises/queen-attack/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/queen-attack/src/test/java/QueenAttackCalculatorTest.java b/exercises/queen-attack/src/test/java/QueenAttackCalculatorTest.java index c8c0a7d9f..eee5a5318 100644 --- a/exercises/queen-attack/src/test/java/QueenAttackCalculatorTest.java +++ b/exercises/queen-attack/src/test/java/QueenAttackCalculatorTest.java @@ -1,16 +1,14 @@ +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; public class QueenAttackCalculatorTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void testCreateQueenWithAValidPosition() { new Queen(2, 2); @@ -19,37 +17,49 @@ public void testCreateQueenWithAValidPosition() { @Ignore("Remove to run test") @Test public void testCreateQueenMustHavePositiveRow() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Queen position must have positive row."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new Queen(-2, 2)); - new Queen(-2, 2); + assertThat(expected) + .hasMessage("Queen position must have positive row."); } @Ignore("Remove to run test") @Test public void testCreateQueenMustHaveRowOnBoard() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Queen position must have row <= 7."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new Queen(8, 4)); - new Queen(8, 4); + assertThat(expected) + .hasMessage("Queen position must have row <= 7."); } @Ignore("Remove to run test") @Test public void testCreateQueenMustHavePositiveColumn() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Queen position must have positive column."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new Queen(2, -2)); - new Queen(2, -2); + assertThat(expected) + .hasMessage("Queen position must have positive column."); } @Ignore("Remove to run test") @Test public void testCreateQueenMustHaveColumnOnBoard() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Queen position must have column <= 7."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new Queen(4, 8)); - new Queen(4, 8); + assertThat(expected) + .hasMessage("Queen position must have column <= 7."); } @Ignore("Remove to run test") @@ -118,19 +128,25 @@ public void testQueensCanAttackOnFourthDiagonal() { @Ignore("Remove to run test") @Test public void testNullPositionsNotAllowed() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("You must supply valid positions for both Queens."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new QueenAttackCalculator(null, new Queen(0, 7))); - new QueenAttackCalculator(null, new Queen(0, 7)); + assertThat(expected) + .hasMessage("You must supply valid positions for both Queens."); } @Ignore("Remove to run test") @Test public void testQueensMustNotOccupyTheSameSquare() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Queens cannot occupy the same position."); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new QueenAttackCalculator(new Queen(2, 2), new Queen(2, 2))); - new QueenAttackCalculator(new Queen(2, 2), new Queen(2, 2)); + assertThat(expected) + .hasMessage("Queens cannot occupy the same position."); } } diff --git a/exercises/rail-fence-cipher/build.gradle b/exercises/rail-fence-cipher/build.gradle index b14be7691..05ba8edad 100644 --- a/exercises/rail-fence-cipher/build.gradle +++ b/exercises/rail-fence-cipher/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { testLogging { diff --git a/exercises/raindrops/build.gradle b/exercises/raindrops/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/raindrops/build.gradle +++ b/exercises/raindrops/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/rational-numbers/build.gradle b/exercises/rational-numbers/build.gradle index b14be7691..05ba8edad 100644 --- a/exercises/rational-numbers/build.gradle +++ b/exercises/rational-numbers/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { testLogging { diff --git a/exercises/rectangles/build.gradle b/exercises/rectangles/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/rectangles/build.gradle +++ b/exercises/rectangles/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/resistor-color-duo/build.gradle b/exercises/resistor-color-duo/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/resistor-color-duo/build.gradle +++ b/exercises/resistor-color-duo/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/resistor-color/build.gradle b/exercises/resistor-color/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/resistor-color/build.gradle +++ b/exercises/resistor-color/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/reverse-string/build.gradle b/exercises/reverse-string/build.gradle index 479746285..950f15a95 100644 --- a/exercises/reverse-string/build.gradle +++ b/exercises/reverse-string/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/reverse-string/src/main/java/ReverseString.java b/exercises/reverse-string/src/main/java/ReverseString.java index 42ac43837..68f6f0b29 100644 --- a/exercises/reverse-string/src/main/java/ReverseString.java +++ b/exercises/reverse-string/src/main/java/ReverseString.java @@ -4,4 +4,4 @@ String reverse(String inputString) { throw new UnsupportedOperationException("Delete this statement and write your own implementation."); } -} \ No newline at end of file +} diff --git a/exercises/rna-transcription/build.gradle b/exercises/rna-transcription/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/rna-transcription/build.gradle +++ b/exercises/rna-transcription/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/robot-name/build.gradle b/exercises/robot-name/build.gradle index 952e38529..96c8acd4c 100644 --- a/exercises/robot-name/build.gradle +++ b/exercises/robot-name/build.gradle @@ -7,7 +7,7 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" testImplementation "org.assertj:assertj-core:3.15.0" } diff --git a/exercises/robot-simulator/build.gradle b/exercises/robot-simulator/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/robot-simulator/build.gradle +++ b/exercises/robot-simulator/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/robot-simulator/src/main/java/GridPosition.java b/exercises/robot-simulator/src/main/java/GridPosition.java index 2ff3a3e3c..20995a0af 100644 --- a/exercises/robot-simulator/src/main/java/GridPosition.java +++ b/exercises/robot-simulator/src/main/java/GridPosition.java @@ -24,7 +24,7 @@ public boolean equals(Object obj) { return true; } else if (obj == null || getClass() != obj.getClass()) { return false; - } else if (x != ((GridPosition) obj).x || y != ((GridPosition)obj).y) { + } else if (x != ((GridPosition) obj).x || y != ((GridPosition) obj).y) { return false; } else { return true; diff --git a/exercises/roman-numerals/build.gradle b/exercises/roman-numerals/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/roman-numerals/build.gradle +++ b/exercises/roman-numerals/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/rotational-cipher/build.gradle b/exercises/rotational-cipher/build.gradle index b14be7691..05ba8edad 100644 --- a/exercises/rotational-cipher/build.gradle +++ b/exercises/rotational-cipher/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { testLogging { diff --git a/exercises/run-length-encoding/build.gradle b/exercises/run-length-encoding/build.gradle index 479746285..950f15a95 100644 --- a/exercises/run-length-encoding/build.gradle +++ b/exercises/run-length-encoding/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/saddle-points/build.gradle b/exercises/saddle-points/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/saddle-points/build.gradle +++ b/exercises/saddle-points/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/saddle-points/src/main/java/MatrixCoordinate.java b/exercises/saddle-points/src/main/java/MatrixCoordinate.java index d4fb45458..0548c58a5 100644 --- a/exercises/saddle-points/src/main/java/MatrixCoordinate.java +++ b/exercises/saddle-points/src/main/java/MatrixCoordinate.java @@ -16,8 +16,12 @@ public String toString() { @Override public boolean equals(final Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } final MatrixCoordinate that = (MatrixCoordinate) o; diff --git a/exercises/scrabble-score/build.gradle b/exercises/scrabble-score/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/scrabble-score/build.gradle +++ b/exercises/scrabble-score/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/secret-handshake/build.gradle b/exercises/secret-handshake/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/secret-handshake/build.gradle +++ b/exercises/secret-handshake/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/series/build.gradle b/exercises/series/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/series/build.gradle +++ b/exercises/series/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/series/src/test/java/SeriesTest.java b/exercises/series/src/test/java/SeriesTest.java index 006eeec21..c52efcd7a 100644 --- a/exercises/series/src/test/java/SeriesTest.java +++ b/exercises/series/src/test/java/SeriesTest.java @@ -1,19 +1,16 @@ +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import java.util.Arrays; import java.util.Collections; import java.util.List; import org.junit.Test; import org.junit.Ignore; -import org.junit.Rule; -import org.junit.rules.ExpectedException; - -import static org.junit.Assert.assertEquals; public class SeriesTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void slicesOfOneFromOne() { Series series = new Series("1"); @@ -84,38 +81,57 @@ public void slicesOfLongSeries() { @Ignore("Remove to run test") @Test public void sliceLengthIsToolarge() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Slice size is too big."); - Series series = new Series("12345"); - series.slices(6); + + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> series.slices(6)); + + assertThat(expected) + .hasMessage("Slice size is too big."); } @Ignore("Remove to run test") @Test public void sliceLengthZero() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Slice size is too small."); Series series = new Series("12345"); - series.slices(0); + + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> series.slices(0)); + + assertThat(expected) + .hasMessage("Slice size is too small."); } @Ignore("Remove to run test") @Test public void sliceLengthNegative() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Slice size is too small."); Series series = new Series("123"); - series.slices(-1); + + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> series.slices(-1)); + + assertThat(expected) + .hasMessage("Slice size is too small."); } @Ignore("Remove to run test") @Test public void emptySeries() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Slice size is too big."); Series series = new Series(""); - series.slices(1); + + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> series.slices(1)); + + assertThat(expected) + .hasMessage("Slice size is too big."); } } diff --git a/exercises/sieve/build.gradle b/exercises/sieve/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/sieve/build.gradle +++ b/exercises/sieve/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/simple-cipher/build.gradle b/exercises/simple-cipher/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/simple-cipher/build.gradle +++ b/exercises/simple-cipher/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/simple-cipher/src/test/java/SimpleCipherStepTwoSubsitutionTest.java b/exercises/simple-cipher/src/test/java/SimpleCipherStepTwoSubsitutionTest.java index 1025b4364..d9a32c057 100644 --- a/exercises/simple-cipher/src/test/java/SimpleCipherStepTwoSubsitutionTest.java +++ b/exercises/simple-cipher/src/test/java/SimpleCipherStepTwoSubsitutionTest.java @@ -1,23 +1,11 @@ +import static org.junit.Assert.assertEquals; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; - -import static org.junit.Assert.*; - -import org.junit.Before; public class SimpleCipherStepTwoSubsitutionTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private Cipher cipherWithDefaultKey; - private static final String key = "abcdefghij"; - - @Before - public void setup() { - cipherWithDefaultKey = new Cipher(key); - } + private Cipher cipherWithDefaultKey = new Cipher("abcdefghij"); @Ignore("Remove to run test") @Test diff --git a/exercises/simple-linked-list/build.gradle b/exercises/simple-linked-list/build.gradle index 952e38529..96c8acd4c 100644 --- a/exercises/simple-linked-list/build.gradle +++ b/exercises/simple-linked-list/build.gradle @@ -7,7 +7,7 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" testImplementation "org.assertj:assertj-core:3.15.0" } diff --git a/exercises/simple-linked-list/src/test/java/SimpleLinkedListTest.java b/exercises/simple-linked-list/src/test/java/SimpleLinkedListTest.java index 600c7f632..847e169c5 100644 --- a/exercises/simple-linked-list/src/test/java/SimpleLinkedListTest.java +++ b/exercises/simple-linked-list/src/test/java/SimpleLinkedListTest.java @@ -1,18 +1,14 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertThrows; import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.util.NoSuchElementException; public class SimpleLinkedListTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void aNewListIsEmpty() { SimpleLinkedList list = new SimpleLinkedList<>(); @@ -30,9 +26,11 @@ public void canCreateFromArray() { @Ignore("Remove to run test") @Test public void popOnEmptyListWillThrow() { - expectedException.expect(NoSuchElementException.class); SimpleLinkedList list = new SimpleLinkedList(); - list.pop(); + + assertThrows( + NoSuchElementException.class, + list::pop); } @Ignore("Remove to run test") diff --git a/exercises/space-age/build.gradle b/exercises/space-age/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/space-age/build.gradle +++ b/exercises/space-age/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/spiral-matrix/build.gradle b/exercises/spiral-matrix/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/spiral-matrix/build.gradle +++ b/exercises/spiral-matrix/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/strain/build.gradle b/exercises/strain/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/strain/build.gradle +++ b/exercises/strain/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/sublist/build.gradle b/exercises/sublist/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/sublist/build.gradle +++ b/exercises/sublist/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/sum-of-multiples/build.gradle b/exercises/sum-of-multiples/build.gradle index 82d705dfd..96c8acd4c 100755 --- a/exercises/sum-of-multiples/build.gradle +++ b/exercises/sum-of-multiples/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/tournament/build.gradle b/exercises/tournament/build.gradle index 479746285..950f15a95 100644 --- a/exercises/tournament/build.gradle +++ b/exercises/tournament/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/transpose/build.gradle b/exercises/transpose/build.gradle index 6c26f4b12..2189900ea 100755 --- a/exercises/transpose/build.gradle +++ b/exercises/transpose/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { testLogging { diff --git a/exercises/tree-building/build.gradle b/exercises/tree-building/build.gradle index 479746285..950f15a95 100644 --- a/exercises/tree-building/build.gradle +++ b/exercises/tree-building/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/tree-building/src/test/java/BuildTreeTest.java b/exercises/tree-building/src/test/java/BuildTreeTest.java index a20895944..9e1244437 100644 --- a/exercises/tree-building/src/test/java/BuildTreeTest.java +++ b/exercises/tree-building/src/test/java/BuildTreeTest.java @@ -1,17 +1,15 @@ +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.util.ArrayList; -import static org.junit.Assert.*; - public class BuildTreeTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void testEmptyList() throws InvalidRecordsException { ArrayList records = new ArrayList<>(); @@ -153,38 +151,41 @@ public void testUnbalancedTree() throws InvalidRecordsException { @Ignore("Remove to run test") @Test - public void testRootNodeHasParent() throws InvalidRecordsException { - expectedException.expect(InvalidRecordsException.class); - expectedException.expectMessage("Invalid Records"); - + public void testRootNodeHasParent() { ArrayList records = new ArrayList<>(); records.add(new Record(0, 1)); records.add(new Record(1, 0)); BuildTree test = new BuildTree(); - test.buildTree(records); + + InvalidRecordsException expected = + assertThrows( + InvalidRecordsException.class, + () -> test.buildTree(records)); + + assertThat(expected).hasMessage("Invalid Records"); } @Ignore("Remove to run test") @Test - public void testNoRootNode() throws InvalidRecordsException { - expectedException.expect(InvalidRecordsException.class); - expectedException.expectMessage("Invalid Records"); - + public void testNoRootNode() { ArrayList records = new ArrayList<>(); records.add(new Record(1, 0)); records.add(new Record(2, 0)); BuildTree test = new BuildTree(); - test.buildTree(records); + + InvalidRecordsException expected = + assertThrows( + InvalidRecordsException.class, + () -> test.buildTree(records)); + + assertThat(expected).hasMessage("Invalid Records"); } @Ignore("Remove to run test") @Test - public void testNonContinuousRecords() throws InvalidRecordsException { - expectedException.expect(InvalidRecordsException.class); - expectedException.expectMessage("Invalid Records"); - + public void testNonContinuousRecords() { ArrayList records = new ArrayList<>(); records.add(new Record(2, 0)); records.add(new Record(4, 2)); @@ -192,15 +193,18 @@ public void testNonContinuousRecords() throws InvalidRecordsException { records.add(new Record(0, 0)); BuildTree test = new BuildTree(); - test.buildTree(records); + + InvalidRecordsException expected = + assertThrows( + InvalidRecordsException.class, + () -> test.buildTree(records)); + + assertThat(expected).hasMessage("Invalid Records"); } @Ignore("Remove to run test") @Test - public void testCycleIndirectly() throws InvalidRecordsException { - expectedException.expect(InvalidRecordsException.class); - expectedException.expectMessage("Invalid Records"); - + public void testCycleIndirectly() { ArrayList records = new ArrayList<>(); records.add(new Record(5, 2)); records.add(new Record(3, 2)); @@ -211,7 +215,13 @@ public void testCycleIndirectly() throws InvalidRecordsException { records.add(new Record(6, 3)); BuildTree test = new BuildTree(); - test.buildTree(records); + + InvalidRecordsException expected = + assertThrows( + InvalidRecordsException.class, + () -> test.buildTree(records)); + + assertThat(expected).hasMessage("Invalid Records"); } private void assertNodeIsLeaf(TreeNode node) { diff --git a/exercises/triangle/build.gradle b/exercises/triangle/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/triangle/build.gradle +++ b/exercises/triangle/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/triangle/src/test/java/TriangleTest.java b/exercises/triangle/src/test/java/TriangleTest.java index 03004be56..78f053de0 100644 --- a/exercises/triangle/src/test/java/TriangleTest.java +++ b/exercises/triangle/src/test/java/TriangleTest.java @@ -1,16 +1,12 @@ +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + import org.junit.Test; import org.junit.Ignore; -import org.junit.Rule; -import org.junit.rules.ExpectedException; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; public class TriangleTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void equilateralTrianglesHaveEqualSides() throws TriangleException { Triangle triangle = new Triangle(2, 2, 2); @@ -36,9 +32,10 @@ public void trianglesWithNoEqualSidesAreNotEquilateral() throws TriangleExceptio @Ignore("Remove to run test") @Test - public void trianglesWithNoSizeAreIllegal() throws TriangleException { - expectedException.expect(TriangleException.class); - new Triangle(0, 0, 0); + public void trianglesWithNoSizeAreIllegal() { + assertThrows( + TriangleException.class, + () -> new Triangle(0, 0, 0)); } @Ignore("Remove to run test") @@ -91,23 +88,26 @@ public void noSidesAreEqualCantBeIsoceles() throws TriangleException { @Ignore("Remove to run test") @Test - public void firstTriangleInequalityViolation() throws TriangleException { - expectedException.expect(TriangleException.class); - new Triangle(1, 1, 3); + public void firstTriangleInequalityViolation() { + assertThrows( + TriangleException.class, + () -> new Triangle(1, 1, 3)); } @Ignore("Remove to run test") @Test - public void secondTriangleInequalityViolation() throws TriangleException { - expectedException.expect(TriangleException.class); - new Triangle(1, 3, 1); + public void secondTriangleInequalityViolation() { + assertThrows( + TriangleException.class, + () -> new Triangle(1, 3, 1)); } @Ignore("Remove to run test") @Test - public void thirdTriangleInequalityViolation() throws TriangleException { - expectedException.expect(TriangleException.class); - new Triangle(3, 1, 1); + public void thirdTriangleInequalityViolation() { + assertThrows( + TriangleException.class, + () -> new Triangle(3, 1, 1)); } @Ignore("Remove to run test") @@ -144,10 +144,10 @@ public void twoSidesEqualAreNotScalene() throws TriangleException { @Ignore("Remove to run test") @Test - public void mayNotViolateTriangleInequality() - throws TriangleException { - expectedException.expect(TriangleException.class); - new Triangle(7, 3, 2); + public void mayNotViolateTriangleInequality() { + assertThrows( + TriangleException.class, + () -> new Triangle(7, 3, 2)); } @Ignore("Remove to run test") diff --git a/exercises/trinary/build.gradle b/exercises/trinary/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/trinary/build.gradle +++ b/exercises/trinary/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/twelve-days/build.gradle b/exercises/twelve-days/build.gradle index 479746285..950f15a95 100644 --- a/exercises/twelve-days/build.gradle +++ b/exercises/twelve-days/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/two-bucket/build.gradle b/exercises/two-bucket/build.gradle index 479746285..950f15a95 100644 --- a/exercises/two-bucket/build.gradle +++ b/exercises/two-bucket/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/two-fer/build.gradle b/exercises/two-fer/build.gradle index 6c26f4b12..2189900ea 100644 --- a/exercises/two-fer/build.gradle +++ b/exercises/two-fer/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { testLogging { diff --git a/exercises/variable-length-quantity/build.gradle b/exercises/variable-length-quantity/build.gradle index 479746285..950f15a95 100644 --- a/exercises/variable-length-quantity/build.gradle +++ b/exercises/variable-length-quantity/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/variable-length-quantity/src/test/java/VariableLengthQuantityTest.java b/exercises/variable-length-quantity/src/test/java/VariableLengthQuantityTest.java index 7506d0c50..0416bfc19 100644 --- a/exercises/variable-length-quantity/src/test/java/VariableLengthQuantityTest.java +++ b/exercises/variable-length-quantity/src/test/java/VariableLengthQuantityTest.java @@ -1,24 +1,17 @@ -import org.junit.Before; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; - -import static org.junit.Assert.assertEquals; import java.util.Arrays; import java.util.List; public class VariableLengthQuantityTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private VariableLengthQuantity variableLengthQuantity; - - @Before - public void setup() { - variableLengthQuantity = new VariableLengthQuantity(); - } + private VariableLengthQuantity variableLengthQuantity = + new VariableLengthQuantity(); @Test public void testZero() { @@ -234,20 +227,28 @@ public void testDecodeMaximum32BitInteger() { @Test public void testCannotDecodeIncompleteSequence() { List bytes = Arrays.asList(0xffL); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Invalid variable-length quantity encoding"); - variableLengthQuantity.decode(bytes); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> variableLengthQuantity.decode(bytes)); + + assertThat(expected) + .hasMessage("Invalid variable-length quantity encoding"); } @Ignore("Remove to run test") @Test public void testCannotDecodeIncompleteSequenceEvenIfValueIsZero() { List bytes = Arrays.asList(0x80L); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Invalid variable-length quantity encoding"); - variableLengthQuantity.decode(bytes); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> variableLengthQuantity.decode(bytes)); + + assertThat(expected) + .hasMessage("Invalid variable-length quantity encoding"); } @Ignore("Remove to run test") diff --git a/exercises/word-count/build.gradle b/exercises/word-count/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/word-count/build.gradle +++ b/exercises/word-count/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/word-search/build.gradle b/exercises/word-search/build.gradle index 479746285..950f15a95 100644 --- a/exercises/word-search/build.gradle +++ b/exercises/word-search/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/word-search/src/main/java/Pair.java b/exercises/word-search/src/main/java/Pair.java index 7474f4e65..bd3289f60 100644 --- a/exercises/word-search/src/main/java/Pair.java +++ b/exercises/word-search/src/main/java/Pair.java @@ -19,8 +19,12 @@ int getY() { @Override public boolean equals(final Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } Pair pair = (Pair) o; diff --git a/exercises/word-search/src/main/java/WordLocation.java b/exercises/word-search/src/main/java/WordLocation.java index 68ad30bba..098bbd735 100644 --- a/exercises/word-search/src/main/java/WordLocation.java +++ b/exercises/word-search/src/main/java/WordLocation.java @@ -11,8 +11,12 @@ class WordLocation { @Override public boolean equals(final Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } WordLocation that = (WordLocation) o; diff --git a/exercises/wordy/build.gradle b/exercises/wordy/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/wordy/build.gradle +++ b/exercises/wordy/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/wordy/src/test/java/WordProblemSolverTest.java b/exercises/wordy/src/test/java/WordProblemSolverTest.java index ac54227b9..73bfe56f2 100644 --- a/exercises/wordy/src/test/java/WordProblemSolverTest.java +++ b/exercises/wordy/src/test/java/WordProblemSolverTest.java @@ -1,22 +1,13 @@ +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.Before; -import org.junit.rules.ExpectedException; - -import static org.junit.Assert.assertEquals; public class WordProblemSolverTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - WordProblemSolver solver; - - @Before - public void setup() { - solver = new WordProblemSolver(); - } + WordProblemSolver solver = new WordProblemSolver(); @Test public void testJustANumber() { @@ -110,73 +101,97 @@ public void testMultipleDivisions() { @Ignore("Remove to run test") @Test public void testUnknownOperation() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("I'm sorry, I don't understand the question!"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> solver.solve("What is 52 cubed?")); - solver.solve("What is 52 cubed?"); + assertThat(expected) + .hasMessage("I'm sorry, I don't understand the question!"); } @Ignore("Remove to run test") @Test public void testNonMathQuestion() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("I'm sorry, I don't understand the question!"); - // See https://en.wikipedia.org/wiki/President_of_the_United_States if you really need to know! - solver.solve("Who is the President of the United States?"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> solver.solve("Who is the President of the United States?")); + + assertThat(expected) + .hasMessage("I'm sorry, I don't understand the question!"); } @Ignore("Remove to run test") @Test public void testMissingAnOperand() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("I'm sorry, I don't understand the question!"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> solver.solve("What is 1 plus?")); - solver.solve("What is 1 plus?"); + assertThat(expected) + .hasMessage("I'm sorry, I don't understand the question!"); } @Ignore("Remove to run test") @Test public void testNoOperandsOrOperators() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("I'm sorry, I don't understand the question!"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> solver.solve("What is?")); - solver.solve("What is?"); + assertThat(expected) + .hasMessage("I'm sorry, I don't understand the question!"); } @Ignore("Remove to run test") @Test public void testTwoOperationsInARow() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("I'm sorry, I don't understand the question!"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> solver.solve("What is 1 plus plus 2?")); - solver.solve("What is 1 plus plus 2?"); + assertThat(expected) + .hasMessage("I'm sorry, I don't understand the question!"); } @Ignore("Remove to run test") @Test public void testTwoNumbersAfterOperation() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("I'm sorry, I don't understand the question!"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> solver.solve("What is 1 plus 2 1?")); - solver.solve("What is 1 plus 2 1?"); + assertThat(expected) + .hasMessage("I'm sorry, I don't understand the question!"); } @Ignore("Remove to run test") @Test public void testPostfixNotation() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("I'm sorry, I don't understand the question!"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> solver.solve("What is 1 2 plus?")); - solver.solve("What is 1 2 plus?"); + assertThat(expected) + .hasMessage("I'm sorry, I don't understand the question!"); } @Ignore("Remove to run test") @Test public void testPrefixNotation() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("I'm sorry, I don't understand the question!"); + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> solver.solve("What is plus 1 2?")); - solver.solve("What is plus 1 2?"); + assertThat(expected) + .hasMessage("I'm sorry, I don't understand the question!"); } } diff --git a/exercises/yacht/build.gradle b/exercises/yacht/build.gradle index 479746285..950f15a95 100644 --- a/exercises/yacht/build.gradle +++ b/exercises/yacht/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/zebra-puzzle/build.gradle b/exercises/zebra-puzzle/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/zebra-puzzle/build.gradle +++ b/exercises/zebra-puzzle/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test { diff --git a/exercises/zipper/build.gradle b/exercises/zipper/build.gradle index 82d705dfd..96c8acd4c 100644 --- a/exercises/zipper/build.gradle +++ b/exercises/zipper/build.gradle @@ -7,7 +7,8 @@ repositories { } dependencies { - testCompile "junit:junit:4.12" + testCompile "junit:junit:4.13" + testImplementation "org.assertj:assertj-core:3.15.0" } test {