Skip to content

java testing

devonfw-core edited this page Jan 27, 2022 · 8 revisions

Java testing

Component testing

We are going to test our components as a unit using Spring Test and Devon4j-test modules.

In order to test a basic component of the app first we will create a test class in the src/test/java folder and inside the main package of the test module. We will name the class following the convention.

[Component]Test

Then, in the declaration of the test class, we will use the @SpringBootTest annotation to run the application context. In addition, we will extend the ComponentTest from Devon4j-test module to have access to the main functionalities of the module, see more details here.

Spring Test allows us to use Dependency Injection so we can inject our component directly using the @Inject annotation.

Each test will be represented by a method annotated with @Test. Inside the method we will test one functionality, evaluating the result thanks to the asserts provided by the ComponentTest class that we are extending.

A simple test example

@SpringBootTest(classes = SpringBootApp.class)
public class DishmanagementTest extends `ComponentTest` {

  @Inject
  private Dishmanagement dishmanagement;

  @Test
  public void findAllDishes() {

    PaginatedListTo<DishCto> result = this.dishmanagement.findDishes();
    assertThat(result).isNotNull();
  }

  ...
}

Running the tests

From Eclipse

We can run the test from within Eclipse with the contextual menu Run As > JUnit Test. This functionality can be launched from method level, class level or even package level. The results will be shown in the JUnit tab.

test results eclipse

From command line using Maven

We can also run tests using Maven and the command line, using the command mvn test (or mvn clean test).

`C:\MyThaiStar>mvn clean test`

Doing this we will run all the tests of the project (recognized by the Test word at the end of the classes) and the results will be shown by sub-project.

...

[D: 2017-07-17 09:30:08,457] [P: INFO ] [C: ] [T: Thread-5] [L: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] - [M: Closing JPA EntityManagerFactory for persistence unit 'default']

Results :

Tests run: 11, Failures: 0, Errors: 0, Skipped: 1

...

[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mtsj-server ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mtsj-server ---
[INFO] No tests to run.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] mtsj ............................................... SUCCESS [  0.902 s]
[INFO] mtsj-core .......................................... SUCCESS [02:30 min]
[INFO] mtsj-server ........................................ SUCCESS [  1.123 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:35 min
[INFO] Finished at: 20XX-07-17T09:30:13+02:00
[INFO] Final Memory: 39M/193M
[INFO] ------------------------------------------------------------------------