-
Notifications
You must be signed in to change notification settings - Fork 0
/
faq.htm
112 lines (98 loc) · 8.81 KB
/
faq.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>StrutsTestCase for JUnit</title>
<LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Default">
</head>
<body>
<center><h2>Frequently Asked Questions About StrutsTestCase for JUnit</h2></center>
<ul>
<li> <a href="#10">Is there a discussion group for StrutsTestCase?</a>
<li> <a href="#1">Are there more detailed instructions on how to run the StrutsTestCase examples?</a>
<li> <a href="#2">Does StrutsTestCase support Tiles?</a>
<li> <a href="#3">Why does my test fail complaining about "Missing configuration resource for path /WEB-INF/struts-config.xml"? </a>
<li> <a href="#4">Why do I keep getting NoClassDefFoundErrors when I run my test case? </a>
<li> <a href="#5">Why do I keep getting LinkageError's when I run my test case? </a>
<li> <a href="#6">Why is a session value I set in one test method not available in another test method? </a>
<li> <a href="#7">How can I use the Struts transaction token support in my test cases? </a>
<li> <a href="#8">Can I access the ActionForm for my test before and after the Action is executed?</a>
<li> <a href="#9">When I use <tt>setActionForm()</tt>, why are the form attributes I set not showing up in my Action class? </a>
<li> <a href="#11">How can I test uploading a file using StrutsTestCase?</a>
<li> <a href="#12">Why does my test stop executing before the ActionForward is processed?</a>
<li> <a href="#13">Can I test multiple Actions in the same test method?</a>
</ul>
<h3 id=10>Is there a discussion group for StrutsTestCase?</h3>
Yes, there is an active <a href="http://sourceforge.net/forum/?group_id=39190">user forum</a> hosted on the SourceForge site.
<h3 id=1>Are there more detailed instructions on how to run the StrutsTestCase examples?</h3>
The StrutsTestCase distribution comes with several sample classes, and step-by-step instructions on how to run them. Please
see the the README.txt file in the examples directory for more details.
<h3 id=2>Does StrutsTestCase support Tiles?</h3>
Yes, StrutsTestCase supports Tiles. If your test are failing, make sure that your CLASSPATH is set properly, so that
StrutsTestCase can find the appropriate resource files, specifically the tiles definitions file. See the next FAQ
entry for more details.
<h3 id=3>Why does my test fail complaining about "Missing configuration resource for path /WEB-INF/struts-config.xml"?</h3>
This usually occurs when your CLASSPATH is not set properly to point to the directory <i>that contains</i> the WEB-INF directory.
For example, if your struts-config.xml is located in the following directory:<br><br>
<tt>/my/directory/WEB-INF/struts-config.xml</tt>
<p>.. then you would set the CLASSPATH as follows:</p>
<tt>CLASSPATH=/my/directory</tt>
<p>The same applies for all other resource files, like tiles definitions and the like. As an alternative, you can use
the <a href="api/servletunit/struts/MockStrutsTestCase.html#setConfigFile(java.lang.String)"><tt>setConfigFile</tt></a> method
to point to your configuration file, keeping in mind that you must reference the file appropriately.</p>
<h3 id=4>Why do I keep getting NoClassDefFoundErrors when I run my test case?</h3>
Assuming that your CLASSPATH is indeed set up properly, there are two situations where this problem might occur:
<ul>
<li>If the NoClassDefFoundError you're receiving complains about a class called <tt><b>ActionConfig</b></tt>, then you are probably trying to use a version of strutstest.jar built against Struts 1.1 in a Struts 1.0 environment. Get the version of StrutsTestCase appropriate to your test environment.<br><br>
<li>If the NoClassDefFoundError you're receiving complains about either <tt><b>ServletRequestWrapper</b></tt> or a <tt><b>ServletResponseWrapper</b></tt>, then you are probably trying to run a version of strutstest.jar that supports Servlet 2.3 in a Servlet engine that only supports the 2.2 specification. Get the version of StrutsTestCase appropriate to your test environment.<br><br>
<li>If the NoClassDefFoundError you're receiving complains about <b>Tiles classes</b> (eg: <tt>DefinitionsUtil</tt> or <tt>ComponentDefinition</tt>), you must include the tiles.jar in your CLASSPATH. This should only an issue for Struts 1.1b1.
</ul>
<h3 id=5>Why do I keep getting LinkageError's when I run my test case?</h3>
There are three known causes for this problem:
<ul>
<li><tt><b>CLASSPATH issues: </b></tt> If your CLASSPATH contains multiple XML libraries that are incompatible, you will get this error. One solution is to places <tt><b>xerces.jar</b></tt> at the front of your CLASSPATH, which ensures that a compatible set of interfaces and implementation classes are loaded.
<li><tt><b>JUnit Swing UI TestRunner: </b></tt> If you are using the <tt>swingui.TestRunner</tt> class, you must uncheck the <tt><b>reload classes every run</b></tt> box before you run your tests.
<li><tt><b>Ant <junit> task: </b></tt> If you are using the <tt><junit></tt> task, you must make sure the <b><tt>fork</tt></b> attribute is set to <b><tt>true</tt></b> before you run your tests.
</ul>
<h3 id=6>Why is a session value I set in one test method not available in another test method?</h3>
StrutsTestCase follows a common JUnit practice of not saving state between unit tests. This is to avoid side effects when running many unit tests at once, each of which should remain independent of one another. If you wish to test an Action subclass that depends on another Action or Servlet having set up one or more state variables, you should simulate this behavior by setting the state variables in the session object (via the <tt>getSession()</tt> method) or the request object (via the <tt>getRequest()</tt> method). Alternately, you can test several Action subclasses in a sequence by calling setRequestPathInfo() and actionPerform() for each one. For more information, please see the <a href="http://www.junit.org">JUnit site</a>.
<h3 id=7>How can I use the Struts transaction token support in my test cases?</h3>
You can use the Struts transaction token in your tests by using the <b><tt>addRequestParameter</tt></b> method, thusly:
<pre>
public void testTransactionToken() {
<em>addRequestParameter(Constants.TOKEN_KEY, "test_token");
getSession().setAttribute(Globals.TRANSACTION_TOKEN_KEY, "test_token");</em>
setRequestPathInfo("/testToken");
actionPerform();
verifyNoActionErrors();
}
</pre>
<h3 id=8>Can I access the ActionForm for my test before and after the Action is executed?</h3>
Yes, you can. Both MockStrutsTestCase and CactusStrutsTestCase have a <tt><b>setActionForm()</b></tt> method for
setting the ActionForm for a given test, and a <tt><b>getActionForm()</b></tt> method for retrieving an ActionForm instance
after the Action is executed.
<h3 id=9>When I use <tt>setActionForm()</tt>, why are the form attributes I set not showing up in my Action class?</h3>
Even though you can pass an ActionForm instance to your Action, it does not mean you are subverting how
Struts handles the form processing. In particular, if you have overridden the ActionForm.reset() method,
then you could potentially be resetting any attributes you had set in the ActionForm instance you
pass to setActionForm(). Likewise, any request parameters you pass along that correspond to attributes
in your ActionForm instance will replace whatever you set in your test method.
<h3 id=11>How can I test uploading a file using StrutsTestCase?</h3>
This is a tricky operation to test, but at least one enterprising user has found <a href="https://sourceforge.net/forum/message.php?msg_id=1812671">a way to do it</a>.
<h3 id=12>Why does my test stop executing before the ActionForward is processed?</h3>
StrutsTestCase is designed to test only a single Action class within a test method, and as such,
by default does not actually process the forward, be it another action or a JSP page. This minimizes side effects
that can produce spurious test results. If you are using Cactus, you can override this behavior by calling the
<a href="api/servletunit/struts/CactusStrutsTestCase.html#processRequest(boolean)">processRequest</a> method so that the
request is fully processed by the container.
<br/><br/>
You can also simulate calling multiple actions in a MockStrutsTestCase test method by calling
setRequestPathInfo() and actionPerform() multiple times within the same method. If you choose to do
this, however, you should call <a href="api/servletunit/struts/MockStrutsTestCase.html#clearRequestParameters()">clearRequestParameters()</a> to
ensure that no unexpected request parameters are passed along.
<h3 id=13>Can I test multiple Actions in the same test method?</h3>
Yes you can -- see the <a href="#12">previous question.</a>
<br>
<br>
<br>
<center><b>Still scratching your head? Please post a message on the <a href="http://sourceforge.net/forum/?group_id=39190">user forums</a>.</b></center>
</body>