| Line 130: |
Line 130: |
| | | | |
| | ===Package structure to create tests=== | | ===Package structure to create tests=== |
| − | [[File:Pstructjstest.png|left|thumb]] | + | [[File:Pstructjstest.png|thumb|alt=|none]] |
| | | | |
| | The convention is tu follow the structure shown on the image. | | The convention is tu follow the structure shown on the image. |
| | | | |
| | + | === Test example === |
| | + | <syntaxhighlight lang="java"> |
| | + | package com.luv2code.junitdemo; |
| | + | import org.junit.jupiter.api.Test; |
| | + | import org.junit.jupiter.api.Assertions; |
| | | | |
| | + | class DemoUtilsTest { |
| | + | @Test |
| | + | void testEqualsAndNotEquals() { |
| | + | // Set up |
| | + | DemoUtils demoUtils = new DemoUtils(); |
| | + | int expected = 6; |
| | | | |
| | + | // Execute |
| | + | int actual = demoUtils.add(2, 4); |
| | | | |
| | + | // Assert |
| | + | Assertions.assertEquals(expected, actual, "2 + 4 must be 6") |
| | + | } |
| | + | } |
| | + | </syntaxhighlight> |
| | | | |
| | + | === Run unit test === |
| | | | |
| | + | ==== All tests ==== |
| | + | <syntaxhighlight lang="bash"> |
| | + | mvn test |
| | + | </syntaxhighlight> |
| | | | |
| | + | ==== Single test ==== |
| | + | <syntaxhighlight lang="bash"> |
| | + | mvn -Dtest=TestMessageBuilder test |
| | + | </syntaxhighlight> |
| | | | |
| | + | ==== Single test method from one test class ==== |
| | + | <syntaxhighlight lang="bash"> |
| | + | mvn -Dtest=TestMessageBuilder#testHelloWorld test |
| | + | </syntaxhighlight> |
| | | | |
| − |
| |
| − |
| |
| − | <br />
| |
| | ==Example: Spring Boot with Data JPA and in memory database H2== | | ==Example: Spring Boot with Data JPA and in memory database H2== |
| | | | |