Skip to content

devon4j testing

travis edited this page Jan 6, 2020 · 17 revisions

Testing with devon4j

Testing our applications is one of the most important parts of the development. The devon4j documentation provides detailed information about the testing principles. In addition to that, you can also find information about the topic in the devonfw guide.

In this chapter we are going to focus on showing some test examples, and explain briefly how to start testing our devon4j apps.

MyThaiStar Testing Example

In all the devon4j projects (based on Maven and Spring) we are going to find a dedicated package for testing.

MyThaiStar Testing Structure

In addition to this the testing part of the project has also its own resources package, so we are going to be able to configure the application properties or other resources to create specific testing scenarios.

We should incorporate the unit test as one of the main efforts during the development, considering even approaches like TDD.

The tests in our applications should cover a significant amount of functionality, however in this part of the tutorial we are going to focus in the test of our devon4j components.

As you have seen in the previous snapshot, each component of our application should have a dedicated package for testing in the test package. Inside each testing package we will create the related test classes that should follow the naming convention

[Component]Test.java

This is because we are going to use Maven to launch the tests of our application and Maven will look for test classes that end with Test word.

Testing with devon4j means that we have already available Spring Test and devon4j test module which means that we will find a significant amount of annotations and implementations that are going to provide us with all the needed libraries and tools to create tests in a really simply way.

Focusing on the components test means that we are going to test the implementation of the logic layer of our applications. Because of this, you can see in our test structure that our test classes are inside the [component].logic.impl package.

If we open one of the test classes we will find something like this

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

  @Inject
  private Dishmanagement dishmanagement;

  @Test
  public void findAllDishes() {

    DishSearchCriteriaTo criteria = new DishSearchCriteriaTo();
    List<CategoryEto> categories = new ArrayList<>();
    criteria.setCategories(categories);
    PaginatedListTo<DishCto> result = this.dishmanagement.findDishCtos(criteria);
    assertThat(result).isNotNull();
  }
  ...

}
  • @SpringBootTest is the Spring Test annotation to load the context of our application. So we will have the application running like in a real situation.

  • extending the devon4j test class ComponentTest will inject in our test class functionalities like assertions.

  • Spring Test gives us the option for dependency injection, so we are going to be able to @Inject our components to test them.

  • Finally with the @Test annotation we can declare a test to be executed during testing process.

Testing our Application

Now that we have briefly overview we are going to add some tests to our JumpTheQueue application.

We have a main component for managing visitors, so we are going to create a dedicated package for the component within the com.devonfw.application.jtqj package. And inside this new package we are going to add a new test class named VisitormanagementTest.java

JumpTheQuehe Testing Structure
ℹ️

You can see that we already have some test packages in the src/test/java/com.devonfw.application.jtqj.general package. Those tests are from the devon4j archetype and we can use them as model for some tests in our apps.

In the VisitormanagementTest class we are going to add the annotations to run the app context when executing the tests, extend the ComponentTest class to obtain the assertions and inject our visitormanagement component.

import javax.inject.Inject;

import org.springframework.boot.test.context.SpringBootTest;

import com.devonfw.application.jtqj.SpringBootApp;
import com.devonfw.application.jtqj.visitormanagement.logic.api.Visitormanagement;
import com.devonfw.application.jtqj.visitormanagement.logic.api.to.VisitorEto;
import com.devonfw.application.jtqj.visitormanagement.logic.api.to.VisitorSearchCriteriaTo;
import com.devonfw.module.test.common.base.ComponentTest;

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

	  @Inject
	  private Visitormanagement visitormanagement;
}
ℹ️

Please note that the class VisitormanagementTest in the code snippet above extends the class ComponentTest, so check if this is the case in your code.

Now we can start adding our first test. In JumpTheQueue we have two main functionalities:

  • register a visitor returning an access code.

  • list the current visitors.

Let’s add a test to check the first one.

We are going to create a method called with a descriptive name, saveVisitorTest, and we are going to add to it the @Test annotation.

Inside this test we are going to verify the registration process of our app. To do so we only need to call the saveVisitor method of the component and provide a VisitorEto object. After the method is called we are going the check the response of the method to verify that the expected business logic has been executed successfully.

  @Test
  public void saveVisitorTest() {

    VisitorEto visitorEto = new VisitorEto();
    visitorEto.setName("Mary");
    visitorEto.setUsername("mary@mary.com");
    visitorEto.setPhoneNumber("123456789");
    visitorEto.setPassword("test");
    visitorEto.setUserType(false);
    visitorEto.setAcceptedTerms(true);
    visitorEto.setAcceptedCommercial(true);
    VisitorEto visitorEtoResult = this.visitormanagement.saveVisitor(visitorEto);

    assertThat(visitorEtoResult.getId()).isNotNull();

    this.visitormanagement.deleteVisitor(visitorEtoResult.getId());
  }
ℹ️

In this saveVisitorTest() method that we give as an example, we can see that theres a deleteVisitor at the end, this would be only done if the tests are being use agaisnt the production db. In the case that we got a separate db, the last delete is not needed.

ℹ️

Have you noticed that the mock data of the test is the same data that we have used in previous chapters for the manual verification of our services? Exactly, from now on this test will allow us to automate the manual verification process.

Now is the moment for running the test. We can do it in several ways but to simplify the example just select the method to be tested, do right click over it and select Run as > JUnit Test

JumpTheQueue Running Tests
ℹ️

We can also debug our tests using the Debug As > JUnit Test option.

The result of the test will be shown in the JUnit tab of Eclipse

JumpTheQueue Test Results 1

Seems that everything went ok, our register process passes the test. Let’s complete the test checking if the just created user is "Mary".

We can do it simply adding more asserts to check the result object

assertThat(visitorEtoResult.getName()).isEqualTo("Mary");

Now running again the test we should obtain the expected result

umpTheQueue Test Results 2

For the second functionality (finding visitors) we can add a new test with a very similar approach. The only difference is that in this case we are going to need to declare a Search Criteria object, that will contain a pageable to recover the first page and the first 100 values.

  @Test
  public void findVisitorsTest() {

    VisitorSearchCriteriaTo criteria = new VisitorSearchCriteriaTo();
    Pageable pageable = PageRequest.of(0, 100);
    criteria.setPageable(pageable);
    Page<VisitorEto> result = this.visitormanagement.findVisitors(criteria);

    assertThat(result).isNotNull();
  }

Use import org.springframework.data.domain to solve the errors. To run both tests (all the tests included in the class) we only need to do right click in any part of the class and select Run As > JUnit Test. All the methods annotated with @Test will be checked.

umpTheQueue Test Results 3

Extra Functionalities

The devon4j test module provide us with some extra functionalities that we can use to create tests in an easier way.

Extending ComponentTest class we also have available the doSetUp() and doTearDown() methods, that we can use to initialize and release resources in our test classes.

In our JumpTheQueue test class we could declare the visitor object in the doSetUp method, so we can use this resource in several test methods instead of declaring it again and again.

Doing this our test class would be as follows

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

	private VisitorEto visitorEto = new VisitorEto();

	@Inject
	private Visitormanagement visitormanagement;


	@Override
	protected void doSetUp() {
		visitorEto.setName("Mary");
		visitorEto.setUsername("mary@mary.com");
		visitorEto.setPhoneNumber("123456789");
		visitorEto.setPassword("test");
		visitorEto.setUserType(false);
		visitorEto.setAcceptedTerms(true);
		visitorEto.setAcceptedCommercial(true);
	}


  @Test
  public void saveVisitorTest() {

    VisitorEto visitorEtoResult = this.visitormanagement.saveVisitor(visitorEto);

    assertThat(visitorEtoResult.getId()).isNotNull();
    assertThat(visitorEtoResult.getName()).isEqualTo("Mary");

    this.visitormanagement.deleteVisitor(visitorEtoResult.getId());
  }

  @Test
  public void findVisitorsTest() {

    VisitorSearchCriteriaTo criteria = new VisitorSearchCriteriaTo();
    Pageable pageable = PageRequest.of(0, 100);
    criteria.setPageable(pageable);
    Page<VisitorEto> result = this.visitormanagement.findVisitors(criteria);

    assertThat(result).isNotNull();
  }

    ...
}

Running the Tests with Maven

We can use Maven to automate the testing of our project. To do it we simply need to open the devonfw console (console.bat script) or a command line with access to Maven and, in the project, execute the command mvn clean test. With this command Maven will scan for classes named with the Test word and will execute all the tests included in these classes.

If we do it with JumpTheQueue project

\Devon-dist\....\jump-the-queue\java\jtqj>mvn clean test

The result will be similar to this

JumpTheQueue Maven Testing

Even though the test that we have made created finished correctly since theres more tests that devon4j generated automaticly, theres going to be one error related to role assignment. In this tutorial we are going to do only the visitor side so we havent implemented role restriction. We encourage you that after finishing the tutorial you add code and try to make your own.

After that we have seen how to create tests in devonfw, in the next chapter we are going to show how to package and deploy our project.


Next Chapter: Deployment with devonfw