How to z2Unit » History » Version 10
Henning Blohm, 22.10.2013 08:30
1 | 1 | Henning Blohm | h1. How to use z2Unit |
---|---|---|---|
2 | |||
3 | 5 | Henning Blohm | The z2Unit feature of "z2-base.base":http://redmine.z2-environment.net/projects/z2-base/repository/base?rev=master allows 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 | 2 | Henning Blohm | |
5 | 5 | Henning Blohm | 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 | 2 | Henning Blohm | |
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 foundation of the z2Unit feature is a JUnit Test Runner implementation that delegates the actual test execution to a Z2 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). |
||
12 | |||
13 | 10 | Henning Blohm | Z2Unit comes with one annotation ("Z2UnitTest":http://www.z2-environment.net/javadoc/com.zfabrik.dev.z2unit!2Fjava/api/com/zfabrik/z2unit/annotations/Z2UnitTest.html) and one Unit Test Runner ("Z2UnitTestRunner":http://www.z2-environment.net/javadoc/com.zfabrik.dev.z2unit!2Fjava/api/com/zfabrik/z2unit/Z2UnitTestRunner.html). |
14 | |||
15 | 2 | Henning Blohm | |
16 | 9 | Henning Blohm | h2. To see how this works in Eclipse |
17 | 2 | Henning Blohm | |
18 | 6 | Henning Blohm | # 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. |
19 | 4 | Henning Blohm | # Create a Z2 Java project my_tests in your Eclipse workspace. Make sure it is armed. |
20 | 2 | Henning Blohm | # Add a test reference to *com.zfabrik.dev.z2unit* to your Java component, say *my_tests/java*. |
21 | # Create a test class in src.test of your Java component like the following: |
||
22 | <pre><code class="java"> |
||
23 | 6 | Henning Blohm | package mytest; |
24 | 2 | Henning Blohm | |
25 | import org.junit.Test; |
||
26 | import org.junit.runner.RunWith; |
||
27 | import com.zfabrik.z2unit.Z2UnitTestRunner; |
||
28 | import com.zfabrik.z2unit.annotations.Z2UnitTest; |
||
29 | |||
30 | @RunWith(Z2UnitTestRunner.class) |
||
31 | @Z2UnitTest(componentName="my_tests/java") |
||
32 | public class AZ2UnitTest { |
||
33 | |||
34 | @Test |
||
35 | 6 | Henning Blohm | public void someTestMethod() { |
36 | System.out.println("Hello z2Unit"); |
||
37 | 2 | Henning Blohm | } |
38 | 1 | Henning Blohm | } |
39 | </code></pre> |
||
40 | # Resolve using the Eclipsoid plugin (see above) |
||
41 | # Right-click and choose Run-As / JUnit Test |
||
42 | 6 | Henning Blohm | |
43 | 9 | Henning Blohm | h2. To cut things short |
44 | 7 | Henning Blohm | |
45 | 9 | Henning Blohm | There is a ready-to-use test project, the most simple project with a test class like the one above really. To try that: |
46 | 6 | Henning Blohm | |
47 | 9 | Henning Blohm | |
48 | 7 | Henning Blohm | # Have z2-base.core and Eclipse set up |
49 | # Clone "http://git.z2-environment.net/z2-samples.z2unit":http://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]]!) |
||
50 | # Import the project *com.zfabrik.samples.z2unit.hello* |
||
51 | # Resolve using the Eclipsoid plugin (see above) |
||
52 | 8 | Henning Blohm | # 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 |
53 | 7 | Henning Blohm | # Right-click and choose Run-As / JUnit Test |
54 | |||
55 | You should see this: |
||
56 | |||
57 | 1 | Henning Blohm | !z2unit_screen.png! |
58 | 7 | Henning Blohm | |
59 | 9 | Henning Blohm | h2. More notes |
60 | 2 | Henning Blohm | |
61 | 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. |
||
62 | |||
63 | 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. |
||
64 | 3 | Henning Blohm | |
65 | h2. More configuration details |
||
66 | |||
67 | 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. |
||
68 | |||
69 | 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). |
||
70 | |||
71 | 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. |