Unit tests https://www.freecodecamp.org/news/java-unit-testing/ https://www.youtube.com/watch?v=vZm0lHciFsQ : junit https://www.youtube.com/watch?v=HsQ9OwKA79s : mockito - JUnit are for unit tests. Unit tests means isolating piece of code and then testing it to see if that component is working or not - Initializing maven project Follow the maven project directory structure. Add following in `pom.xml`. In following code junit dependency is added with scope test(means they r recognized in src/test/java not src/main/java) and relevant source and target is set in cofiguration of maven compiler plugin. In configuration of maven jar plugin set the manifest to include class containing main method ```xml 4.0.0 com.example demo 0.0.1-SNAPSHOT demo Demo project for JUnit Tests 17 junit junit 4.10 test org.apache.maven.plugins maven-compiler-plugin 3.8.1 11 11 org.apache.maven.plugins maven-jar-plugin 3.2.0 com.example.demo.MyClass ``` - In following code , it can be noticed that test cases are in same package as in main program. The operation involve for test cases are methods like `assertEquals`, `assertTrue`,`assertNotEquals`, `assertThrows`, `assertDoesNotThrow`. - In GradleClassTest, in assertThrows lambda is used ie `()->` which contains the code to be executed and if throws exception matching the one in first param that the test case will pass Program in `src/main/java/com/example/demo/SimpleCalculator.java` ```java package com.example.demo; public class SimpleCalculator { public int add(int numberA,int numberB){ return numberA+numberB; } } ``` Test case in `src/test/java/com/example/demo/SimpleCalculatorTest.java` ```java package com.example.demo; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class SimpleCalculatorTest { @Test @DisplayName("2+2 equal to 4") void twoPlusTwoShouldEqualFour(){ // var can be used for local variables so it can be used for SimpleCalculator here System.out.println("This test passed"); var calculator=new SimpleCalculator(); assertEquals(4,calculator.add(2, 2)); assertTrue(calculator.add(2, 2)==4 ); } @Test @DisplayName("3+7 equal to 10") void threePlusSevenShouldEqualTen(){ var calculator=new SimpleCalculator(); assertEquals(10, calculator.add(3, 7)); } } ```   in `src/main/java/com/example/demo/GradleClass.java` ```java package com.example.demo; public class GraderClass { public char determineLetterGrade(int numberGrade){ if(numberGrade<0){ throw new IllegalArgumentException("Number grade cannot be negative"); } if(numberGrade <60){ return 'F'; } if(numberGrade <70){ return 'D'; } if(numberGrade <80){ return 'C'; } if(numberGrade <90){ return 'B'; } return 'A'; } } ``` in `src/test/java/com/example/demo/GradleClassTest.java` ```java package com.example.demo; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class GraderClassTest { GraderClass gc; @BeforeEach void setup(){ gc= new GraderClass(); } @Test public void NegativeReturnsIllegalArgumentException(){ assertThrows(IllegalArgumentException.class, ()-> {gc.determineLetterGrade(-1);}); } @Test public void FiftyMeansF(){ assertEquals('F', gc.determineLetterGrade(50)); } @Test public void SixtyMeansD(){ assertEquals('D', gc.determineLetterGrade(60)); } @Test public void SeventyMeansC(){ assertEquals('C', gc.determineLetterGrade(70)); } @Test public void EigtyMeansB(){ assertEquals('B', gc.determineLetterGrade(80)); } @Test public void NintyMeansA(){ assertEquals('A', gc.determineLetterGrade(90)); } } ```