BAEL-6849 Junit Tests for Interfaces in Java (#14847)
* BAEL-6849 Junit Tests for Interfaces in Java * BAEL-6849 Junit Tests for Interfaces in Java * BAEL-6849 Junit Tests for Interfaces in Java * BAEL-6849 JUnit Tests for Interfaces in Java
This commit is contained in:
parent
d1aa92f178
commit
071d0ca2e9
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.interfaces.unittest;
|
||||
|
||||
public class Circle implements Shape {
|
||||
|
||||
private double radius;
|
||||
|
||||
Circle(double radius) {
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double area() {
|
||||
return 3.14 * radius * radius;
|
||||
}
|
||||
|
||||
public double circumference() {
|
||||
return 2 * 3.14 * radius;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.interfaces.unittest;
|
||||
|
||||
public class Rectangle implements Shape {
|
||||
|
||||
private double length;
|
||||
private double breadth;
|
||||
|
||||
public Rectangle(double length, double breadth) {
|
||||
this.length = length;
|
||||
this.breadth = breadth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double area() {
|
||||
return length * breadth;
|
||||
}
|
||||
|
||||
public double perimeter() {
|
||||
return 2 * (length + breadth);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.interfaces.unittest;
|
||||
|
||||
public interface Shape {
|
||||
|
||||
double area();
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.interfaces.unittest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CircleExtendsBaseUnitTest extends ShapeUnitTest {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> instantiateShapeWithExpectedArea() {
|
||||
Map<String, Object> shapeAreaMap = new HashMap<>();
|
||||
shapeAreaMap.put("shape", new Circle(5));
|
||||
shapeAreaMap.put("area", 78.5);
|
||||
return shapeAreaMap;
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCircumferenceIsCalculated_thenSuccessful() {
|
||||
Circle circle = new Circle(2);
|
||||
double circumference = circle.circumference();
|
||||
assertEquals(12.56, circumference);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.interfaces.unittest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CircleUnitTest {
|
||||
|
||||
@Test
|
||||
void whenAreaIsCalculated_thenSuccessful() {
|
||||
Shape circle = new Circle(5);
|
||||
double area = circle.area();
|
||||
assertEquals(78.5, area);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCircumferenceIsCalculated_thenSuccessful() {
|
||||
Circle circle = new Circle(2);
|
||||
double circumference = circle.circumference();
|
||||
assertEquals(12.56, circumference);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.interfaces.unittest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
public class ParameterizedUnitTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("data")
|
||||
void givenShapeInstance_whenAreaIsCalculated_thenSuccessful(Shape shapeInstance, double expectedArea) {
|
||||
double area = shapeInstance.area();
|
||||
assertEquals(expectedArea, area);
|
||||
|
||||
}
|
||||
|
||||
private static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
{ new Circle(5), 78.5 },
|
||||
{ new Rectangle(4, 5), 20 }
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.interfaces.unittest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class RectangleExtendsBaseUnitTest extends ShapeUnitTest {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> instantiateShapeWithExpectedArea() {
|
||||
Map<String, Object> shapeAreaMap = new HashMap<>();
|
||||
shapeAreaMap.put("shape", new Rectangle(5, 4));
|
||||
shapeAreaMap.put("area", 20.0);
|
||||
return shapeAreaMap;
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenPerimeterIsCalculated_thenSuccessful() {
|
||||
Rectangle rectangle = new Rectangle(5, 4);
|
||||
double perimeter = rectangle.perimeter();
|
||||
assertEquals(18, perimeter);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.interfaces.unittest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class RectangleUnitTest {
|
||||
|
||||
@Test
|
||||
void whenAreaIsCalculated_thenSuccessful() {
|
||||
Shape rectangle = new Rectangle(5, 4);
|
||||
double area = rectangle.area();
|
||||
assertEquals(20, area);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenPerimeterIsCalculated_thenSuccessful() {
|
||||
Rectangle rectangle = new Rectangle(5, 4);
|
||||
double perimeter = rectangle.perimeter();
|
||||
assertEquals(18, perimeter);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.interfaces.unittest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public abstract class ShapeUnitTest {
|
||||
|
||||
public abstract Map<String, Object> instantiateShapeWithExpectedArea();
|
||||
|
||||
@Test
|
||||
void givenShapeInstance_whenAreaIsCalculated_thenSuccessful() {
|
||||
Map<String, Object> shapeAreaMap = instantiateShapeWithExpectedArea();
|
||||
Shape shape = (Shape) shapeAreaMap.get("shape");
|
||||
double expectedArea = (double) shapeAreaMap.get("area");
|
||||
double area = shape.area();
|
||||
assertEquals(expectedArea, area);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue