Project

General

Profile

How to Unit Test in Z2 » History » Version 2

Henning Blohm, 16.08.2021 09:55

1 1 Henning Blohm
h1. How to Unit Test in Z2
2
3
The *Z2 Jupiter* of "z2-base.base":http://redmine.z2-environment.net/projects/z2-base/repository/base?rev=master allow to run in-system tests on z2, from anywhere where you can run JUnit tests. To learn more about the JUnit testing framework, please visit http://www.junit.org.
4
5
In-system tests are ordinary unit tests that run within the server environment. Running tests within the running environment is also called integration testing. 
6
7
Standard JUnit tests run in an environment that often has little to do with the tested code's "native" environment, other than seeing the same types. Anything else, database connectivity, domain test data, component and naming abstractions (e.g. JNDI) need to be firstly abstracted out from the tested code and secondly _mocked_, that is, simulated one way or the other, into the test assembly of things. 
8
9
While that has the advantage of a defined, clean-room test bed, for higher-level components this can become unreasonable and assuring the correctness of the mocked up environment becomes a testing issue on its own.
10
11
The z2 Jupiter feature is built on the "JUnit 5":https://junit.org/junit5/ API that succeeded the "JUnit 4":https://junit.org/junit4/ API that is the underlying foundation of the previously promoted z2Unit feature (see [[How to z2Unit]]). It is recommended that you use the z2 Jupiter implementation today starting with z2 Version 2.9.
12
13
The foundation Z2 Jupiter is the Z2 Jupiter Test Engine implementation that delegate test discovery and test execution to a running z2 server runtime. That is, although JUnit believes to run a locally defined test class, the actual test runs in another process, running the methods and reporting results corresponding to the structure of the local test implementation (which matches its server-side equivalent).
14
15
h2. Using z2 Jupiter
16
17
Using z2 Jupiter is as easy as referencing the module *com.zfabrik.dev.z2jupiter* from the test module and adding one more annotation to your test class: "Z2JupiterTestable":http://www.z2-environment.net/javadoc/com.zfabrik.dev.z2jupiter!2Fjava/api/com/zfabrik/dev/z2jupiter/Z2JupiterTestable.html.
18
19 2 Henning Blohm
Z2 Jupiter uses itself for testing. You can see a simple "Hello World" test at source:base|com.zfabrik.dev.z2jupiter/java/src.test/com/zfabrik/dev/z2jupiter/test/HelloZ2JupiterTest.java:
20 1 Henning Blohm
21 2 Henning Blohm
<pre><code class="java">
22
package com.zfabrik.dev.z2jupiter.test;
23 1 Henning Blohm
24 2 Henning Blohm
import org.junit.jupiter.api.Test;
25 1 Henning Blohm
26 2 Henning Blohm
import com.zfabrik.dev.z2jupiter.Z2JupiterTestable;
27
import com.zfabrik.dev.z2jupiter.impl.Z2JupiterImpl;
28 1 Henning Blohm
29 2 Henning Blohm
@Z2JupiterTestable(componentName = Z2JupiterImpl.MODULE_NAME)
30
public class HelloZ2JupiterTest {
31
	
32
	@Test
33
	public void helloZ2Jupiter() {
34
		System.err.println("Hello Z2 Jupiter");
35
	}
36
}
37
</code></pre>
38 1 Henning Blohm
39 2 Henning Blohm
If you want to use Z2 Jupiter in your projects for tests implemented under *java/src.test*, make sure to have the framework referenced in *java/z.properties* like this:
40 1 Henning Blohm
41 2 Henning Blohm
<pre><code class="bash">
42
java.testReferences=\
43
	com.zfabrik.dev.z2jupiter,\
44
</code></pre>
45 1 Henning Blohm
46
h2. To see how this works in Eclipse
47
48
# Follow the "Up in 5 minutes" trail [[Step_2_-_Install_and_run_in_5_minutes]] to make sure you understand how to setup z2 and Eclipse.
49
# Create a Z2 Java project my_tests in your Eclipse workspace. Make sure it is armed.
50
# Add a test reference to *com.zfabrik.dev.z2unit* to your Java component, say *my_tests/java*. 
51
# Create a test class in src.test of your Java component like the following:
52
<pre><code class="java">
53
package mytest;
54
55
import org.junit.Test;
56
import org.junit.runner.RunWith;
57
import com.zfabrik.z2unit.Z2UnitTestRunner;
58
import com.zfabrik.z2unit.annotations.Z2UnitTest;
59
60
@RunWith(Z2UnitTestRunner.class)
61
@Z2UnitTest(componentName="my_tests/java")
62
public class AZ2UnitTest {
63
64
    @Test
65
    public void someTestMethod() {  
66
        System.out.println("Hello z2Unit");
67
    }
68
}
69
</code></pre>
70
# Resolve using the Eclipsoid plugin (see above)
71
# Right-click and choose Run-As / JUnit Test
72
73
h2. To cut things short
74
75
There is a ready-to-use test project, the most simple project with a test class like the one above really. To try that:
76
77
78
# Have z2-base.core and Eclipse set up
79
# Clone "https://www.z2-environment.net/git/z2-samples.z2unit":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-z2unit next to your workspace (Folders matter. No idea why? Follow [[Step_2_-_Install_and_run_in_5_minutes]]!)
80
# Import the project *com.zfabrik.samples.z2unit.hello*
81
# Resolve using the Eclipsoid plugin (see above)
82
# Open the class "com.zfabrik.samples.z2unit.test.AZ2UnitTest":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-z2unit/revisions/master/entry/com.zfabrik.samples.z2unit.hello/java/src.test/com/zfabrik/samples/z2unit/test/AZ2UnitTest.java
83
# Right-click and choose Run-As / JUnit Test
84
85
You should see this:
86
87
!z2unit_screen.png!
88
89
h2. Using and Configuring z2Unit outside of the IDE
90
91
Note that resolving the dependencies and synchronizing the runtime is an important prerequisite to running a z2Unit Unit Test class. You need to have all dependencies of the test class resolved so that JUnit can resolve the test class locally (although all the action will happen elsewhere) and you need to have synchronized so that the matching definitions are found on the server side.
92
93
If you want to automate tests and cannot rely on the Eclipsoid to have a suitable class path, you should use the "com.zfabrik.dev.util/jarRetriever":http://www.z2-environment.eu/v21doc#Retrieving%20jars%20from%20Z2 tool to retrieve all required dependencies. In that case, you can run z2Unit tests just as any unit tests also from an ANT script for example. A typical application is to run z2Unit integration tests as part of your test automation effort.
94
95
The execution of a z2Unit test requires the test client, which is the local JUnit runtime invoked by ANT (or your IDE as above) invoking the @Z2UnitTestRunner@, to connect via HTTP to the Z2 application server. If you run tests on the same operating system instance the application runs on, this works by default by reaching out to localhost. If your application server is on a different machine though, you will want to configure that accordingly.
96
97
For example, you could define a different @z2unitUrl@ when configuring the @Z2UnitTest@ annotation. 
98
99
That is not practical when running test automatically however, as you would not want to change application code for that purpose.
100
101
Instead you can specify essentially the same configuration that the @Z2UnitTest@ annotation holds by setting any of the system properties shown belown during test execution. For example in your ANT script.
102
103
Alternatively you can specify these configuration settings by putting a file called, by default, @z2unit.properties@ on the classpath of the Unit Test Runner (i.e. the ANT test execution classpath for example). The name of the configuration file can be overwritten in a test specific way by setting in on the @Z2UnitTest@ annotation on the test class, in case you need many different configurations.
104
105
Properties that control the behavior of the @Z2UnitTestRunner@, and that may be specified as system properties or via the configuration file are shown in the following table:
106
107
|_. Property Name |_. Property Meaning |
108
| z2unit.className |  Name of the test class. As a result, z2Unit will report test execution events on that class which may not match the structure of the client side class. |
109
| z2unit.componentName | Name of the java component that contains the test class. A short form may be used for java components "/java". |
110
| z2unit.z2unitUrl | URL to send the test request to. Defaults to @http://localhost:8080/z2unit/run@. |
111
112
113
h2. Using other Unit Test Runners
114
115
Using the Z2UnitTest annotation, a test can be configured to run with a non-standard unit test runner. That is useful for example, if your test is actually a test suite or something special like a Spock test. See [[Sample-groovy-in-Z2]] for an example of the latter.
116
117
Furthermore, a unit test can be configured to have some runtime dependencies in Z2, i.e. components that should be prepared prior to the test execution. That is extremely useful, if the test depends on a configured Spring application context for example. In the latter case, the test could even use Spring configuration annotations (which is what we typically do).
118
119
h2. References
120
121
Read on in the "Javadocs":http://www.z2-environment.net/javadoc/com.zfabrik.dev.z2unit!2Fjava/api/com/zfabrik/z2unit/annotations/Z2UnitTest.html for more details.