joplin-notes/joplin notion extension/Web Development/Java/Unit tests.md
2026-11-04 00:33:23 +05:30

215 lines
No EOL
5.8 KiB
Markdown

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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for JUnit Tests</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source> <!-- Specify your desired Java version -->
<target>11</target> <!-- Specify your desired Java version -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifestEntries>
<Main-Class>com.example.demo.MyClass</Main-Class>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
```
- 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));
}
}
```
&nbsp;
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));
}
}
```