Changes

Jump to navigation Jump to search
4,182 bytes added ,  13:38, 23 June 2022
m
Line 268: Line 268:  
assertThrows(Exception.class, ()-> {functionCallThatThrowsError(params);})
 
assertThrows(Exception.class, ()-> {functionCallThatThrowsError(params);})
   −
assertDoesNotThrows(Exception.class, ()-> {functionCallThatDoesNotThrowsError(params);})
+
assertDoesNotThrows( ()-> {functionCallThatDoesNotThrowsError(params);})
   −
assertTimeoutPreemptively
+
assertTimeoutPreemptively(Duration.ofSeconds(3), () -> { demoUtils.checkTimeout(); }, "Method should execute in 3 seconds");
    +
=== Run tests in order ===
 +
By default the order of the tests is deterministic but not obious, you can force the order<syntaxhighlight lang="java">
 +
// MethodOrderer.DisplayName, MethodOrderer..MethodName, MethodOrderer.Random, MethodOrderer.OrderAnnotation
    +
@TestMethodOrder(MethodOrderer.DisplayName.class)
 +
class DemoUtilsTest {
 +
...
 +
    @DisplayName("Equals and not equals")
 +
    void testEqualsAndNotEquals()...
 +
 +
// ----------------------------------------------
 +
 +
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
 +
class DemoUtilsTest {
 +
...
 +
    @DisplayName("Equals and not equals")
 +
    @Order(1)
 +
    void testEqualsAndNotEquals()...
 +
</syntaxhighlight>
 +
 +
=== Configure maven to run tests ===
 +
pom.xml<syntaxhighlight lang="xml">
 +
<!-- Spring Boot -->
 +
 +
    <dependency>
 +
      <groupId>org.springframework.boot</groupId>
 +
      <artifactId>spring-boot-starter-test</artifactId>
 +
      <scope>test</scope>
 +
    </dependency>
 +
 +
<!-- Java -->
 +
    <dependency>
 +
      <groupId>org.apache.maven.plugins</groupId>
 +
      <artifactId>maven-surefire-plugin</artifactId>
 +
      <version>3.0.0-M5</version>
 +
      <scope>test</scope>
 +
    </dependency>
 +
 +
</syntaxhighlight>
 +
 +
=== Code coverage ===
 +
IntelliJ has built-in support for code coverage Maven can too, requires plugin like maven-surefire-report-plugin<syntaxhighlight lang="xml">
 +
<build>
 +
    <plugins>
 +
    <plugin>
 +
      <groupId>org.apache.maven.plugins</groupId>
 +
      <artifactId>maven-surefire-report-plugin</artifactId>
 +
      <version>3.0.0-M5</version>
 +
      <executions>
 +
          <execution>
 +
              <phase>test</phase>
 +
              <goals>
 +
                  <goal>report</goal>
 +
              <goals>
 +
          </execution>
 +
      </executions>     
 +
 +
    </plugin>
 +
</plugins>
 +
</build>
 +
</syntaxhighlight>mvn clean test
 +
 +
mvn site -DgenerateReports=false
 +
<br /><syntaxhighlight lang="xml">
 +
// at the surefire plugin
 +
 +
....
 +
            <configuration>
 +
                <testFailureIgnore>true</testFailureIgnore>
 +
                <statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
 +
                    <usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodname>
 +
                </statelessTestsetReporter>
 +
            </configuration>
 +
        </plugin>
 +
.....
 +
</syntaxhighlight>
 +
 +
=== JaCoCo (Java Code Coverage) ===
 +
<syntaxhighlight lang="xml">
 +
...
 +
<plugin>
 +
    <groupId>org.jacoco</groupId>
 +
    <artifactId>jacoco-maven-plugin</artifactId>
 +
    <version>0.8.7</version>
 +
 +
    <executions>
 +
        <execution>
 +
            <id>jacoco-prepare</id>
 +
            <goals>
 +
                <goal>prepare-agent</goal>
 +
            </goals>
 +
        </execution>
 +
        <execution>
 +
            <id>jacoco-report</id>
 +
            <phase>test</phase>
 +
            <goals>
 +
                  <goal>report</goal>
 +
            </goals>
 +
        </execution>
 +
    </executions>
 +
</plugin>
 
....
 
....
 +
</syntaxhighlight>
 +
 +
=== Disable tests ===
 +
<syntaxhighlight lang="java">
 +
class ConditionalTest {
 +
    @Test
 +
    @Disabled("Dont run this test until ticket @34 is solved")
 +
    void basicTest() {}
 +
 +
    @Test
 +
    @EnabledOnOs(OS.WINDOWS, OS.MAC, OS.LINUX)
 +
    void testForAllSystems() {}
 +
 +
    @Test
 +
    @EnabledOnJre(JRE.JAVA_17)
 +
    void testOnJavaVersion() {}
 +
 +
    @Test
 +
    @EnabledOnJreRange(min=JRE.JAVA_13, max=JRE.JAVA_18)  // If only min provided works until latest java
 +
    void testOnJavaVersionRange() {}
 +
 +
    @Test
 +
    @EnabledIfSystemProperty(named="SOMEPROPERTYNAME", matches="SOMEVALUE")
 +
    void testOnJavaVersion() {}
 +
 +
    @Test
 +
    @EnabledIfEnvironmentVariable(named="SOMEPROPERTYNAME", matches="SOMEVALUE")
 +
    void testOnJavaVersion() {}
 +
 +
 +
}
 +
</syntaxhighlight>
 +
 +
=== Parameterized Tests ===
 +
<syntaxhighlight lang="java">
 +
// @parameterizedTest
 +
@parameterizedTest(name="value={0}, expected={1}")
 +
/* @CsvSource({
 +
    "1,1",
 +
    "2,2",
 +
    "3,Fizz",
 +
    "4,4",
 +
    "5,Buzz"
 +
})*/
 +
@CsvFileSource(resources="/small-test-data.csv")  // src/test/resources/small-test-data.csv
 +
void testCsvData(int value, String expected){
 +
    assertEquals(expected, FizzBuzz.compute(value));
 +
}
 +
</syntaxhighlight>
    
==Example: Spring Boot with Data JPA and in memory database H2==
 
==Example: Spring Boot with Data JPA and in memory database H2==

Navigation menu