Testing with JUnit

Summary: The automatic analysis tests that a building block has a correct behavior and fulfills its contract. In addition, you should test if each operation within the block works correctly. For that, you can design test suits for your blocks with JUnit, a widely used framework for unit testing. You can read more about JUnit testing here.

Create a new Test Project

  • Select File / New / Project…
  • Select Reactive Blocks / Reactive Blocks Project.
  • Choose the JUnit project setup.

Generally, it is a good idea to create a new test project for each library that you want to test. We recommend to use the same qualified name as the library under test, with .tests added. So, for our library org.reactiveblocks.library.foo, we create a test project called org.reactiveblocks.library.foo.tests.

Create a new Test Case

Within the test project, create a new application building block. Each application provides one test run, and it's okay to have many applications and many tests within a test project. Usually, each test system focuses on a single block from a library to test. In the example, we have created Foo Test 1 to test the block Foo of our library.

A test usually starts by preparing some data. Here we do that in the simple operation prepare. However, you can involve any other building blocks to prepare the system for testing. The verdict of the test is written in operations as well. Here we have a simple setup: Whenever the block answers via pin bad, the test should fail. Therefore, the method failed is pretty simple:

  import org.junit.Assert;
  ...
  public void testFailed() {
      Assert.fail("Test failed, too bad!");
  }

The class Assert comes from JUnit, and is on the classpath because the JUnit libraries were added during the project setup. In addition to the simple method fail, it also contains methods to compare data.

Implementing the Test Case

To implement all test cases within your test project do the following:

  • Select and Right-click on the project in the Blocks view.
  • Select Build -> Build JUnit Test Project.

This will implement all test cases within the project, and compile them into a single JUnit test suite that can be executed in one single run.

Ignoring a Test Case

If you want that certain test cases within the same project being excluded from the test suite, just tag the corresponding block with the tag ignore within the Overview page of that block.

Running the Test Suite

  • Right-click the generated test suite project.
  • Select Run as / JUnit Test.

As a result, the JUnit view of Eclipse shows up, executes the tests and shows the test results.

In the example, two of the test cases failed. In this case, the message provided with the verdict method will show up, and helps you to locate the error.

Build JUnit Projects

A JUnit test project () typically contains several test blocks. For this type of project you can build all test systems with one action. Choose Build from the context menu of such project in the Block view. Then, select Build JUnit Test Project to start the build process.

You can also build all test blocks in your workspace with a single click. For this, choose option Build all JUnit Test Projects. This option is only shown when you have several JUnit projects in your workspace. Similar to building a system, you will be prompted with a dialog that enables you to change the logging framework and level, the generated project name, etc. All executable code is placed in one project that is highlighted when the build process is done.