How-To
Create test
To create a test you must create a class that extends OmnTestBean.
Please have in mind that your class is also loaded as a spring bean (e.g. using component scan).
In general, the abstract class already contains the Component annotation, so it doesn’t need to be used in your class.
However, it is advisable to set the annotation to make it more understandable to another developer.
import com.meylemueller.omn.system.test.OmnTestBean;
import org.springframework.stereotype.Component;
@Component
public class SimpleOmnTestBean extends OmnTestBean
{
//Your test cases
}
For the class to be present, the following Maven dependency must be included.
<dependency>
<groupId>com.meylemueller.omn</groupId>
<artifactId>system-test</artifactId>
<version>${omn.version}</version>
</dependency>
As you know it from JUnit, there is a certain life cycle or rather flowchart.
Since we are working with a real bean in the case of a system test, there is only a "before" (OmnBeforeBean) and an "after" (OmnAfterBean).
As the name suggests, OmnBeforeBean is executed before the actual tests are started. Thus it is suitable for example to prepare the test in such a way that everything needed is present.
OmnAfterBean on the other hand is executed at the end after all tests. Here for example persistent data could be cleaned up again to leave a clean state in the system.
The actual methods in the bean that are to be tested are then randomly processed one after the other.
Only the OmnTest annotation is required for this.
This works in exactly the same way as the Test annotation in JUnit.
For example, if you expect an exception in the test, you only have to set the expected parameter.
import com.meylemueller.omn.system.test.runner.annotation.OmnAfterBean;
import com.meylemueller.omn.system.test.runner.annotation.OmnBeforeBean;
import com.meylemueller.omn.system.test.runner.annotation.OmnIgnore;
import com.meylemueller.omn.system.test.runner.annotation.OmnTest;
import org.springframework.stereotype.Component;
import static org.junit.Assert.assertEquals;
@Component
public class SimpleOmnTestBean extends OmnTestBean
{
private int i = -1;
@OmnBeforeBean
public void before() {
i = 1;
}
@OmnAfterBean
public void after() {
i = 0;
}
@OmnTest
public void test() {
assertEquals("i will only be 1 if OmnBeforeBean is executed", 1, i);
}
@OmnTest
@OmnIgnore // Test will be skipped because of this annotation. But it will be counted as ignored.
public void ignore() {
test();
}
public int getI() {
return i;
}
}
In this example the object is initialized with i=-1.
The OmnBeforeBean sets the value to 1 so that the test() method is satisfied.
Afterwards by OmnAfterBean the value is set to 0. This would have to be checked however over another test outside the bean.
Test order
You have the possibility to define the order of your test beans.
This is possible by a self-defined priority. The bean with the smallest value is called first.
Should it be the case that two beans have the same priority, the decision is made by random.
public class OmnTestBean1 extends OmnTestBean
{
@Override
public int getPriority()
{
return 1;
}
}
public class OmnTestBean10 extends OmnTestBean
{
@Override
public int getPriority()
{
return 10;
}
}
In this case OmnTestBean1 will be executed before OmnTestBean2.
With this help, tests can be structured as well as made interdependent.
Execute tests
In the case of OMN, the system tests are performed by Accelerator.
In the case of projects, this must be built by the customer. Here you can follow the example below.
!!!Please make sure that you have an InfluxDB up and running before execution!!!
import com.meylemueller.omn.system.test.executor.OmnSystemTestExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import java.util.Collections;
@Service
public class ApiCoreSystemTestExecutor implements ApplicationListener<ContextRefreshedEvent> {
@Value("${prj.system.test.influxdb.url}")
private String urlInfluxDb;
@Value("${prj.system.test.influxdb.token}")
private String tokenInfluxDb;
@Value("${prj.system.test.influxdb.orga}")
private String orgaInfluxDb;
@Value("${prj.system.test.influxdb.bucket}")
private String bucketInfluxDb;
@Autowired
private OmnSystemTestExecutor omnSystemTestExecutor;
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
if(null == contextRefreshedEvent.getApplicationContext().getParent()) {
omnSystemTestExecutor.executeTestsWithFilter(
Collections.singletonList("YOUR_PACKAGE_PATH"),
"YOUR_PROCESS_NAME",
urlInfluxDb, tokenInfluxDb, orgaInfluxDb, bucketInfluxDb
);
}
}
}
This example will execute all tests which are available at YOUR_PACKAGE_PATH after the complete Spring context is loaded.