Skip to content

Migrate to assertThrows #1826

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
f09be4c
Update JUnit to 4.13 and add default dependency on AssertJ.
jmrunkle Apr 23, 2020
ba6460a
Fix style issueswith tests and initial implementations.
jmrunkle Apr 23, 2020
fe8b973
Update affine-cipher to use assertThrows.
jmrunkle Apr 23, 2020
11d0599
Migrate all-your-base to assertThrows.
jmrunkle Apr 23, 2020
4d77b1f
Migrate alphametics to assertThrows.
jmrunkle Apr 23, 2020
27deb4e
Migrate bank-account to assertThrows.
jmrunkle Apr 23, 2020
e57920b
Migrate binary-search to assertThrows.
jmrunkle Apr 23, 2020
f1d923a
Migrate bowling to assertThrows.
jmrunkle Apr 23, 2020
a95be6c
Migrate change to assertThrows.
jmrunkle Apr 23, 2020
8d39e33
Migrate circular-buffer to assertThrows.
jmrunkle Apr 23, 2020
92856d2
Migrate collatz-conjecture to assertThrows.
jmrunkle Apr 23, 2020
a35524b
Migrate dominoes to assertThrows.
jmrunkle Apr 23, 2020
a0433fd
Migrate error-handling to assertThrows.
jmrunkle Apr 23, 2020
521f05c
Migrate forth to assertThrows.
jmrunkle Apr 23, 2020
94a0d4e
Migrate go-counting to assertThrows.
jmrunkle Apr 23, 2020
290515e
Migrate grains to assertThrows.
jmrunkle Apr 23, 2020
bd0d516
Migrate hamming to assertThrows.
jmrunkle Apr 23, 2020
b7ea2cb
Migrate largest-series-product to assertThrows.
jmrunkle Apr 23, 2020
7df94a6
Migrate nth-prime to assertThrows.
jmrunkle Apr 23, 2020
fa8587d
Migrate nucleotide-count to assertThrows.
jmrunkle Apr 23, 2020
dd607e0
Migrate ocr-numbers to assertThrows.
jmrunkle Apr 23, 2020
df42fea
Migrate palindrome-products to assertThrows.
jmrunkle Apr 23, 2020
2147c55
Remove unused ExpectedException from pascals-triangle.
jmrunkle Apr 23, 2020
5874784
Migrate perfect-numbers to assertThrows.
jmrunkle Apr 23, 2020
0698e3e
Migrate phone-number to assertThrows.
jmrunkle Apr 23, 2020
e3609c5
Migrate queen-attack to assertThrows.
jmrunkle Apr 23, 2020
a185c28
Migrate series to assertThrows.
jmrunkle Apr 23, 2020
d08b47d
Remove unused ExpectedException from simple-cipher.
jmrunkle Apr 23, 2020
69f95dd
Migrate simple-linked-list to assertThrows.
jmrunkle Apr 23, 2020
a48198c
Migrate tree-building to assertThrows.
jmrunkle Apr 23, 2020
e0c8580
Migrate triangle to assertThrows.
jmrunkle Apr 23, 2020
fa8a7a7
Migrate variable-length-quantity to assertThrows.
jmrunkle Apr 23, 2020
a5a7aae
Migrate wordy to assertThrows.
jmrunkle Apr 23, 2020
0361a4e
Update POLICIES to use assertThrows rather than ExpectedException.
jmrunkle Apr 23, 2020
2b7dfcb
Fix style violations.
jmrunkle Apr 23, 2020
18d3eb5
Fix bowling tests and reference solution.
jmrunkle Apr 23, 2020
a4f7a77
Restore dropped import for go-counting.
jmrunkle Apr 23, 2020
e250a0f
Fix exception for tree-building.
jmrunkle Apr 23, 2020
47157ab
Fix style violation on bowling reference solution.
jmrunkle Apr 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions POLICIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down Expand Up @@ -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)]

Expand Down
3 changes: 2 additions & 1 deletion _template/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion exercises/accumulate/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion exercises/acronym/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion exercises/affine-cipher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
39 changes: 18 additions & 21 deletions exercises/affine-cipher/src/test/java/AffineCipherTest.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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.");
}

}
3 changes: 2 additions & 1 deletion exercises/all-your-base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
79 changes: 47 additions & 32 deletions exercises/all-your-base/src/test/java/BaseConverterTest.java
Original file line number Diff line number Diff line change
@@ -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});
Expand Down Expand Up @@ -218,79 +215,97 @@ 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")
@Test
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")
@Test
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.");
}

}
3 changes: 2 additions & 1 deletion exercises/allergies/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion exercises/alphametics/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 9 additions & 11 deletions exercises/alphametics/src/test/java/AlphameticsTest.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion exercises/anagram/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repositories {
}

dependencies {
testCompile "junit:junit:4.12"
testCompile "junit:junit:4.13"
testImplementation "org.assertj:assertj-core:3.15.0"
}

Expand Down
1 change: 0 additions & 1 deletion exercises/anagram/src/test/java/AnagramTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class AnagramTest {

Expand Down
3 changes: 2 additions & 1 deletion exercises/armstrong-numbers/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.");

}

}
Loading