From f703a80d12d6610a6f238dbca96de6540e046b83 Mon Sep 17 00:00:00 2001 From: Pavan Baloju Date: Thu, 5 Oct 2023 15:49:23 +0530 Subject: [PATCH 1/4] Update tests to use assertj (#2147) --- .../src/test/java/CollatzCalculatorTest.java | 14 ++-- .../src/test/java/CryptoSquareTest.java | 18 ++--- .../darts/src/test/java/DartsTest.java | 28 +++---- .../DifferenceOfSquaresCalculatorTest.java | 20 ++--- .../src/test/java/DiffieHellmanTest.java | 21 +++--- .../src/test/java/DnDCharacterTest.java | 71 +++++++++--------- .../dominoes/src/test/java/DominoesTest.java | 18 ++--- .../src/test/java/ProteinTranslatorTest.java | 74 ++++++------------- .../src/test/java/RotationalCipherTest.java | 24 +++--- 9 files changed, 130 insertions(+), 158 deletions(-) diff --git a/exercises/practice/collatz-conjecture/src/test/java/CollatzCalculatorTest.java b/exercises/practice/collatz-conjecture/src/test/java/CollatzCalculatorTest.java index 7a53bf272..de03da5fa 100644 --- a/exercises/practice/collatz-conjecture/src/test/java/CollatzCalculatorTest.java +++ b/exercises/practice/collatz-conjecture/src/test/java/CollatzCalculatorTest.java @@ -1,34 +1,34 @@ -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.junit.Assert.assertEquals; - import org.junit.Ignore; import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + public class CollatzCalculatorTest { private CollatzCalculator collatzCalculator = new CollatzCalculator(); @Test public void testZeroStepsRequiredWhenStartingFrom1() { - assertEquals(0, collatzCalculator.computeStepCount(1)); + assertThat(collatzCalculator.computeStepCount(1)).isEqualTo(0); } @Ignore("Remove to run test") @Test public void testCorrectNumberOfStepsWhenAllStepsAreDivisions() { - assertEquals(4, collatzCalculator.computeStepCount(16)); + assertThat(collatzCalculator.computeStepCount(16)).isEqualTo(4); } @Ignore("Remove to run test") @Test public void testCorrectNumberOfStepsWhenBothStepTypesAreNeeded() { - assertEquals(9, collatzCalculator.computeStepCount(12)); + assertThat(collatzCalculator.computeStepCount(12)).isEqualTo(9); } @Ignore("Remove to run test") @Test public void testAVeryLargeInput() { - assertEquals(152, collatzCalculator.computeStepCount(1000000)); + assertThat(collatzCalculator.computeStepCount(1000000)).isEqualTo(152); } @Ignore("Remove to run test") diff --git a/exercises/practice/crypto-square/src/test/java/CryptoSquareTest.java b/exercises/practice/crypto-square/src/test/java/CryptoSquareTest.java index 1ce1f6969..c26f59d53 100644 --- a/exercises/practice/crypto-square/src/test/java/CryptoSquareTest.java +++ b/exercises/practice/crypto-square/src/test/java/CryptoSquareTest.java @@ -1,7 +1,7 @@ -import org.junit.Test; import org.junit.Ignore; +import org.junit.Test; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; public class CryptoSquareTest { @@ -10,7 +10,7 @@ public void emptyPlaintextResultsInEmptyCiphertext() { CryptoSquare cryptoSquare = new CryptoSquare(""); String expectedOutput = ""; - assertEquals(expectedOutput, cryptoSquare.getCiphertext()); + assertThat(cryptoSquare.getCiphertext()).isEqualTo(expectedOutput); } @Ignore("Remove to run test") @@ -19,7 +19,7 @@ public void lettersAreLowerCasedDuringEncryption() { CryptoSquare cryptoSquare = new CryptoSquare("A"); String expectedOutput = "a"; - assertEquals(expectedOutput, cryptoSquare.getCiphertext()); + assertThat(cryptoSquare.getCiphertext()).isEqualTo(expectedOutput); } @Ignore("Remove to run test") @@ -28,7 +28,7 @@ public void spacesAreRemovedDuringEncryption() { CryptoSquare cryptoSquare = new CryptoSquare(" b "); String expectedOutput = "b"; - assertEquals(expectedOutput, cryptoSquare.getCiphertext()); + assertThat(cryptoSquare.getCiphertext()).isEqualTo(expectedOutput); } @Ignore("Remove to run test") @@ -37,7 +37,7 @@ public void punctuationIsRemovedDuringEncryption() { CryptoSquare cryptoSquare = new CryptoSquare("@1,%!"); String expectedOutput = "1"; - assertEquals(expectedOutput, cryptoSquare.getCiphertext()); + assertThat(cryptoSquare.getCiphertext()).isEqualTo(expectedOutput); } @Ignore("Remove to run test") @@ -46,7 +46,7 @@ public void nineCharacterPlaintextResultsInThreeChunksOfThreeCharacters() { CryptoSquare cryptoSquare = new CryptoSquare("This is fun!"); String expectedOutput = "tsf hiu isn"; - assertEquals(expectedOutput, cryptoSquare.getCiphertext()); + assertThat(cryptoSquare.getCiphertext()).isEqualTo(expectedOutput); } @Ignore("Remove to run test") @@ -55,7 +55,7 @@ public void eightCharacterPlaintextResultsInThreeChunksWithATrailingSpace() { CryptoSquare cryptoSquare = new CryptoSquare("Chill out."); String expectedOutput = "clu hlt io "; - assertEquals(expectedOutput, cryptoSquare.getCiphertext()); + assertThat(cryptoSquare.getCiphertext()).isEqualTo(expectedOutput); } @Ignore("Remove to run test") @@ -65,6 +65,6 @@ public void fiftyFourCharacterPlaintextResultsInSevenChunksWithTrailingSpaces() "given us roots."); String expectedOutput = "imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau "; - assertEquals(expectedOutput, cryptoSquare.getCiphertext()); + assertThat(cryptoSquare.getCiphertext()).isEqualTo(expectedOutput); } } diff --git a/exercises/practice/darts/src/test/java/DartsTest.java b/exercises/practice/darts/src/test/java/DartsTest.java index 7e81456a1..815081993 100644 --- a/exercises/practice/darts/src/test/java/DartsTest.java +++ b/exercises/practice/darts/src/test/java/DartsTest.java @@ -1,87 +1,87 @@ import org.junit.Ignore; import org.junit.Test; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; public class DartsTest { Darts darts = new Darts(); @Test public void missedTarget() { - assertEquals(0, darts.score(-9, 9)); + assertThat(darts.score(-9, 9)).isEqualTo(0); } @Ignore("Remove to run test") @Test public void onTheOuterCircle() { - assertEquals(1, darts.score(0, 10)); + assertThat(darts.score(0, 10)).isEqualTo(1); } @Ignore("Remove to run test") @Test public void onTheMiddleCircle() { - assertEquals(5, darts.score(-5, 0)); + assertThat(darts.score(-5, 0)).isEqualTo(5); } @Ignore("Remove to run test") @Test public void onTheInnerCircle() { - assertEquals(10, darts.score(0, -1)); + assertThat(darts.score(0, -1)).isEqualTo(10); } @Ignore("Remove to run test") @Test public void exactlyOnCentre() { - assertEquals(10, darts.score(0, 0)); + assertThat(darts.score(0, 0)).isEqualTo(10); } @Ignore("Remove to run test") @Test public void nearTheCentre() { - assertEquals(10, darts.score(-0.1, -0.1)); + assertThat(darts.score(-0.1, -0.1)).isEqualTo(10); } @Ignore("Remove to run test") @Test public void justWithinTheInnerCircle() { - assertEquals(10, darts.score(0.7, 0.7)); + assertThat(darts.score(0.7, 0.7)).isEqualTo(10); } @Ignore("Remove to run test") @Test public void justOutsideTheInnerCircle() { - assertEquals(5, darts.score(0.8, -0.8)); + assertThat(darts.score(0.8, -0.8)).isEqualTo(5); } @Ignore("Remove to run test") @Test public void justWithinTheMiddleCircle() { - assertEquals(5, darts.score(-3.5, 3.5)); + assertThat(darts.score(-3.5, 3.5)).isEqualTo(5); } @Ignore("Remove to run test") @Test public void justOutsideTheMiddleCircle() { - assertEquals(1, darts.score(-3.6, -3.6)); + assertThat(darts.score(-3.6, -3.6)).isEqualTo(1); } @Ignore("Remove to run test") @Test public void justWithinTheOuterCircle() { - assertEquals(1, darts.score(-7.0, 7.0)); + assertThat(darts.score(-7.0, 7.0)).isEqualTo(1); } @Ignore("Remove to run test") @Test public void justOutsideTheOuterCircle() { - assertEquals(0, darts.score(7.1, -7.1)); + assertThat(darts.score(7.1, -7.1)).isEqualTo(0); } @Ignore("Remove to run test") @Test public void asymmetricPositionBetweenTheInnerAndMiddleCircles() { - assertEquals(5, darts.score(0.5, -4)); + assertThat(darts.score(0.5, -4)).isEqualTo(5); } } diff --git a/exercises/practice/difference-of-squares/src/test/java/DifferenceOfSquaresCalculatorTest.java b/exercises/practice/difference-of-squares/src/test/java/DifferenceOfSquaresCalculatorTest.java index 7e00749c2..e5372d233 100644 --- a/exercises/practice/difference-of-squares/src/test/java/DifferenceOfSquaresCalculatorTest.java +++ b/exercises/practice/difference-of-squares/src/test/java/DifferenceOfSquaresCalculatorTest.java @@ -2,7 +2,7 @@ import org.junit.Ignore; import org.junit.Test; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; public class DifferenceOfSquaresCalculatorTest { @@ -17,7 +17,7 @@ public void setUp() { public void testSquareOfSumUpToOne() { int expected = 1; int actual = calculator.computeSquareOfSumTo(1); - assertEquals(expected, actual); + assertThat(actual).isEqualTo(expected); } @Ignore("Remove to run test") @@ -25,7 +25,7 @@ public void testSquareOfSumUpToOne() { public void testSquareOfSumUpToFive() { int expected = 225; int actual = calculator.computeSquareOfSumTo(5); - assertEquals(expected, actual); + assertThat(actual).isEqualTo(expected); } @Ignore("Remove to run test") @@ -33,7 +33,7 @@ public void testSquareOfSumUpToFive() { public void testSquareOfSumUpToHundred() { int expected = 25502500; int actual = calculator.computeSquareOfSumTo(100); - assertEquals(expected, actual); + assertThat(actual).isEqualTo(expected); } @Ignore("Remove to run test") @@ -41,7 +41,7 @@ public void testSquareOfSumUpToHundred() { public void testSumOfSquaresUpToOne() { int expected = 1; int actual = calculator.computeSumOfSquaresTo(1); - assertEquals(expected, actual); + assertThat(actual).isEqualTo(expected); } @Ignore("Remove to run test") @@ -49,7 +49,7 @@ public void testSumOfSquaresUpToOne() { public void testSumOfSquaresUpToFive() { int expected = 55; int actual = calculator.computeSumOfSquaresTo(5); - assertEquals(expected, actual); + assertThat(actual).isEqualTo(expected); } @Ignore("Remove to run test") @@ -57,7 +57,7 @@ public void testSumOfSquaresUpToFive() { public void testSumOfSquaresUpToHundred() { int expected = 338350; int actual = calculator.computeSumOfSquaresTo(100); - assertEquals(expected, actual); + assertThat(actual).isEqualTo(expected); } @Ignore("Remove to run test") @@ -65,7 +65,7 @@ public void testSumOfSquaresUpToHundred() { public void testDifferenceOfSquaresUpToOne() { int expected = 0; int actual = calculator.computeDifferenceOfSquares(1); - assertEquals(expected, actual); + assertThat(actual).isEqualTo(expected); } @Ignore("Remove to run test") @@ -73,7 +73,7 @@ public void testDifferenceOfSquaresUpToOne() { public void testDifferenceOfSquaresUpToFive() { int expected = 170; int actual = calculator.computeDifferenceOfSquares(5); - assertEquals(expected, actual); + assertThat(actual).isEqualTo(expected); } @Ignore("Remove to run test") @@ -81,7 +81,7 @@ public void testDifferenceOfSquaresUpToFive() { public void testDifferenceOfSquaresUpToHundred() { int expected = 25164150; int actual = calculator.computeDifferenceOfSquares(100); - assertEquals(expected, actual); + assertThat(actual).isEqualTo(expected); } } diff --git a/exercises/practice/diffie-hellman/src/test/java/DiffieHellmanTest.java b/exercises/practice/diffie-hellman/src/test/java/DiffieHellmanTest.java index 16494459f..c2fb08bfa 100644 --- a/exercises/practice/diffie-hellman/src/test/java/DiffieHellmanTest.java +++ b/exercises/practice/diffie-hellman/src/test/java/DiffieHellmanTest.java @@ -2,12 +2,11 @@ import org.junit.Ignore; import org.junit.Test; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertEquals; - import java.math.BigInteger; -import java.util.*; +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; public class DiffieHellmanTest { @@ -27,8 +26,8 @@ public void testPrivateKeyInRange() { } for (BigInteger privateKey : privateKeys) { - assertTrue(privateKey.compareTo(BigInteger.ONE) >= 0); - assertTrue(privateKey.compareTo(prime) < 0); + assertThat(privateKey.compareTo(BigInteger.ONE)).isGreaterThanOrEqualTo(0); + assertThat(privateKey.compareTo(prime)).isLessThan(0); } } @@ -41,7 +40,7 @@ public void testPrivateKeyRandomlyGenerated() { BigInteger privateKeyA = diffieHellman.privateKey(prime); BigInteger privateKeyB = diffieHellman.privateKey(prime); - assertNotEquals(privateKeyA, privateKeyB); + assertThat(privateKeyA).isNotEqualTo(privateKeyB); } @Ignore("Remove to run test") @@ -52,7 +51,7 @@ public void testPublicKeyCorrectlyCalculated() { BigInteger privateKey = BigInteger.valueOf(6); BigInteger expected = BigInteger.valueOf(8); - assertEquals(expected, diffieHellman.publicKey(primeA, primeB, privateKey)); + assertThat(diffieHellman.publicKey(primeA, primeB, privateKey)).isEqualTo(expected); } @Ignore("Remove to run test") @@ -63,7 +62,7 @@ public void testSecretKeyCorrectlyCalculated() { BigInteger privateKey = BigInteger.valueOf(6); BigInteger expected = BigInteger.valueOf(2); - assertEquals(expected, diffieHellman.secret(prime, publicKey, privateKey)); + assertThat(diffieHellman.secret(prime, publicKey, privateKey)).isEqualTo(expected); } @Ignore("Remove to run test") @@ -81,7 +80,7 @@ public void testExchange() { BigInteger secretA = diffieHellman.secret(primeA, bobPublicKey, alicePrivateKey); BigInteger secretB = diffieHellman.secret(primeA, alicePublicKey, bobPrivateKey); - assertEquals(secretA, secretB); + assertThat(secretA).isEqualTo(secretB); } } diff --git a/exercises/practice/dnd-character/src/test/java/DnDCharacterTest.java b/exercises/practice/dnd-character/src/test/java/DnDCharacterTest.java index b2a12fa0f..2097909d5 100644 --- a/exercises/practice/dnd-character/src/test/java/DnDCharacterTest.java +++ b/exercises/practice/dnd-character/src/test/java/DnDCharacterTest.java @@ -3,8 +3,7 @@ import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; public class DnDCharacterTest { @@ -12,139 +11,139 @@ public class DnDCharacterTest { @Test public void testAbilityModifierForScore3IsNegative4() { - assertEquals(-4, dndCharacter.modifier(3)); + assertThat(dndCharacter.modifier(3)).isEqualTo(-4); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore4IsNegative3() { - assertEquals(-3, dndCharacter.modifier(4)); + assertThat(dndCharacter.modifier(4)).isEqualTo(-3); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore5IsNegative3() { - assertEquals(-3, dndCharacter.modifier(5)); + assertThat(dndCharacter.modifier(5)).isEqualTo(-3); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore6IsNegative2() { - assertEquals(-2, dndCharacter.modifier(6)); + assertThat(dndCharacter.modifier(6)).isEqualTo(-2); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore7IsNegative2() { - assertEquals(-2, dndCharacter.modifier(7)); + assertThat(dndCharacter.modifier(7)).isEqualTo(-2); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore8IsNegative1() { - assertEquals(-1, dndCharacter.modifier(8)); + assertThat(dndCharacter.modifier(8)).isEqualTo(-1); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore9IsNegative1() { - assertEquals(-1, dndCharacter.modifier(9)); + assertThat(dndCharacter.modifier(9)).isEqualTo(-1); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore10Is0() { - assertEquals(0, dndCharacter.modifier(10)); + assertThat(dndCharacter.modifier(10)).isEqualTo(0); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore11Is0() { - assertEquals(0, dndCharacter.modifier(11)); + assertThat(dndCharacter.modifier(11)).isEqualTo(0); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore12Is1() { - assertEquals(1, dndCharacter.modifier(12)); + assertThat(dndCharacter.modifier(12)).isEqualTo(1); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore13Is1() { - assertEquals(1, dndCharacter.modifier(13)); + assertThat(dndCharacter.modifier(13)).isEqualTo(1); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore14Is2() { - assertEquals(2, dndCharacter.modifier(14)); + assertThat(dndCharacter.modifier(14)).isEqualTo(2); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore15Is2() { - assertEquals(2, dndCharacter.modifier(15)); + assertThat(dndCharacter.modifier(15)).isEqualTo(2); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore16Is3() { - assertEquals(3, dndCharacter.modifier(16)); + assertThat(dndCharacter.modifier(16)).isEqualTo(3); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore17Is3() { - assertEquals(3, dndCharacter.modifier(17)); + assertThat(dndCharacter.modifier(17)).isEqualTo(3); } @Ignore("Remove to run test") @Test public void testAbilityModifierForScore18Is4() { - assertEquals(4, dndCharacter.modifier(18)); + assertThat(dndCharacter.modifier(18)).isEqualTo(4); } @Ignore("Remove to run test") @Test public void test4DiceWereUsedForRollingScores() { - assertEquals(4, dndCharacter.rollDice().size()); + assertThat(dndCharacter.rollDice().size()).isEqualTo(4); } @Ignore("Remove to run test") @Test public void testDiceValuesBetween1And6() { - assertTrue(dndCharacter.rollDice().stream().allMatch(d -> d >= 1 && d <= 6)); + assertThat(dndCharacter.rollDice()).allMatch(d -> d >= 1 && d <= 6); } @Ignore("Remove to run test") @Test public void testAbilityCalculationsUses3LargestNumbersFromScoresInDescendingOrder() { - assertEquals(9, dndCharacter.ability(List.of(4, 3, 2, 1))); + assertThat(dndCharacter.ability(List.of(4, 3, 2, 1))).isEqualTo(9); } @Ignore("Remove to run test") @Test public void testAbilityCalculationsUses3LargestNumbersFromFromScoresInAscendingOrder() { - assertEquals(9, dndCharacter.ability(List.of(1, 2, 3, 4))); + assertThat(dndCharacter.ability(List.of(1, 2, 3, 4))).isEqualTo(9); } @Ignore("Remove to run test") @Test public void testAbilityCalculationsUses3LargestNumbersFromScoresInRandomOrder() { - assertEquals(9, dndCharacter.ability(List.of(2, 4, 3, 1))); + assertThat(dndCharacter.ability(List.of(2, 4, 3, 1))).isEqualTo(9); } @Ignore("Remove to run test") @Test public void testAbilityCalculationsWithLowestEqualNumbers() { - assertEquals(3, dndCharacter.ability(List.of(1, 1, 1, 1))); + assertThat(dndCharacter.ability(List.of(1, 1, 1, 1))).isEqualTo(3); } @Ignore("Remove to run test") @Test public void testAbilityCalculationsWithHighestEqualNumbers() { - assertEquals(18, dndCharacter.ability(List.of(6, 6, 6, 6))); + assertThat(dndCharacter.ability(List.of(6, 6, 6, 6))).isEqualTo(18); } @Ignore("Remove to run test") @@ -153,8 +152,8 @@ public void testAbilityCalculationDoesNotChangeInputScores() { List scores = List.of(1, 2, 3, 4); dndCharacter.ability(scores); - assertEquals(4, scores.size()); - assertEquals(scores, List.of(1, 2, 3, 4)); + assertThat(scores.size()).isEqualTo(4); + assertThat(scores).containsExactly(1, 2, 3, 4); } @Ignore("Remove to run test") @@ -162,20 +161,20 @@ public void testAbilityCalculationDoesNotChangeInputScores() { public void testRandomCharacterIsValid() { for (int i = 0; i < 1000; i++) { DnDCharacter character = new DnDCharacter(); - assertTrue(character.getStrength() > 2 && character.getStrength() < 19); - assertTrue(character.getDexterity() > 2 && character.getDexterity() < 19); - assertTrue(character.getConstitution() > 2 && character.getConstitution() < 19); - assertTrue(character.getIntelligence() > 2 && character.getIntelligence() < 19); - assertTrue(character.getWisdom() > 2 && character.getWisdom() < 19); - assertTrue(character.getCharisma() > 2 && character.getCharisma() < 19); - assertEquals(10 + character.modifier(character.getConstitution()), character.getHitpoints()); + assertThat(character.getStrength()).isGreaterThan(2).isLessThan(19); + assertThat(character.getDexterity()).isGreaterThan(2).isLessThan(19); + assertThat(character.getConstitution()).isGreaterThan(2).isLessThan(19); + assertThat(character.getIntelligence()).isGreaterThan(2).isLessThan(19); + assertThat(character.getWisdom()).isGreaterThan(2).isLessThan(19); + assertThat(character.getCharisma()).isGreaterThan(2).isLessThan(19); + assertThat(character.getHitpoints()).isEqualTo(10 + character.modifier(character.getConstitution())); } } @Ignore("Remove to run test") @Test public void testEachAbilityIsOnlyCalculatedOnce() { - assertEquals(dndCharacter.getStrength(), dndCharacter.getStrength()); + assertThat(dndCharacter.getStrength()).isEqualTo(dndCharacter.getStrength()); } @Ignore("Remove to run test") @@ -192,7 +191,7 @@ public void testUniqueCharacterIsCreated() { || dnDCharacter.getWisdom() != uniqueDnDCharacter.getWisdom() || dnDCharacter.getCharisma() != uniqueDnDCharacter.getCharisma() || dnDCharacter.getHitpoints() != uniqueDnDCharacter.getHitpoints(); - assertTrue(dnDCharactersHaveDifferentAttributes); + assertThat(dnDCharactersHaveDifferentAttributes).isTrue(); } } } diff --git a/exercises/practice/dominoes/src/test/java/DominoesTest.java b/exercises/practice/dominoes/src/test/java/DominoesTest.java index 3ab906530..9876a25cb 100644 --- a/exercises/practice/dominoes/src/test/java/DominoesTest.java +++ b/exercises/practice/dominoes/src/test/java/DominoesTest.java @@ -1,13 +1,13 @@ -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.junit.Assert.assertEquals; - import org.junit.Ignore; import org.junit.Test; -import java.util.List; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; public class DominoesTest { @@ -19,7 +19,7 @@ public void emtpyInputEmptyOutputTest() throws ChainNotFoundException { List chain = dominoes.formChain(dominoesList); - assertEquals("The output list should be empty.", 0, chain.size()); + assertThat(chain).withFailMessage("The output list should be empty.").hasSize(0); } @Ignore("Remove to run test") @@ -197,14 +197,14 @@ private void assertEndDominoesMatch(List outputDominoes) { + rightValueOfLastDomino + ")."; - assertEquals(errorMessage, leftValueOfFirstDomino, rightValueOfLastDomino); + assertThat(leftValueOfFirstDomino).withFailMessage(errorMessage).isEqualTo(rightValueOfLastDomino); } private void assertSameDominoes(List inputDominoes, List outputDominoes) { String errorMessage = "The number of dominoes in the input list (" + inputDominoes.size() + ") needs to match the number of dominoes in the output chain (" + outputDominoes.size() + ")"; - assertEquals(errorMessage, inputDominoes.size(), outputDominoes.size()); + assertThat(inputDominoes).withFailMessage(errorMessage).hasSameSizeAs(outputDominoes); for (Domino domino : inputDominoes) { int inputFrequency = Collections.frequency(inputDominoes, domino); @@ -214,7 +214,7 @@ private void assertSameDominoes(List inputDominoes, List outputD domino.getRight() + ")" + " in the input is (" + inputFrequency + "), but (" + outputFrequency + ") in the output."; - assertEquals(frequencyErrorMessage, inputFrequency, outputFrequency); + assertThat(inputFrequency).withFailMessage(frequencyErrorMessage).isEqualTo(outputFrequency); } } @@ -229,7 +229,7 @@ private void assertConsecutiveDominoes(List dominoes) { + leftValueOfNextDomino + ")."; - assertEquals(errorMessage, dominoes.get(i).getRight(), dominoes.get(i + 1).getLeft()); + assertThat(dominoes.get(i).getRight()).withFailMessage(errorMessage).isEqualTo(dominoes.get(i + 1).getLeft()); } } } diff --git a/exercises/practice/protein-translation/src/test/java/ProteinTranslatorTest.java b/exercises/practice/protein-translation/src/test/java/ProteinTranslatorTest.java index 5cf0beda9..29bb662e1 100644 --- a/exercises/practice/protein-translation/src/test/java/ProteinTranslatorTest.java +++ b/exercises/practice/protein-translation/src/test/java/ProteinTranslatorTest.java @@ -2,10 +2,7 @@ import org.junit.Ignore; import org.junit.Test; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; public class ProteinTranslatorTest { @@ -18,162 +15,139 @@ public void setUp() { @Test public void testMethionineRnaSequence() { - List expected = Arrays.asList("Methionine"); - assertEquals(expected, proteinTranslator.translate("AUG")); + assertThat(proteinTranslator.translate("AUG")).containsExactly("Methionine"); } @Ignore("Remove to run test") @Test public void testPhenylalanineRnaSequence1() { - List expected = Arrays.asList("Phenylalanine"); - assertEquals(expected, proteinTranslator.translate("UUU")); + assertThat(proteinTranslator.translate("UUU")).containsExactly("Phenylalanine"); } @Ignore("Remove to run test") @Test public void testPhenylalanineRnaSequence2() { - List expected = Arrays.asList("Phenylalanine"); - assertEquals(expected, proteinTranslator.translate("UUC")); + assertThat(proteinTranslator.translate("UUC")).containsExactly("Phenylalanine"); } @Ignore("Remove to run test") @Test public void testLeucineRnaSequence1() { - List expected = Arrays.asList("Leucine"); - assertEquals(expected, proteinTranslator.translate("UUA")); + assertThat(proteinTranslator.translate("UUA")).containsExactly("Leucine"); } @Ignore("Remove to run test") @Test public void testLeucineRnaSequence2() { - List expected = Arrays.asList("Leucine"); - assertEquals(expected, proteinTranslator.translate("UUG")); + assertThat(proteinTranslator.translate("UUG")).containsExactly("Leucine"); } @Ignore("Remove to run test") @Test public void testSerineRnaSequence1() { - List expected = Arrays.asList("Serine"); - assertEquals(expected, proteinTranslator.translate("UCU")); + assertThat(proteinTranslator.translate("UCU")).containsExactly("Serine"); } @Ignore("Remove to run test") @Test public void testSerineRnaSequence2() { - List expected = Arrays.asList("Serine"); - assertEquals(expected, proteinTranslator.translate("UCC")); + assertThat(proteinTranslator.translate("UCC")).containsExactly("Serine"); } @Ignore("Remove to run test") @Test public void testSerineRnaSequence3() { - List expected = Arrays.asList("Serine"); - assertEquals(expected, proteinTranslator.translate("UCA")); + assertThat(proteinTranslator.translate("UCA")).containsExactly("Serine"); } @Ignore("Remove to run test") @Test public void testSerineRnaSequence4() { - List expected = Arrays.asList("Serine"); - assertEquals(expected, proteinTranslator.translate("UCG")); + assertThat(proteinTranslator.translate("UCG")).containsExactly("Serine"); } @Ignore("Remove to run test") @Test public void testTyrosineRnaSequence1() { - List expected = Arrays.asList("Tyrosine"); - assertEquals(expected, proteinTranslator.translate("UAU")); + assertThat(proteinTranslator.translate("UAU")).containsExactly("Tyrosine"); } @Ignore("Remove to run test") @Test public void testTyrosineRnaSequence2() { - List expected = Arrays.asList("Tyrosine"); - assertEquals(expected, proteinTranslator.translate("UAC")); + assertThat(proteinTranslator.translate("UAC")).containsExactly("Tyrosine"); } @Ignore("Remove to run test") @Test public void testCysteineRnaSequence1() { - List expected = Arrays.asList("Cysteine"); - assertEquals(expected, proteinTranslator.translate("UGU")); + assertThat(proteinTranslator.translate("UGU")).containsExactly("Cysteine"); } @Ignore("Remove to run test") @Test public void testCysteineRnaSequence2() { - List expected = Arrays.asList("Cysteine"); - assertEquals(expected, proteinTranslator.translate("UGC")); + assertThat(proteinTranslator.translate("UGC")).containsExactly("Cysteine"); } @Ignore("Remove to run test") @Test public void testTryptophanRnaSequence1() { - List expected = Arrays.asList("Tryptophan"); - assertEquals(expected, proteinTranslator.translate("UGG")); + assertThat(proteinTranslator.translate("UGG")).containsExactly("Tryptophan"); } @Ignore("Remove to run test") @Test public void testStopRnaSequence1() { - List expected = Arrays.asList(); - assertEquals(expected, proteinTranslator.translate("UAA")); + assertThat(proteinTranslator.translate("UAA")).isEmpty(); } @Ignore("Remove to run test") @Test public void testStopRnaSequence2() { - List expected = Arrays.asList(); - assertEquals(expected, proteinTranslator.translate("UAG")); + assertThat(proteinTranslator.translate("UAG")).isEmpty(); } @Ignore("Remove to run test") @Test public void testStopRnaSequence3() { - List expected = Arrays.asList(); - assertEquals(expected, proteinTranslator.translate("UGA")); + assertThat(proteinTranslator.translate("UGA")).isEmpty(); } @Ignore("Remove to run test") @Test public void testTranslationOfRnaToProteinList() { - List expected = Arrays.asList("Methionine", "Phenylalanine", "Tryptophan"); - assertEquals(expected, proteinTranslator.translate("AUGUUUUGG")); + assertThat(proteinTranslator.translate("AUGUUUUGG")).containsExactly("Methionine", "Phenylalanine", "Tryptophan"); } @Ignore("Remove to run test") @Test public void testTranslationStopsIfStopCodonAtBeginning() { - List expected = Arrays.asList(); - assertEquals(expected, proteinTranslator.translate("UAGUGG")); + assertThat(proteinTranslator.translate("UAGUGG")).isEmpty(); } @Ignore("Remove to run test") @Test public void testTranslationStopsIfStopCodonAtEnd1() { - List expected = Arrays.asList("Tryptophan"); - assertEquals(expected, proteinTranslator.translate("UGGUAG")); + assertThat(proteinTranslator.translate("UGGUAG")).containsExactly("Tryptophan"); } @Ignore("Remove to run test") @Test public void testTranslationStopsIfStopCodonAtEnd2() { - List expected = Arrays.asList("Methionine", "Phenylalanine"); - assertEquals(expected, proteinTranslator.translate("AUGUUUUAA")); + assertThat(proteinTranslator.translate("AUGUUUUAA")).containsExactly("Methionine", "Phenylalanine"); } @Ignore("Remove to run test") @Test public void testTranslationStopsIfStopCodonInMiddle1() { - List expected = Arrays.asList("Tryptophan"); - assertEquals(expected, proteinTranslator.translate("UGGUAGUGG")); + assertThat(proteinTranslator.translate("UGGUAGUGG")).containsExactly("Tryptophan"); } @Ignore("Remove to run test") @Test public void testTranslationStopsIfStopCodonInMiddle2() { - List expected = Arrays.asList("Tryptophan", "Cysteine", "Tyrosine"); - assertEquals(expected, proteinTranslator.translate("UGGUGUUAUUAAUGGUUU")); + assertThat(proteinTranslator.translate("UGGUGUUAUUAAUGGUUU")).containsExactly("Tryptophan", "Cysteine", "Tyrosine"); } } diff --git a/exercises/practice/rotational-cipher/src/test/java/RotationalCipherTest.java b/exercises/practice/rotational-cipher/src/test/java/RotationalCipherTest.java index 4a25164b1..a98b6ce97 100644 --- a/exercises/practice/rotational-cipher/src/test/java/RotationalCipherTest.java +++ b/exercises/practice/rotational-cipher/src/test/java/RotationalCipherTest.java @@ -1,7 +1,8 @@ -import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; + public class RotationalCipherTest { private RotationalCipher rotationalCipher; @@ -9,70 +10,69 @@ public class RotationalCipherTest { @Test public void rotateSingleCharacterBy0() { rotationalCipher = new RotationalCipher(0); - Assert.assertEquals("a", rotationalCipher.rotate("a")); + assertThat(rotationalCipher.rotate("a")).isEqualTo("a"); } @Ignore("Remove to run test") @Test public void rotateSingleCharacterBy1() { rotationalCipher = new RotationalCipher(1); - Assert.assertEquals("b", rotationalCipher.rotate("a")); + assertThat(rotationalCipher.rotate("a")).isEqualTo("b"); } @Ignore("Remove to run test") @Test public void rotateSingleCharacterBy26() { rotationalCipher = new RotationalCipher(26); - Assert.assertEquals("a", rotationalCipher.rotate("a")); + assertThat(rotationalCipher.rotate("a")).isEqualTo("a"); } @Ignore("Remove to run test") @Test public void rotateSingleCharacterBy13() { rotationalCipher = new RotationalCipher(13); - Assert.assertEquals("z", rotationalCipher.rotate("m")); + assertThat(rotationalCipher.rotate("m")).isEqualTo("z"); } @Ignore("Remove to run test") @Test public void rotateSingleCharacterWithWrapAround() { rotationalCipher = new RotationalCipher(13); - Assert.assertEquals("a", rotationalCipher.rotate("n")); + assertThat(rotationalCipher.rotate("n")).isEqualTo("a"); } @Ignore("Remove to run test") @Test public void rotateCapitalLetters() { rotationalCipher = new RotationalCipher(5); - Assert.assertEquals("TRL", rotationalCipher.rotate("OMG")); + assertThat(rotationalCipher.rotate("OMG")).isEqualTo("TRL"); } @Ignore("Remove to run test") @Test public void rotateSpaces() { rotationalCipher = new RotationalCipher(5); - Assert.assertEquals("T R L", rotationalCipher.rotate("O M G")); + assertThat(rotationalCipher.rotate("O M G")).isEqualTo("T R L"); } @Ignore("Remove to run test") @Test public void rotateNumbers() { rotationalCipher = new RotationalCipher(4); - Assert.assertEquals("Xiwxmrk 1 2 3 xiwxmrk", rotationalCipher.rotate("Testing 1 2 3 testing")); + assertThat(rotationalCipher.rotate("Testing 1 2 3 testing")).isEqualTo("Xiwxmrk 1 2 3 xiwxmrk"); } @Ignore("Remove to run test") @Test public void rotatePunctuation() { rotationalCipher = new RotationalCipher(21); - Assert.assertEquals("Gzo'n zvo, Bmviyhv!", rotationalCipher.rotate("Let's eat, Grandma!")); + assertThat(rotationalCipher.rotate("Let's eat, Grandma!")).isEqualTo("Gzo'n zvo, Bmviyhv!"); } @Ignore("Remove to run test") @Test public void rotateAllLetters() { rotationalCipher = new RotationalCipher(13); - Assert.assertEquals("The quick brown fox jumps over the lazy dog.", - rotationalCipher.rotate("Gur dhvpx oebja sbk whzcf bire gur ynml qbt.")); + assertThat(rotationalCipher.rotate("Gur dhvpx oebja sbk whzcf bire gur ynml qbt.")).isEqualTo("The quick brown fox jumps over the lazy dog."); } } From b8e2d23375c96e6048f8280ac80b1b7e081cdf97 Mon Sep 17 00:00:00 2001 From: Pavan Baloju Date: Thu, 5 Oct 2023 22:43:17 +0530 Subject: [PATCH 2/4] Fix style check in DominoesTest --- exercises/practice/dominoes/src/test/java/DominoesTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exercises/practice/dominoes/src/test/java/DominoesTest.java b/exercises/practice/dominoes/src/test/java/DominoesTest.java index 9876a25cb..f3a465035 100644 --- a/exercises/practice/dominoes/src/test/java/DominoesTest.java +++ b/exercises/practice/dominoes/src/test/java/DominoesTest.java @@ -229,7 +229,8 @@ private void assertConsecutiveDominoes(List dominoes) { + leftValueOfNextDomino + ")."; - assertThat(dominoes.get(i).getRight()).withFailMessage(errorMessage).isEqualTo(dominoes.get(i + 1).getLeft()); + assertThat(dominoes.get(i).getRight()).withFailMessage(errorMessage) + .isEqualTo(dominoes.get(i + 1).getLeft()); } } } From dbb9397058a9a0a78419f08b5c19eb7503d9a4a6 Mon Sep 17 00:00:00 2001 From: Pavan Baloju Date: Thu, 5 Oct 2023 22:45:16 +0530 Subject: [PATCH 3/4] Fix style check --- .../src/test/java/ProteinTranslatorTest.java | 6 ++++-- .../src/test/java/RotationalCipherTest.java | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/exercises/practice/protein-translation/src/test/java/ProteinTranslatorTest.java b/exercises/practice/protein-translation/src/test/java/ProteinTranslatorTest.java index 29bb662e1..4a184d365 100644 --- a/exercises/practice/protein-translation/src/test/java/ProteinTranslatorTest.java +++ b/exercises/practice/protein-translation/src/test/java/ProteinTranslatorTest.java @@ -117,7 +117,8 @@ public void testStopRnaSequence3() { @Ignore("Remove to run test") @Test public void testTranslationOfRnaToProteinList() { - assertThat(proteinTranslator.translate("AUGUUUUGG")).containsExactly("Methionine", "Phenylalanine", "Tryptophan"); + assertThat(proteinTranslator.translate("AUGUUUUGG")) + .containsExactly("Methionine", "Phenylalanine", "Tryptophan"); } @Ignore("Remove to run test") @@ -147,7 +148,8 @@ public void testTranslationStopsIfStopCodonInMiddle1() { @Ignore("Remove to run test") @Test public void testTranslationStopsIfStopCodonInMiddle2() { - assertThat(proteinTranslator.translate("UGGUGUUAUUAAUGGUUU")).containsExactly("Tryptophan", "Cysteine", "Tyrosine"); + assertThat(proteinTranslator.translate("UGGUGUUAUUAAUGGUUU")) + .containsExactly("Tryptophan", "Cysteine", "Tyrosine"); } } diff --git a/exercises/practice/rotational-cipher/src/test/java/RotationalCipherTest.java b/exercises/practice/rotational-cipher/src/test/java/RotationalCipherTest.java index a98b6ce97..c49624ec2 100644 --- a/exercises/practice/rotational-cipher/src/test/java/RotationalCipherTest.java +++ b/exercises/practice/rotational-cipher/src/test/java/RotationalCipherTest.java @@ -73,6 +73,7 @@ public void rotatePunctuation() { @Test public void rotateAllLetters() { rotationalCipher = new RotationalCipher(13); - assertThat(rotationalCipher.rotate("Gur dhvpx oebja sbk whzcf bire gur ynml qbt.")).isEqualTo("The quick brown fox jumps over the lazy dog."); + assertThat(rotationalCipher.rotate("Gur dhvpx oebja sbk whzcf bire gur ynml qbt.")) + .isEqualTo("The quick brown fox jumps over the lazy dog."); } } From 87384173ec01ba66da102793fd994a58b5a65fbf Mon Sep 17 00:00:00 2001 From: Pavan Baloju Date: Thu, 5 Oct 2023 23:59:15 +0530 Subject: [PATCH 4/4] Revert changes in deprecated exercise --- .../src/test/java/DiffieHellmanTest.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/exercises/practice/diffie-hellman/src/test/java/DiffieHellmanTest.java b/exercises/practice/diffie-hellman/src/test/java/DiffieHellmanTest.java index c2fb08bfa..16494459f 100644 --- a/exercises/practice/diffie-hellman/src/test/java/DiffieHellmanTest.java +++ b/exercises/practice/diffie-hellman/src/test/java/DiffieHellmanTest.java @@ -2,11 +2,12 @@ import org.junit.Ignore; import org.junit.Test; -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.List; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; -import static org.assertj.core.api.Assertions.assertThat; +import java.math.BigInteger; +import java.util.*; public class DiffieHellmanTest { @@ -26,8 +27,8 @@ public void testPrivateKeyInRange() { } for (BigInteger privateKey : privateKeys) { - assertThat(privateKey.compareTo(BigInteger.ONE)).isGreaterThanOrEqualTo(0); - assertThat(privateKey.compareTo(prime)).isLessThan(0); + assertTrue(privateKey.compareTo(BigInteger.ONE) >= 0); + assertTrue(privateKey.compareTo(prime) < 0); } } @@ -40,7 +41,7 @@ public void testPrivateKeyRandomlyGenerated() { BigInteger privateKeyA = diffieHellman.privateKey(prime); BigInteger privateKeyB = diffieHellman.privateKey(prime); - assertThat(privateKeyA).isNotEqualTo(privateKeyB); + assertNotEquals(privateKeyA, privateKeyB); } @Ignore("Remove to run test") @@ -51,7 +52,7 @@ public void testPublicKeyCorrectlyCalculated() { BigInteger privateKey = BigInteger.valueOf(6); BigInteger expected = BigInteger.valueOf(8); - assertThat(diffieHellman.publicKey(primeA, primeB, privateKey)).isEqualTo(expected); + assertEquals(expected, diffieHellman.publicKey(primeA, primeB, privateKey)); } @Ignore("Remove to run test") @@ -62,7 +63,7 @@ public void testSecretKeyCorrectlyCalculated() { BigInteger privateKey = BigInteger.valueOf(6); BigInteger expected = BigInteger.valueOf(2); - assertThat(diffieHellman.secret(prime, publicKey, privateKey)).isEqualTo(expected); + assertEquals(expected, diffieHellman.secret(prime, publicKey, privateKey)); } @Ignore("Remove to run test") @@ -80,7 +81,7 @@ public void testExchange() { BigInteger secretA = diffieHellman.secret(primeA, bobPublicKey, alicePrivateKey); BigInteger secretB = diffieHellman.secret(primeA, alicePublicKey, bobPrivateKey); - assertThat(secretA).isEqualTo(secretB); + assertEquals(secretA, secretB); } }