├── 1 - Simple Example JUnit ├── .idea │ ├── .gitignore │ ├── compiler.xml │ ├── jarRepositories.xml │ └── misc.xml ├── pom.xml ├── src │ ├── main │ │ └── java │ │ │ ├── Main.java │ │ │ └── MoneyUtil.java │ └── test │ │ └── java │ │ └── MoneyUtilTest.java └── target │ ├── classes │ ├── Main.class │ └── MoneyUtil.class │ └── test-classes │ └── MoneyUtilTest.class ├── 2 - TDD Exercise ├── .idea │ ├── .gitignore │ ├── compiler.xml │ ├── jarRepositories.xml │ └── misc.xml ├── pom.xml ├── src │ ├── main │ │ └── java │ │ │ ├── FizzBuzz.java │ │ │ └── Main.java │ └── test │ │ └── java │ │ └── FizzBuzzTest.java └── target │ ├── classes │ ├── FizzBuzz.class │ └── Main.class │ └── test-classes │ └── FizzBuzzTest.class ├── 3 - Mockito and CleanCode - Exercise ├── .idea │ ├── .gitignore │ ├── compiler.xml │ ├── jarRepositories.xml │ └── misc.xml ├── pom.xml ├── src │ └── main │ │ └── java │ │ ├── Game.java │ │ └── Main.java └── target │ ├── classes │ ├── Game.class │ └── Main.class │ └── test-classes │ └── GameTest.class ├── 3 - Mockito and CleanCode - Resolved ├── .idea │ ├── .gitignore │ ├── compiler.xml │ ├── jarRepositories.xml │ ├── misc.xml │ └── uiDesigner.xml ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ ├── Game.java │ │ │ ├── GameOption.java │ │ │ ├── Main.java │ │ │ └── ScoreBoard.java │ │ └── resources │ │ │ └── mockito-extensions │ │ │ └── org.mockito.plugins.MockMaker │ └── test │ │ └── java │ │ └── GameTest.java └── target │ ├── classes │ ├── Game.class │ ├── GameOption.class │ ├── Main.class │ ├── ScoreBoard.class │ └── mockito-extensions │ │ └── org.mockito.plugins.MockMaker │ └── test-classes │ └── GameTest.class ├── 4 - Cucumber ├── .idea │ ├── .gitignore │ ├── compiler.xml │ ├── jarRepositories.xml │ ├── misc.xml │ └── uiDesigner.xml ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ ├── Game.java │ │ │ ├── GameOption.java │ │ │ ├── Main.java │ │ │ └── ScoreBoard.java │ │ └── resources │ │ │ └── mockito-extensions │ │ │ └── org.mockito.plugins.MockMaker │ └── test │ │ └── java │ │ ├── GameSteps.java │ │ ├── GameTest.java │ │ └── resource │ │ └── game.feature └── target │ ├── classes │ ├── Game.class │ ├── GameOption.class │ ├── Main.class │ ├── ScoreBoard.class │ └── mockito-extensions │ │ └── org.mockito.plugins.MockMaker │ └── test-classes │ ├── GameSteps.class │ └── GameTest.class ├── 5 - Pitest ├── .gitignore ├── README.md ├── doc │ ├── jacoco-step-0.png │ ├── pit-report-detailed-step-2.png │ ├── pit-report-detailed-step-3.png │ ├── pit-report-step-2.png │ ├── pit-report-step-3.png │ └── sonar-step-0.png ├── pitest-examples.iml ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── rdelgatte │ │ └── pitest │ │ └── RangeValidator.java │ └── test │ └── java │ └── com │ └── rdelgatte │ └── pitest │ └── RangeValidatorTest.java └── LICENSE /1 - Simple Example JUnit/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /1 - Simple Example JUnit/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /1 - Simple Example JUnit/.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | -------------------------------------------------------------------------------- /1 - Simple Example JUnit/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /1 - Simple Example JUnit/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | junitproject 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-web 14 | 2.6.7 15 | 16 | 17 | junit 18 | junit 19 | 4.8.2 20 | 21 | 22 | 23 | 24 | 8 25 | 8 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /1 - Simple Example JUnit/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | double salary = 1000; 5 | System.out.println(MoneyUtil.format(salary)); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /1 - Simple Example JUnit/src/main/java/MoneyUtil.java: -------------------------------------------------------------------------------- 1 | import java.math.BigDecimal; 2 | 3 | public class MoneyUtil { 4 | 5 | public static String format(double money) { 6 | return format(money, "$"); 7 | } 8 | 9 | 10 | public static String format(double money, String symbol) { 11 | if (symbol == null) { 12 | throw new IllegalArgumentException(); 13 | } 14 | if (money < 0) { 15 | symbol = "-" + symbol; 16 | money = money * (-1); 17 | } 18 | BigDecimal rounded = BigDecimal.valueOf(money).setScale(2, BigDecimal.ROUND_HALF_UP); 19 | return symbol + rounded; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /1 - Simple Example JUnit/src/test/java/MoneyUtilTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Assert; 2 | import org.junit.Test; 3 | 4 | public class MoneyUtilTest { 5 | 6 | @Test 7 | public void moneyTest() { 8 | String money = MoneyUtil.format(1000.0); 9 | Assert.assertEquals("$1000.00", money); 10 | } 11 | 12 | @Test 13 | public void negativeMoneyTest() { 14 | String money = MoneyUtil.format(-1000.0); 15 | Assert.assertEquals("-$1000.00", money); 16 | } 17 | 18 | @Test 19 | public void euroMoneyTest() { 20 | String money = MoneyUtil.format(-1000.0, "€"); 21 | Assert.assertEquals("-€1000.00", money); 22 | } 23 | 24 | @Test(expected = IllegalArgumentException.class) 25 | public void notNullExceptionMoneyTest() { 26 | MoneyUtil.format(-1000.0, null); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /1 - Simple Example JUnit/target/classes/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/1 - Simple Example JUnit/target/classes/Main.class -------------------------------------------------------------------------------- /1 - Simple Example JUnit/target/classes/MoneyUtil.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/1 - Simple Example JUnit/target/classes/MoneyUtil.class -------------------------------------------------------------------------------- /1 - Simple Example JUnit/target/test-classes/MoneyUtilTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/1 - Simple Example JUnit/target/test-classes/MoneyUtilTest.class -------------------------------------------------------------------------------- /2 - TDD Exercise/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /2 - TDD Exercise/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /2 - TDD Exercise/.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | -------------------------------------------------------------------------------- /2 - TDD Exercise/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /2 - TDD Exercise/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | junitproject 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-web 14 | 2.6.7 15 | 16 | 17 | junit 18 | junit 19 | 4.8.2 20 | 21 | 22 | 23 | 24 | 8 25 | 8 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /2 - TDD Exercise/src/main/java/FizzBuzz.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class FizzBuzz { 5 | 6 | public static final String FIZZ = "Fizz"; 7 | public static final String BUZZ = "Buzz"; 8 | 9 | public List getNumbers() { 10 | List numbers = new ArrayList<>(); 11 | for (int i = 1; i <= 100; i++) { 12 | numbers.add(getNumber(i)); 13 | } 14 | return numbers; 15 | } 16 | 17 | private String getNumber(int i) { 18 | if (isMultiple(i, 3) && isMultiple(i, 5)) { 19 | return FIZZ + BUZZ; 20 | } 21 | else if (isMultiple(i, 3)) { 22 | return FIZZ; 23 | } 24 | else if(isMultiple(i, 5)) { 25 | return BUZZ; 26 | } 27 | return "" + i; 28 | } 29 | 30 | private boolean isMultiple(int number, int multiple) { 31 | return number % multiple == 0; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /2 - TDD Exercise/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /2 - TDD Exercise/src/test/java/FizzBuzzTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Assert; 2 | import org.junit.Before; 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | 7 | public class FizzBuzzTest { 8 | 9 | public static final String FIZZ = "Fizz"; 10 | public static final String BUZZ = "Buzz"; 11 | private FizzBuzz game; 12 | 13 | @Before 14 | public void before() { 15 | game = new FizzBuzz(); 16 | } 17 | 18 | @Test 19 | public void should_returnNumbers_whenNotAMultipleOf3Or5() { 20 | List numbersList = game.getNumbers(); 21 | Assert.assertEquals("1", numbersList.get(0)); 22 | Assert.assertEquals("2", numbersList.get(1)); 23 | Assert.assertEquals("7", numbersList.get(6)); 24 | Assert.assertEquals("13", numbersList.get(12)); 25 | } 26 | 27 | @Test 28 | public void should_returnFizz_whenAMultipleOf3() { 29 | List numbersList = game.getNumbers(); 30 | Assert.assertEquals(FIZZ, numbersList.get(2)); 31 | Assert.assertEquals(FIZZ, numbersList.get(5)); 32 | Assert.assertEquals(FIZZ, numbersList.get(8)); 33 | Assert.assertEquals(FIZZ, numbersList.get(11)); 34 | } 35 | 36 | @Test 37 | public void should_returnBuzz_whenAMultipleOf5() { 38 | List numbersList = game.getNumbers(); 39 | Assert.assertEquals(BUZZ, numbersList.get(4)); 40 | Assert.assertEquals(BUZZ, numbersList.get(9)); 41 | Assert.assertEquals(BUZZ, numbersList.get(19)); 42 | Assert.assertEquals(BUZZ, numbersList.get(24)); 43 | } 44 | 45 | @Test 46 | public void should_returnFizzBuzz_whenAMultipleOf3And5() { 47 | List numbersList = game.getNumbers(); 48 | Assert.assertEquals(FIZZ + BUZZ, numbersList.get(14)); 49 | Assert.assertEquals(FIZZ + BUZZ, numbersList.get(29)); 50 | Assert.assertEquals(FIZZ + BUZZ, numbersList.get(59)); 51 | Assert.assertEquals(FIZZ + BUZZ, numbersList.get(89)); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /2 - TDD Exercise/target/classes/FizzBuzz.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/2 - TDD Exercise/target/classes/FizzBuzz.class -------------------------------------------------------------------------------- /2 - TDD Exercise/target/classes/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/2 - TDD Exercise/target/classes/Main.class -------------------------------------------------------------------------------- /2 - TDD Exercise/target/test-classes/FizzBuzzTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/2 - TDD Exercise/target/test-classes/FizzBuzzTest.class -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Exercise/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Exercise/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Exercise/.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Exercise/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Exercise/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | junitproject 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-web 14 | 2.6.7 15 | 16 | 17 | junit 18 | junit 19 | 4.8.2 20 | 21 | 22 | org.junit.jupiter 23 | junit-jupiter 24 | RELEASE 25 | test 26 | 27 | 28 | 29 | org.mockito 30 | mockito-junit-jupiter 31 | 2.28.2 32 | test 33 | 34 | 35 | 36 | 37 | 38 | 8 39 | 8 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Exercise/src/main/java/Game.java: -------------------------------------------------------------------------------- 1 | import org.springframework.beans.factory.annotation.Autowired; 2 | import org.springframework.stereotype.Component; 3 | 4 | import java.util.Random; 5 | import java.util.Scanner; 6 | 7 | public class Game { 8 | 9 | private Scanner input = new Scanner(System.in); 10 | private Random random = new Random(); 11 | 12 | public void play() { 13 | //start game 14 | System.out.println("Let's play Rock, Paper, Scissors!"); 15 | System.out.println("Say \"Rock\", \"Paper\", or \"Scissors\" to indicate your choice. Otherwise say \"Quit\" to quit."); 16 | String choice = input.nextLine(); //prompt response 17 | choice = choice.toLowerCase(); //change to lowercase for consistency 18 | 19 | //initialize variables 20 | int tienum = 0; 21 | int winnum = 0; 22 | int lossnum = 0; 23 | 24 | while (!choice.equals("quit")) //do the following if the user does not put in "quit" 25 | { 26 | int choicenum = 0; 27 | if (choice.equals("rock")) //assign numbers to string 28 | { 29 | choicenum = 1; 30 | } 31 | else if (choice.equals("paper")) //assign numbers to string 32 | { 33 | choicenum = 2; 34 | } 35 | else if (choice.equals("scissors"))//assign numbers to string 36 | { 37 | choicenum = 3; 38 | } 39 | else //not valid responses 40 | { 41 | while(choicenum == 0) //continue while user input is still not valid 42 | { 43 | System.out.println("Sorry, it looks like you didn't enter a correct input. Try again."); 44 | choice = input.nextLine(); 45 | choice = choice.toLowerCase(); 46 | if (choice.equals("rock")) 47 | { 48 | choicenum = 1; 49 | } 50 | else if (choice.equals("paper")) 51 | { 52 | choicenum = 2; 53 | } 54 | else if (choice.equals("scissors")) 55 | { 56 | choicenum = 3; 57 | } 58 | else if (choice.equals("quit")) 59 | System.exit(0); //quit program 60 | } 61 | } 62 | int compnum = (int) (random.nextInt(3)) + 1;//computer generate random num 63 | //print computer choice 64 | if (compnum == 1) System.out.println("Computer chose rock"); 65 | if (compnum == 2) System.out.println("Computer chose paper"); 66 | if (compnum == 3) System.out.println("Computer chose scissors"); 67 | 68 | 69 | if(choicenum == compnum) //tie cases 70 | { 71 | System.out.println("It's a tie"); 72 | tienum++; 73 | } 74 | else if (choicenum == 1 && compnum == 3) //user wins rock vs scissors 75 | { 76 | System.out.println("you win!"); 77 | winnum++; 78 | } 79 | else if (choicenum == 3 && compnum == 2) //user wins scissors vs paper 80 | { 81 | System.out.println("you win!"); 82 | winnum++; 83 | } 84 | else if (choicenum == 2 && compnum ==1) //user wins paper vs rock 85 | { 86 | System.out.println("you win!"); 87 | winnum++; 88 | } 89 | else //otherwise computer wins 90 | { 91 | System.out.println("you lose."); 92 | lossnum++; 93 | } 94 | System.out.println("wins:" + winnum + "\nloses:" + lossnum + "\nties:" + tienum); //print out number of wins, ties, and loses 95 | System.out.println("Let's play again! \n \n"); //start game again 96 | System.out.println("Say \"Rock\", \"Paper\", or \"Scissors\" to indicate your choice. Otherwise say \"Quit\" to quit."); 97 | choice = input.nextLine(); //prompt for new user input 98 | choice = choice.toLowerCase(); 99 | } 100 | if(choice.equals("quit")) //if user prints "quit", then quit program 101 | return; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Exercise/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | Game game = new Game(); 5 | game.play(); 6 | } 7 | } -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Exercise/target/classes/Game.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/3 - Mockito and CleanCode - Exercise/target/classes/Game.class -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Exercise/target/classes/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/3 - Mockito and CleanCode - Exercise/target/classes/Main.class -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Exercise/target/test-classes/GameTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/3 - Mockito and CleanCode - Exercise/target/test-classes/GameTest.class -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | junitproject 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-web 14 | 2.6.7 15 | 16 | 17 | junit 18 | junit 19 | 4.8.2 20 | 21 | 22 | org.junit.jupiter 23 | junit-jupiter 24 | RELEASE 25 | test 26 | 27 | 28 | 29 | org.mockito 30 | mockito-junit-jupiter 31 | 2.28.2 32 | test 33 | 34 | 35 | 36 | 37 | 38 | 8 39 | 8 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/src/main/java/Game.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | import java.util.Scanner; 3 | 4 | public class Game { 5 | 6 | private Scanner input = new Scanner(System.in); 7 | private Random random = new Random(); 8 | 9 | public void play() { 10 | printGameRules(); 11 | 12 | ScoreBoard scoreBoard = new ScoreBoard(); 13 | String choice = input.nextLine().toUpperCase(); 14 | 15 | while (!choice.equals("QUIT")) 16 | { 17 | GameOption choicenum = getChoiceNum(choice); 18 | while(choicenum == null) 19 | { 20 | System.out.println("Sorry, it looks like you didn't enter a correct input. Try again."); 21 | choice = input.nextLine().toUpperCase(); 22 | choicenum = getChoiceNum(choice); 23 | } 24 | 25 | GameOption compnum = getComputerChoice(); 26 | completeGamePlay(scoreBoard, choicenum, compnum); 27 | printResults(scoreBoard); 28 | 29 | choice = input.nextLine().toUpperCase(); 30 | } 31 | } 32 | 33 | private void printResults(ScoreBoard scoreBoard) { 34 | System.out.println("wins:" + scoreBoard.getWins() + "\nloses:" + scoreBoard.getLosses() + "\nties:" + scoreBoard.getTie()); //print out number of wins, ties, and loses 35 | System.out.println("Let's play again! \n \n"); //start game again 36 | System.out.println("Say \"Rock\", \"Paper\", or \"Scissors\" to indicate your choice. Otherwise say \"Quit\" to quit."); 37 | } 38 | 39 | private void completeGamePlay(ScoreBoard scoreBoard, GameOption choicenum, GameOption compnum) { 40 | if(choicenum == compnum) { 41 | tie(scoreBoard); 42 | } 43 | else if (choicenum == GameOption.ROCK && compnum == GameOption.SCISSORS 44 | || choicenum == GameOption.SCISSORS && compnum == GameOption.PAPER 45 | || (choicenum == GameOption.PAPER && compnum == GameOption.ROCK)) { 46 | wins(scoreBoard); 47 | } 48 | lose(scoreBoard); 49 | } 50 | 51 | private void lose(ScoreBoard scoreBoard) { 52 | System.out.println("you lose."); 53 | scoreBoard.incrementLosses(); 54 | } 55 | 56 | private void tie(ScoreBoard scoreBoard) { 57 | System.out.println("It's a tie"); 58 | scoreBoard.incrementTie(); 59 | } 60 | 61 | private void wins(ScoreBoard scoreBoard) { 62 | System.out.println("you win!"); 63 | scoreBoard.incrementWins(); 64 | } 65 | 66 | private GameOption getChoiceNum(String choice) { 67 | GameOption selectedOption = null; 68 | 69 | if (choice.equals("QUIT")) 70 | System.exit(0); 71 | 72 | try { 73 | selectedOption = GameOption.valueOf(choice); 74 | } catch (Exception e) { 75 | return null; 76 | } 77 | return selectedOption; 78 | } 79 | 80 | private GameOption getComputerChoice() { 81 | GameOption option = GameOption.values()[random.nextInt(3)]; 82 | System.out.println("Computer chose " + option.toString().toLowerCase()); 83 | return option; 84 | } 85 | 86 | private void printGameRules() { 87 | System.out.println("Let's play Rock, Paper, Scissors!"); 88 | System.out.println("Say \"Rock\", \"Paper\", or \"Scissors\" to indicate your choice. Otherwise say \"Quit\" to quit."); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/src/main/java/GameOption.java: -------------------------------------------------------------------------------- 1 | public enum GameOption { 2 | ROCK, PAPER, SCISSORS; 3 | } 4 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | Game game = new Game(); 5 | game.play(); 6 | } 7 | } -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/src/main/java/ScoreBoard.java: -------------------------------------------------------------------------------- 1 | public class ScoreBoard { 2 | 3 | private Integer tie; 4 | private Integer wins; 5 | private Integer losses; 6 | 7 | public ScoreBoard() { 8 | tie = 0; 9 | wins = 0; 10 | losses = 0; 11 | } 12 | 13 | public Integer getTie() { 14 | return tie; 15 | } 16 | 17 | public Integer getWins() { 18 | return wins; 19 | } 20 | 21 | public Integer getLosses() { 22 | return losses; 23 | } 24 | 25 | public void incrementWins() { 26 | wins++; 27 | } 28 | 29 | public void incrementLosses() { 30 | losses++; 31 | } 32 | 33 | public void incrementTie() { 34 | tie++; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/src/main/resources/mockito-extensions/org.mockito.plugins.MockMaker: -------------------------------------------------------------------------------- 1 | mock-maker-inline -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/src/test/java/GameTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Assert; 2 | import org.junit.Before; 3 | import org.junit.Test; 4 | import org.junit.jupiter.api.BeforeEach; 5 | import org.junit.runner.RunWith; 6 | import org.mockito.InjectMocks; 7 | import org.mockito.Mock; 8 | import org.mockito.junit.MockitoJUnitRunner; 9 | 10 | import java.io.ByteArrayOutputStream; 11 | import java.io.PrintStream; 12 | import java.util.Random; 13 | import java.util.Scanner; 14 | 15 | import static org.mockito.Mockito.when; 16 | 17 | @RunWith(MockitoJUnitRunner.class) 18 | public class GameTest { 19 | 20 | public static final int OPTION_ROCK = 0; 21 | public static final int OPTION_PAPER = 1; 22 | public static final int OPTION_SCISSORS = 2; 23 | 24 | @InjectMocks 25 | private Game game; 26 | 27 | @Mock 28 | Scanner scanner; 29 | 30 | @Mock 31 | Random random; 32 | 33 | private ByteArrayOutputStream out; 34 | 35 | @Before 36 | public void setup() { 37 | out = new ByteArrayOutputStream(); 38 | System.setOut(new PrintStream(out)); 39 | } 40 | 41 | @Test 42 | public void when_writeQuit_then_exitGame() { 43 | when(scanner.nextLine()).thenReturn("Quit"); 44 | game.play(); 45 | 46 | Assert.assertTrue(out.toString().contains("Let's play Rock")); 47 | } 48 | 49 | @Test 50 | public void when_chooseRock_then_beatsScissors() { 51 | when(scanner.nextLine()).thenReturn("Rock").thenReturn("Quit"); 52 | when(random.nextInt(3)).thenReturn(OPTION_SCISSORS); 53 | 54 | game.play(); 55 | 56 | Assert.assertTrue(out.toString().contains("Computer chose scissors")); 57 | Assert.assertTrue(out.toString().contains("wins:1")); 58 | } 59 | 60 | 61 | 62 | @Test 63 | public void when_chooseScissors_then_beatsPaper() { 64 | when(scanner.nextLine()).thenReturn("Scissors").thenReturn("Quit"); 65 | when(random.nextInt(3)).thenReturn(OPTION_PAPER); 66 | 67 | game.play(); 68 | 69 | Assert.assertTrue(out.toString().contains("Computer chose paper")); 70 | Assert.assertTrue(out.toString().contains("wins:1")); 71 | } 72 | 73 | @Test 74 | public void when_choosePaper_then_beatsRock() { 75 | when(scanner.nextLine()).thenReturn("Paper").thenReturn("Quit"); 76 | when(random.nextInt(3)).thenReturn(OPTION_ROCK); 77 | 78 | game.play(); 79 | 80 | Assert.assertTrue(out.toString().contains("Computer chose rock")); 81 | Assert.assertTrue(out.toString().contains("wins:1")); 82 | } 83 | 84 | @Test 85 | public void when_bothChooseRock_then_tie() { 86 | when(scanner.nextLine()).thenReturn("Rock").thenReturn("Quit"); 87 | when(random.nextInt(3)).thenReturn(OPTION_ROCK); 88 | 89 | game.play(); 90 | 91 | Assert.assertTrue(out.toString().contains("Computer chose rock")); 92 | Assert.assertTrue(out.toString().contains("ties:1")); 93 | } 94 | 95 | @Test 96 | public void when_chooseRockAndComputerChoosePaper_then_loose() { 97 | when(scanner.nextLine()).thenReturn("Rock").thenReturn("Quit"); 98 | when(random.nextInt(3)).thenReturn(OPTION_PAPER); 99 | 100 | game.play(); 101 | 102 | Assert.assertTrue(out.toString().contains("Computer chose paper")); 103 | Assert.assertTrue(out.toString().contains("loses:1")); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/target/classes/Game.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/3 - Mockito and CleanCode - Resolved/target/classes/Game.class -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/target/classes/GameOption.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/3 - Mockito and CleanCode - Resolved/target/classes/GameOption.class -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/target/classes/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/3 - Mockito and CleanCode - Resolved/target/classes/Main.class -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/target/classes/ScoreBoard.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/3 - Mockito and CleanCode - Resolved/target/classes/ScoreBoard.class -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/target/classes/mockito-extensions/org.mockito.plugins.MockMaker: -------------------------------------------------------------------------------- 1 | mock-maker-inline -------------------------------------------------------------------------------- /3 - Mockito and CleanCode - Resolved/target/test-classes/GameTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/3 - Mockito and CleanCode - Resolved/target/test-classes/GameTest.class -------------------------------------------------------------------------------- /4 - Cucumber/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /4 - Cucumber/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /4 - Cucumber/.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | -------------------------------------------------------------------------------- /4 - Cucumber/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /4 - Cucumber/.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /4 - Cucumber/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | junitproject 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-web 14 | 2.6.7 15 | 16 | 17 | junit 18 | junit 19 | 4.8.2 20 | 21 | 22 | org.junit.jupiter 23 | junit-jupiter 24 | RELEASE 25 | test 26 | 27 | 28 | 29 | org.mockito 30 | mockito-junit-jupiter 31 | 2.28.2 32 | test 33 | 34 | 35 | 36 | io.cucumber 37 | cucumber-java8 38 | 7.0.0 39 | test 40 | 41 | 42 | io.cucumber 43 | cucumber-java 44 | 7.0.0 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | 8 53 | 8 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /4 - Cucumber/src/main/java/Game.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | import java.util.Scanner; 3 | 4 | public class Game { 5 | 6 | private Scanner input = new Scanner(System.in); 7 | private Random random = new Random(); 8 | 9 | public void play() { 10 | printGameRules(); 11 | 12 | ScoreBoard scoreBoard = new ScoreBoard(); 13 | String choice = input.nextLine().toUpperCase(); 14 | 15 | while (!choice.equals("QUIT")) 16 | { 17 | GameOption choicenum = getChoiceNum(choice); 18 | while(choicenum == null) 19 | { 20 | System.out.println("Sorry, it looks like you didn't enter a correct input. Try again."); 21 | choice = input.nextLine().toUpperCase(); 22 | choicenum = getChoiceNum(choice); 23 | } 24 | 25 | GameOption compnum = getComputerChoice(); 26 | completeGamePlay(scoreBoard, choicenum, compnum); 27 | printResults(scoreBoard); 28 | 29 | choice = input.nextLine().toUpperCase(); 30 | } 31 | } 32 | 33 | private void printResults(ScoreBoard scoreBoard) { 34 | System.out.println("wins:" + scoreBoard.getWins() + "\nloses:" + scoreBoard.getLosses() + "\nties:" + scoreBoard.getTie()); //print out number of wins, ties, and loses 35 | System.out.println("Let's play again! \n \n"); //start game again 36 | System.out.println("Say \"Rock\", \"Paper\", or \"Scissors\" to indicate your choice. Otherwise say \"Quit\" to quit."); 37 | } 38 | 39 | private void completeGamePlay(ScoreBoard scoreBoard, GameOption choicenum, GameOption compnum) { 40 | if(choicenum == compnum) { 41 | tie(scoreBoard); 42 | } 43 | else if (choicenum == GameOption.ROCK && compnum == GameOption.SCISSORS 44 | || choicenum == GameOption.SCISSORS && compnum == GameOption.PAPER 45 | || (choicenum == GameOption.PAPER && compnum == GameOption.ROCK)) { 46 | wins(scoreBoard); 47 | } else { 48 | lose(scoreBoard); 49 | } 50 | } 51 | 52 | private void lose(ScoreBoard scoreBoard) { 53 | System.out.println("you lose."); 54 | scoreBoard.incrementLosses(); 55 | } 56 | 57 | private void tie(ScoreBoard scoreBoard) { 58 | System.out.println("It's a tie"); 59 | scoreBoard.incrementTie(); 60 | } 61 | 62 | private void wins(ScoreBoard scoreBoard) { 63 | System.out.println("you win!"); 64 | scoreBoard.incrementWins(); 65 | } 66 | 67 | private GameOption getChoiceNum(String choice) { 68 | GameOption selectedOption = null; 69 | 70 | if (choice.equals("QUIT")) 71 | System.exit(0); 72 | 73 | try { 74 | selectedOption = GameOption.valueOf(choice); 75 | } catch (Exception e) { 76 | return null; 77 | } 78 | return selectedOption; 79 | } 80 | 81 | private GameOption getComputerChoice() { 82 | GameOption option = GameOption.values()[random.nextInt(3)]; 83 | System.out.println("Computer chose " + option.toString().toLowerCase()); 84 | return option; 85 | } 86 | 87 | private void printGameRules() { 88 | System.out.println("Let's play Rock, Paper, Scissors!"); 89 | System.out.println("Say \"Rock\", \"Paper\", or \"Scissors\" to indicate your choice. Otherwise say \"Quit\" to quit."); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /4 - Cucumber/src/main/java/GameOption.java: -------------------------------------------------------------------------------- 1 | public enum GameOption { 2 | ROCK, PAPER, SCISSORS; 3 | } 4 | -------------------------------------------------------------------------------- /4 - Cucumber/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | Game game = new Game(); 5 | game.play(); 6 | } 7 | } -------------------------------------------------------------------------------- /4 - Cucumber/src/main/java/ScoreBoard.java: -------------------------------------------------------------------------------- 1 | public class ScoreBoard { 2 | 3 | private Integer tie; 4 | private Integer wins; 5 | private Integer losses; 6 | 7 | public ScoreBoard() { 8 | tie = 0; 9 | wins = 0; 10 | losses = 0; 11 | } 12 | 13 | public Integer getTie() { 14 | return tie; 15 | } 16 | 17 | public Integer getWins() { 18 | return wins; 19 | } 20 | 21 | public Integer getLosses() { 22 | return losses; 23 | } 24 | 25 | public void incrementWins() { 26 | wins++; 27 | } 28 | 29 | public void incrementLosses() { 30 | losses++; 31 | } 32 | 33 | public void incrementTie() { 34 | tie++; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /4 - Cucumber/src/main/resources/mockito-extensions/org.mockito.plugins.MockMaker: -------------------------------------------------------------------------------- 1 | mock-maker-inline -------------------------------------------------------------------------------- /4 - Cucumber/src/test/java/GameSteps.java: -------------------------------------------------------------------------------- 1 | import io.cucumber.java.Before; 2 | import io.cucumber.java.en.And; 3 | import io.cucumber.java.en.Given; 4 | import io.cucumber.java.en.Then; 5 | import io.cucumber.java.en.When; 6 | import org.junit.Assert; 7 | 8 | import org.junit.runner.RunWith; 9 | import org.mockito.InjectMocks; 10 | import org.mockito.Mock; 11 | import org.mockito.MockitoAnnotations; 12 | import org.mockito.junit.MockitoJUnitRunner; 13 | 14 | import java.io.ByteArrayOutputStream; 15 | import java.io.PrintStream; 16 | import java.util.Random; 17 | import java.util.Scanner; 18 | 19 | import static org.mockito.Mockito.when; 20 | 21 | @RunWith(MockitoJUnitRunner.class) 22 | public class GameSteps { 23 | 24 | public static final int OPTION_ROCK = 0; 25 | public static final int OPTION_PAPER = 1; 26 | public static final int OPTION_SCISSORS = 2; 27 | 28 | @InjectMocks 29 | private Game game; 30 | 31 | @Mock 32 | Scanner scanner; 33 | 34 | @Mock 35 | Random random; 36 | 37 | private ByteArrayOutputStream out; 38 | 39 | @Before 40 | public void setup() { 41 | MockitoAnnotations.initMocks(this); 42 | out = new ByteArrayOutputStream(); 43 | System.setOut(new PrintStream(out)); 44 | } 45 | 46 | @Given("the user will choose {string}") 47 | public void theUserWillChoose(String userSelection) { 48 | MockitoAnnotations.initMocks(this); 49 | when(scanner.nextLine()).thenReturn(userSelection).thenReturn("Quit"); 50 | 51 | } 52 | 53 | @And("the computer will choose {string}") 54 | public void theComputerWillChoose(String computerSelection) { 55 | int selection = 0; 56 | if ("scissors".equals(computerSelection)) { 57 | selection = OPTION_SCISSORS; 58 | } 59 | if ("rock".equals(computerSelection)) { 60 | selection = OPTION_ROCK; 61 | } 62 | if ("paper".equals(computerSelection)) { 63 | selection = OPTION_PAPER; 64 | } 65 | when(random.nextInt(3)).thenReturn(selection); 66 | } 67 | 68 | @When("they play") 69 | public void theyPlay() { 70 | game.play(); 71 | } 72 | 73 | @Then("the user wins") 74 | public void theUserWins() { 75 | Assert.assertTrue(out.toString().contains("wins:1")); 76 | } 77 | 78 | @And("the user lose") 79 | public void theUserLose() { 80 | Assert.assertTrue(out.toString().contains("loses:1")); 81 | } 82 | 83 | @And("the user tie") 84 | public void theUserTie() { 85 | Assert.assertTrue(out.toString().contains("ties:1")); 86 | } 87 | 88 | @Then("verify that the computer chose {string}") 89 | public void verifyThatTheComputerChose(String computerSelection) { 90 | Assert.assertTrue(out.toString().contains("Computer chose " + computerSelection)); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /4 - Cucumber/src/test/java/GameTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Assert; 2 | import org.junit.Before; 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.mockito.InjectMocks; 6 | import org.mockito.Mock; 7 | import org.mockito.MockitoAnnotations; 8 | import org.mockito.junit.MockitoJUnitRunner; 9 | 10 | import java.io.ByteArrayOutputStream; 11 | import java.io.PrintStream; 12 | import java.util.Random; 13 | import java.util.Scanner; 14 | 15 | import static org.mockito.Mockito.when; 16 | 17 | @RunWith(MockitoJUnitRunner.class) 18 | public class GameTest { 19 | 20 | public static final int OPTION_ROCK = 0; 21 | public static final int OPTION_PAPER = 1; 22 | public static final int OPTION_SCISSORS = 2; 23 | 24 | @InjectMocks 25 | private Game game; 26 | 27 | @Mock 28 | Scanner scanner; 29 | 30 | @Mock 31 | Random random; 32 | 33 | private ByteArrayOutputStream out; 34 | 35 | @Before 36 | public void setup() { 37 | MockitoAnnotations.initMocks(this); 38 | out = new ByteArrayOutputStream(); 39 | System.setOut(new PrintStream(out)); 40 | } 41 | 42 | @Test 43 | public void when_writeQuit_then_exitGame() { 44 | when(scanner.nextLine()).thenReturn("Quit"); 45 | game.play(); 46 | 47 | Assert.assertTrue(out.toString().contains("Let's play Rock")); 48 | } 49 | 50 | @Test 51 | public void when_chooseRock_then_beatsScissors() { 52 | when(scanner.nextLine()).thenReturn("Rock").thenReturn("Quit"); 53 | when(random.nextInt(3)).thenReturn(OPTION_SCISSORS); 54 | 55 | game.play(); 56 | 57 | Assert.assertTrue(out.toString().contains("Computer chose scissors")); 58 | Assert.assertTrue(out.toString().contains("wins:1")); 59 | } 60 | 61 | 62 | 63 | @Test 64 | public void when_chooseScissors_then_beatsPaper() { 65 | when(scanner.nextLine()).thenReturn("Scissors").thenReturn("Quit"); 66 | when(random.nextInt(3)).thenReturn(OPTION_PAPER); 67 | 68 | game.play(); 69 | 70 | Assert.assertTrue(out.toString().contains("Computer chose paper")); 71 | Assert.assertTrue(out.toString().contains("wins:1")); 72 | } 73 | 74 | @Test 75 | public void when_choosePaper_then_beatsRock() { 76 | when(scanner.nextLine()).thenReturn("Paper").thenReturn("Quit"); 77 | when(random.nextInt(3)).thenReturn(OPTION_ROCK); 78 | 79 | game.play(); 80 | 81 | Assert.assertTrue(out.toString().contains("Computer chose rock")); 82 | Assert.assertTrue(out.toString().contains("wins:1")); 83 | } 84 | 85 | @Test 86 | public void when_bothChooseRock_then_tie() { 87 | when(scanner.nextLine()).thenReturn("Rock").thenReturn("Quit"); 88 | when(random.nextInt(3)).thenReturn(OPTION_ROCK); 89 | 90 | game.play(); 91 | 92 | Assert.assertTrue(out.toString().contains("Computer chose rock")); 93 | Assert.assertTrue(out.toString().contains("ties:1")); 94 | } 95 | 96 | @Test 97 | public void when_chooseRockAndComputerChoosePaper_then_loose() { 98 | when(scanner.nextLine()).thenReturn("Rock").thenReturn("Quit"); 99 | when(random.nextInt(3)).thenReturn(OPTION_PAPER); 100 | 101 | game.play(); 102 | 103 | Assert.assertTrue(out.toString().contains("Computer chose paper")); 104 | Assert.assertTrue(out.toString().contains("loses:1")); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /4 - Cucumber/src/test/java/resource/game.feature: -------------------------------------------------------------------------------- 1 | Feature: Game Rock, Paper, or Scissors 2 | 3 | Scenario: The user wins when chooses Rock and computer chooses Scissors 4 | Given the user will choose "rock" 5 | And the computer will choose "scissors" 6 | When they play 7 | Then verify that the computer chose "scissors" 8 | And the user wins 9 | 10 | Scenario: The user wins when chooses Scissors and computer chooses Paper 11 | Given the user will choose "scissors" 12 | And the computer will choose "paper" 13 | When they play 14 | Then verify that the computer chose "paper" 15 | And the user wins 16 | 17 | Scenario: The user wins when chooses Paper and computer chooses Rock 18 | Given the user will choose "paper" 19 | And the computer will choose "rock" 20 | When they play 21 | Then verify that the computer chose "rock" 22 | And the user wins 23 | 24 | Scenario: The user lose when chooses Rock and computer chooses Paper 25 | Given the user will choose "rock" 26 | And the computer will choose "paper" 27 | When they play 28 | Then verify that the computer chose "paper" 29 | And the user lose 30 | 31 | Scenario: The user tie when chooses Rock and computer chooses Rock 32 | Given the user will choose "rock" 33 | And the computer will choose "rock" 34 | When they play 35 | Then verify that the computer chose "rock" 36 | And the user tie -------------------------------------------------------------------------------- /4 - Cucumber/target/classes/Game.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/4 - Cucumber/target/classes/Game.class -------------------------------------------------------------------------------- /4 - Cucumber/target/classes/GameOption.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/4 - Cucumber/target/classes/GameOption.class -------------------------------------------------------------------------------- /4 - Cucumber/target/classes/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/4 - Cucumber/target/classes/Main.class -------------------------------------------------------------------------------- /4 - Cucumber/target/classes/ScoreBoard.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/4 - Cucumber/target/classes/ScoreBoard.class -------------------------------------------------------------------------------- /4 - Cucumber/target/classes/mockito-extensions/org.mockito.plugins.MockMaker: -------------------------------------------------------------------------------- 1 | mock-maker-inline -------------------------------------------------------------------------------- /4 - Cucumber/target/test-classes/GameSteps.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/4 - Cucumber/target/test-classes/GameSteps.class -------------------------------------------------------------------------------- /4 - Cucumber/target/test-classes/GameTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/4 - Cucumber/target/test-classes/GameTest.class -------------------------------------------------------------------------------- /5 - Pitest/.gitignore: -------------------------------------------------------------------------------- 1 | # Intellij 2 | .idea/ 3 | 4 | # Mac 5 | .DS_Store 6 | 7 | # Maven 8 | target/ 9 | 10 | /pitestdemo.iml 11 | -------------------------------------------------------------------------------- /5 - Pitest/README.md: -------------------------------------------------------------------------------- 1 | # Mutation testing with Pitest 2 | 3 | This basic project aims to highlight benefits of mutation testing in Java using [Pitest](http://pitest.org) 4 | 5 | ## RangeValidator 6 | It assesses whether a provided input number is between 0 and 100 like below: 7 | 8 | ``` 9 | boolean isValid(int input) { 10 | return input > 0 && input <= 100; 11 | } 12 | ``` 13 | 14 | ### 1) Initial state (`git checkout step-0`) 15 | In this step, we can notice that our code is fully covered by unit tests: 16 | 17 | ![Jacoco report](doc/jacoco-step-0.png) 18 | *Jacoco report* 19 | 20 | ![Sonar report](doc/sonar-step-0.png) 21 | *Sonar report* 22 | 23 | But when looking at the unit tests, we can notice they are assessing nothing: 24 | ``` 25 | @Test 26 | @DisplayName("Should return true given 50") 27 | void fifty_isValid_returnsTrue() { 28 | cut.isValid(50); 29 | } 30 | 31 | @Test 32 | @DisplayName("Should return false given 200") 33 | void twoHundred_isValid_returnsFalse() { 34 | cut.isValid(200); 35 | } 36 | 37 | @Test 38 | @DisplayName("Should return false given -10") 39 | void minus10_isValid_returnsTrue() { 40 | cut.isValid(-10); 41 | } 42 | ``` 43 | 44 | Both line and branch coverage are reporting a 100% unit tests covering but nothing is being tested really!! 45 | 46 | 47 | ### 2) Adding assertions (`git checkout step-1`) 48 | Let's assert that use cases! 49 | 50 | ``` 51 | @Test 52 | @DisplayName("Should return true given 50") 53 | void fifty_isValid_returnsTrue() { 54 | assertThat(cut.isValid(50)).isTrue(); 55 | } 56 | 57 | @Test 58 | @DisplayName("Should return false given 200") 59 | void twoHundred_isValid_returnsFalse() { 60 | assertThat(cut.isValid(200)).isFalse(); 61 | } 62 | 63 | @Test 64 | @DisplayName("Should return false given -10") 65 | void minus10_isValid_returnsTrue() { 66 | assertThat(cut.isValid(-10)).isFalse(); 67 | } 68 | ``` 69 | 70 | Ok we do feel better now right? Reports from Sonar / Jacoco still look good with 100% code coverage reporting but can we go further? 71 | 72 | 73 | ### 3) Mutation testing (`git checkout step-2`) 74 | Let's talk about mutation testing! 75 | 76 | Adding Pitest to our `pom.xml` file: 77 | ``` 78 | 79 | org.pitest 80 | pitest-maven 81 | ${pitest.maven.version} 82 | 83 | 84 | org.pitest 85 | pitest-junit5-plugin 86 | 0.3 87 | 88 | 89 | 90 | 91 | XML 92 | HTML 93 | 94 | 95 | com.rdelgatte.pitest.* 96 | 97 | 98 | com.rdelgatte.pitest.* 99 | 100 | true 101 | 102 | false 103 | 104 | 105 | ``` 106 | 107 | - Run pitest mutation coverage using `mvn test pitest:mutationCoverage` 108 | - Look at Pitest report (see `console` or `target/pit-reports/index.html` file) 109 | 110 | 111 | ![Pitest report](doc/pit-report-step-2.png) 112 | *Pitest report* 113 | 114 | Here we can notice the line coverage is still 100% but a new coverage has been introduced here: `Mutation Coverage` 115 | 116 | ![Pitest detailed report](doc/pit-report-detailed-step-2.png) 117 | *Pitest detailed report* 118 | 119 | Pitest executed tests after mutating our original source code and discovered some mutations are not handled by unit tests so we need to fix that. 120 | 121 | To do so, we should cover cases including limit test case which means when provided value is either 0 and 100. 122 | 123 | Following are the test cases to cover mutation testing: 124 | ``` 125 | @Test 126 | @DisplayName("Should return true given 50") 127 | void fifty_isValid_returnsTrue() { 128 | assertThat(cut.isValid(50)).isTrue(); 129 | } 130 | 131 | @Test 132 | @DisplayName("Should return false given 200") 133 | void twoHundred_isValid_returnsFalse() { 134 | assertThat(cut.isValid(200)).isFalse(); 135 | } 136 | 137 | @Test 138 | @DisplayName("Should return true given 100") 139 | void hundred_isValid_returnsTrue() { 140 | assertThat(cut.isValid(100)).isTrue(); 141 | } 142 | 143 | @Test 144 | @DisplayName("Should return false given 0") 145 | void zero_isValid_returnsFalse() { 146 | assertThat(cut.isValid(0)).isFalse(); 147 | } 148 | ``` 149 | 150 | Running again Pitest mutation coverage command and looking at its report, we can notice bot line and mutation coverage look 100% good: 151 | 152 | ![Pitest report](doc/pit-report-step-3.png) 153 | *Pitest report after mutation test coverage* 154 | 155 | ![Pitest detailed report](doc/pit-report-detailed-step-3.png) 156 | *Pitest detailed report after mutation test coverage* 157 | 158 | You can see more about this case (step by step) in the following [introduction to Mutation Testing](https://rdelgatte.github.io/pitest-presentation/). -------------------------------------------------------------------------------- /5 - Pitest/doc/jacoco-step-0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/5 - Pitest/doc/jacoco-step-0.png -------------------------------------------------------------------------------- /5 - Pitest/doc/pit-report-detailed-step-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/5 - Pitest/doc/pit-report-detailed-step-2.png -------------------------------------------------------------------------------- /5 - Pitest/doc/pit-report-detailed-step-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/5 - Pitest/doc/pit-report-detailed-step-3.png -------------------------------------------------------------------------------- /5 - Pitest/doc/pit-report-step-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/5 - Pitest/doc/pit-report-step-2.png -------------------------------------------------------------------------------- /5 - Pitest/doc/pit-report-step-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/5 - Pitest/doc/pit-report-step-3.png -------------------------------------------------------------------------------- /5 - Pitest/doc/sonar-step-0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasmoy-dev/Java-Testing-Course/8cb467f8d77fa2580bdd4a78bbbf19c785162645/5 - Pitest/doc/sonar-step-0.png -------------------------------------------------------------------------------- /5 - Pitest/pitest-examples.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /5 - Pitest/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.rdelgatte 8 | pitest-examples 9 | 1.0-SNAPSHOT 10 | 11 | 12 | UTF-8 13 | 1.8 14 | 3.7.0 15 | 2.22.0 16 | 0.8.2 17 | 1.3.0 18 | 5.3.0 19 | 1.0.0 20 | 3.9.0 21 | 22 | 23 | 24 | 25 | 26 | org.apache.maven.plugins 27 | maven-compiler-plugin 28 | 3.7.0 29 | 30 | ${java.version} 31 | ${java.version} 32 | 33 | 34 | 35 | 36 | org.apache.maven.plugins 37 | maven-surefire-plugin 38 | ${maven.surefire.plugin.version} 39 | 40 | 41 | 42 | org.jacoco 43 | jacoco-maven-plugin 44 | ${jacoco.maven.plugin.version} 45 | 46 | 47 | 48 | prepare-agent 49 | report 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.pitest 57 | pitest-maven 58 | ${pitest.maven.version} 59 | 60 | 61 | org.pitest 62 | pitest-junit5-plugin 63 | 0.3 64 | 65 | 66 | 67 | 68 | XML 69 | HTML 70 | 71 | 72 | com.rdelgatte.pitest.* 73 | 74 | 75 | com.rdelgatte.pitest.* 76 | 77 | true 78 | 79 | false 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | org.junit.jupiter 88 | junit-jupiter-engine 89 | ${junit.jupiter.engine.version} 90 | test 91 | 92 | 93 | org.junit.platform 94 | junit-platform-launcher 95 | ${junit.platform.launcher.version} 96 | test 97 | 98 | 99 | org.assertj 100 | assertj-core 101 | ${assertj.core.version} 102 | test 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /5 - Pitest/src/main/java/com/rdelgatte/pitest/RangeValidator.java: -------------------------------------------------------------------------------- 1 | package com.rdelgatte.pitest; 2 | 3 | class RangeValidator { 4 | 5 | boolean isValid(int input) { 6 | return input > 0 && input <= 100; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /5 - Pitest/src/test/java/com/rdelgatte/pitest/RangeValidatorTest.java: -------------------------------------------------------------------------------- 1 | package com.rdelgatte.pitest; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.DisplayName; 7 | import org.junit.jupiter.api.Test; 8 | 9 | class RangeValidatorTest { 10 | 11 | private RangeValidator cut; 12 | 13 | @BeforeEach 14 | void setUp() { 15 | cut = new RangeValidator(); 16 | } 17 | 18 | /** 19 | * {@link RangeValidator#isValid(int)} 20 | */ 21 | 22 | @Test 23 | @DisplayName("Should return true given 50") 24 | void fifty_isValid_returnsTrue() { 25 | assertThat(cut.isValid(50)).isTrue(); 26 | } 27 | 28 | @Test 29 | @DisplayName("Should return false given 200") 30 | void twoHundred_isValid_returnsFalse() { 31 | assertThat(cut.isValid(200)).isFalse(); 32 | } 33 | 34 | @Test 35 | @DisplayName("Should return true given 100") 36 | void hundred_isValid_returnsTrue() { 37 | assertThat(cut.isValid(100)).isTrue(); 38 | } 39 | 40 | @Test 41 | @DisplayName("Should return false given 0") 42 | void zero_isValid_returnsFalse() { 43 | assertThat(cut.isValid(0)).isFalse(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 lucasmoy-dev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------