Unit Tests
Best Practices For Unit Testing In Java
package org.exersises.example.test;
public class Circle {
public static double calculateArea(double radius) {
return Math.PI * radius * radius;
}
}
package org.exersises.example.test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.junit.Assert;
class CircleTest {
@Test
void test() {
fail("Not yet implemented");
}
@Test
public void givenRadius_whenCalculateArea_thenReturnArea() {
double actualArea = Circle.calculateArea(1d);
double expectedArea = 3.141592653589793;
Assert.assertEquals(expectedArea, actualArea);
}
}
class Solution {
public static void main(String[] args) {
ArrayList<String> strings = new ArrayList<String>();
strings.add("Hello, World!");
strings.add("Welcome to CoderPad.");
strings.add("This pad is running Java " + Runtime.version().feature());
for (String string : strings) {
System.out.println(string);
}
SolutionTest test = new SolutionTest();
test.givenTen_whenFactorial_thenReturnNumber();
//var res = Solution.factorial(10);
//System.out.println(res);
}
public static int factorial(int n) {
if (n<0) return -1;
if (n == 1 || n == 0) return n;
return n*factorial(n-1);
}
}
class SolutionTest {
@Test
public void givenTen_whenFactorial_thenReturnNumber() {
int actualFactorial = Solution.factorial(10);
int expectedFactorial = 36288001;
Assert.assertEquals(expectedFactorial, actualFactorial);
}
}