< JUnit5

JUnit5 - metódusok tesztelése

Szerző: Sallai András

Copyright © Sallai András, 2022

Licenc: CC Attribution-Share Alike 4.0 International

Web: https://szit.hu

VSCode beállítása

{ "java.project.sourcePaths": ["src", "test"], "java.project.outputPath": "bin", "java.project.referencedLibraries": [ "lib/**/*.jar", "c:\\Users\\janos\\Library\\junit\\junit-jupiter-api-5.9.1.jar", "c:\\Users\\janos\\Library\\junit\\junit-platform-console-standalone-1.9.1.jar", "/home/janos/Library/junit/junit-jupiter-api-5.9.1.jar", "/home/janos/Library/junit/junit-platform-console-standalone-1.9.1.jar" ] }

Minta

public class App { public static void main(String[] args) throws Exception { Rhombus rhombus = new Rhombus(); rhombus.calcPerimeter(); } }
public class Rhombus { public double calcPerimeter() { return 0; } public double calcArea() { return 0; } }
import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Method; import java.util.ArrayList; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class RhombusTest { ArrayList<String> rhombusMethodList; @BeforeEach void getMethodList() { this.rhombusMethodList = new ArrayList<>(); Method[] methods = Rhombus.class.getDeclaredMethods(); for(Method method : methods) { this.rhombusMethodList.add(method.getName()); } } @Test void testCalcPerimter() { boolean hasCalcPerimeter = rhombusMethodList.contains("calcPerimeter"); assertTrue(hasCalcPerimeter); } @Test void testCalcArea() { boolean hasCalcArea = rhombusMethodList.contains("calcArea"); assertTrue(hasCalcArea); } }