How to z2Unit » History » Version 2
Henning Blohm, 18.09.2012 18:47
1 | 1 | Henning Blohm | h1. How to use z2Unit |
---|---|---|---|
2 | |||
3 | 2 | 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-container tests in 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-container 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 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 | Z2Unit comes with one annotation and one Unit Test Runner. |
||
14 | |||
15 | To see how this works in Eclipse |
||
16 | |||
17 | # Create a Z2 Java project my_tests in your Eclipse workspace. Make sure it is arned. |
||
18 | # Add a test reference to *com.zfabrik.dev.z2unit* to your Java component, say *my_tests/java*. |
||
19 | # Create a test class in src.test of your Java component like the following: |
||
20 | <pre><code class="java"> |
||
21 | package tests; |
||
22 | |||
23 | import org.junit.Test; |
||
24 | import org.junit.runner.RunWith; |
||
25 | import com.zfabrik.z2unit.Z2UnitTestRunner; |
||
26 | import com.zfabrik.z2unit.annotations.Z2UnitTest; |
||
27 | |||
28 | @RunWith(Z2UnitTestRunner.class) |
||
29 | @Z2UnitTest(componentName="my_tests/java") |
||
30 | public class AZ2UnitTest { |
||
31 | |||
32 | @Test |
||
33 | public void someTestMethod() { |
||
34 | System.out.println("This is just a test method"); |
||
35 | } |
||
36 | } |
||
37 | </code></pre> |
||
38 | # Resolve using the Eclipsoid plugin (see above) |
||
39 | # Right-click and choose Run-As / JUnit Test |
||
40 | |||
41 | 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. |
||
42 | |||
43 | 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. |