Changes for page Testing-JUnit and TestNG

Last modified by Johann Werner on 2012/08/06 03:19

From version 5.1
edited by fjecker
on 2009/05/19 06:39
Change comment: There is no comment for this version
To version 6.1
edited by Greg.Brown
on 2009/09/26 15:58
Change comment: There is no comment for this version

Summary

Details

Page properties
Title
... ... @@ -1,1 +1,1 @@
1 -Testing-JUnit
1 +Testing-JUnit and TestNG
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.fjecker
1 +XWiki.gbrown
Content
... ... @@ -1,9 +1,9 @@
1 -= Testing with JUnit =
1 += Testing with JUnit or TestNG =
2 2  
3 3  == Minor configuration in eclipse ==
4 4  
5 5  To be able to test your business objects layer in JUnit test cases, you should set the following parameters in your test case/test suite launch configuration :
6 -First in the //"Arguements"// tab, set your working directory to the build product of your project. For example, for a framework project the working directory should be :
6 +First in the //"Arguments"// tab, set your working directory to the build product of your project. For example, for a framework project the working directory should be :
7 7  
8 8  {{noformat}}
9 9  
... ... @@ -19,9 +19,14 @@
19 19  
20 20  {{/noformat}}
21 21  
22 +[[image:RunConfig1.jpg]]
23 +
22 22  Then, in the classpath tab, select the //"User Entries"// entry and use the //"Advanced..."// button and select the //"Add Folders"// button.
25 +[[image:RunConfig2.jpg]]
26 +
23 23  Select the //"Java"// folder of your build product.
24 24  Use the //"Up/Down"// buttons to move this folder at the first position of your classpath.
29 +[[image:RunConfig3.jpg]]
25 25  
26 26  {{note title="Be Careful"}}
27 27  
... ... @@ -28,3 +28,55 @@
28 28  If during your tests execution you see error messages such as *cannot cast EOGenericRecord to <your object>* please check that the _"Java"_ folder is at first position in your classpath entries.
29 29  
30 30  {{/note}}
36 +
37 +== Various approaches to initializing your application for testing ==
38 +
39 +For unit type tests, initialization isn't usually a problem, as unit testing only tests a small "unit" isolated and not interacting with all the other "units" which make up your application. But there are other tests which may require many units of your application working cooperatively, and in order to work properly, all these units must be initialized properly.
40 +
41 +=== Create a method which runs once before any tests ===
42 +
43 +Your application should be initialzed just once before the tests. There are annotations etc. which allow one to pick a method which will be run prior to any tests. For a TestNG example (similar to Junit 4):
44 +
45 +{{code}}
46 +
47 +package example.com.test;
48 +
49 +import er.extensions.appserver.ERXApplication;
50 +import java.util.Properties;
51 +
52 +import org.testng.annotations.BeforeTest;
53 +
54 +import com.webobjects.appserver.WOApplication;
55 +import com.webobjects.foundation.NSBundle;
56 +
57 +public class WOInit {
58 + static NSBundle MYBUNDLE = NSBundle.mainBundle();
59 +
60 + @BeforeTest // init once
61 + public void initWO() {
62 +
63 + System.out.println("****InitWO starting...mainbundle is " + NSBundle.mainBundle().bundlePath());
64 + // somehow 'new' seems to init the static structures best
65 + example.com.app.Application myApp = new example.com.app.Application( ) ;
66 + //ERXApplication.primeApplication(NSBundle.mainBundle().bundlePath(), null, "example.com.app.Application");
67 + //er.extensions.ERXExtensions.initApp(example.com.app.Application.class, new String[0]) ;
68 + System.out.println("****InitWO lists mainbundle:");
69 + MYBUNDLE.properties().list(System.out);
70 + Properties props = System.getProperties();
71 + System.out.println(props.toString());
72 + System.out.println("****InitWO ...Finished... ");
73 +
74 + }
75 +
76 +}
77 +
78 +{{/code}}
79 +
80 +There are various ways (some commented out) to initialize your application in the above example.
81 +
82 +* example.com.app.Application myApp = new example.com.app.Application( ) ;
83 +* ERXApplication.primeApplication(NSBundle.mainBundle().bundlePath(), null, "example.com.app.Application"); ~/~/(Wonder) or
84 +* WOApplication.primeApplication(NSBundle.mainBundle().bundlePath(), null, "example.com.app.Application"); ~/~/(No Wonder)
85 +* er.extensions.ERXExtensions.initApp(example.com.app.Application.class, new String0) ;
86 +
87 +One of them may work best in your situation. It may be important for your application to find its main bundle properly, and all the goodies bound up in that main bundle.