Wiki source code of Testing-JUnit and TestNG

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

Hide last authors
Ray Kiddy 9.1 1 = Testing with JUnit or TestNG =
fjecker 2.1 2
Ray Kiddy 9.1 3 == Various approaches to initializing your application for testing ==
4
5 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.
6
7 === Create a method which runs once before any tests ===
8
9 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):
10
11 {{code}}
12
13 package example.com.test;
14
15 import er.extensions.appserver.ERXApplication;
16 import java.util.Properties;
17
18 import org.testng.annotations.BeforeTest;
19
20 import com.webobjects.appserver.WOApplication;
21 import com.webobjects.foundation.NSBundle;
22
23 public class WOInit {
24 static NSBundle MYBUNDLE = NSBundle.mainBundle();
25
26 @BeforeTest // init once
27 public void initWO() {
28
29 System.out.println("****InitWO starting...mainbundle is " + NSBundle.mainBundle().bundlePath());
30 // somehow 'new' seems to init the static structures best
31 example.com.app.Application myApp = new example.com.app.Application( ) ;
32 //ERXApplication.primeApplication(NSBundle.mainBundle().bundlePath(), null, "example.com.app.Application");
33 //er.extensions.ERXExtensions.initApp(example.com.app.Application.class, new String[0]) ;
34 System.out.println("****InitWO lists mainbundle:");
35 MYBUNDLE.properties().list(System.out);
36 Properties props = System.getProperties();
37 System.out.println(props.toString());
38 System.out.println("****InitWO ...Finished... ");
39
40 }
41
42 }
43
44 {{/code}}
45
46 There are various ways (some commented out) to initialize your application in the above example.
47
48 * example.com.app.Application myApp = new example.com.app.Application( ) ;
49 * ERXApplication.primeApplication(NSBundle.mainBundle().bundlePath(), null, "example.com.app.Application"); ~/~/(Wonder) or
50 * WOApplication.primeApplication(NSBundle.mainBundle().bundlePath(), null, "example.com.app.Application"); ~/~/(No Wonder)
Johann Werner 12.1 51 * er.extensions.ERXExtensions.initApp(example.com.app.Application.class, new String[WO:0]) ;
Ray Kiddy 9.1 52
53 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.
54
fjecker 2.1 55 == Minor configuration in eclipse ==
56
Johann Werner 11.1 57 To be able to test your business objects layer in JUnit or TestNG test cases, you should set the following parameters in your test case/test suite launch configuration depending on if you are using //normal// or bundle-less builds.
fjecker 2.1 58
Johann Werner 11.1 59 === Normal builds ===
60
61 A //normal// build means that you have checked the build option //Generate bundles// within the WOLips preferences.
62
Johann Werner 12.1 63 [[image:attach:bundle_builds.png]]
Johann Werner 11.1 64
65 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:
66
fjecker 2.1 67 {{noformat}}
68
Ray Kiddy 9.1 69 ${workspace_loc:MyEclipseProject/build/MyEclipseProject.framework}
fjecker 2.1 70
71 {{/noformat}}
72
Johann Werner 12.1 73 For an application it should be (using the handy wolips_dir_loc variable)
fjecker 2.1 74
75 {{noformat}}
76
77 ${working_dir_loc_WOLips:MyEcliseProject}
78
79 {{/noformat}}
80
Johann Werner 12.1 81 [[image:attach:RunConfig1.jpg]]
Greg.Brown 6.1 82
fjecker 2.1 83 Then, in the classpath tab, select the //"User Entries"// entry and use the //"Advanced..."// button and select the //"Add Folders"// button.
Johann Werner 12.1 84 [[image:attach:RunConfig2.jpg]]
Greg.Brown 6.1 85
Johann Werner 11.1 86 Select the //"Java"// folder of your build product. Use the //"Up/Down"// buttons to move this folder at the first position of your classpath.
Johann Werner 12.1 87 [[image:attach:RunConfig3.jpg]]
Johann Werner 10.1 88
Johann Werner 11.1 89 {{note title="Be Careful"}}
Johann Werner 12.1 90 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.
Johann Werner 10.1 91 {{/note}}
92
Johann Werner 11.1 93 The last step is to add the parameter //-DNSProjectBundleEnabled=true// to your VM parameters.
Johann Werner 12.1 94 [[image:attach:vm_arguments.png]]
fjecker 2.1 95
Johann Werner 11.1 96 === Bundle-less builds ===
fjecker 2.1 97
Johann Werner 11.1 98 A //bundle-less// build means that you have unchecked the build option //Generate bundles// within the WOLips preferences.
fjecker 2.1 99
Johann Werner 12.1 100 [[image:attach:bundleless_builds.png]]
Johann Werner 11.1 101
102 That means that there will be no //build// folder in your project. In that case you must set the working directoy to the default:
103
104 {{noformat}}
105
106 ${workspace_loc:MyEclipseProject}
107
108 {{/noformat}}
109
110 On the classpath tab you don't need to add the //"Java"// folder of your build product so only the default classpath should be listed for the //"User Entries"//. Be sure to add the parameter //-DNSProjectBundleEnabled=true// to your VM parameters as for //normal// builds.