I had tried using proxy.execute(), which will use Struts. Maybe that's
where I'm a bit lost. I don't implement execute() because of my CRUD
approach.
Can someone point me to some appropriate struts 2 junit tests so I can
figure this out?
Musachy Barroso wrote:
you are creating an action and invoking a method on it, Struts is not
involved at all in that. You might want to look at the struts junits
to see how you can invoke an action.
musachy
On Mon, Feb 23, 2009 at 12:57 PM, Timothy Astle <timothy.as...@caris.com> wrote:
I'm following the approach given here for writing unit tests for actions:
http://depressedprogrammer.wordpress.com/2007/06/18/unit-testing-struts-2-actions-spring-junit/
My Problem:
I want to write a unit test that will verify that getSettings() returns back
a JSON string. However, when I run the test, it does not go through the
JSONResult type I have defined. I'm wondering if it has to do with the
BaseStrutsTestCase.createAction? I noticed a snip inside of it that seems
confusing (comment and code do not seem to agree)
<snip>
//do not execute the result after executing the action
proxy.setExecuteResult(true);
</snip>
I would figure if this is set to true, then it would go through my result
and I would be able to get the stream from the Mock Response and check my
JSON string. However, it doesn't seem to be the case.
Below are some code snippets to help paint a picture if more details are
required.
@Test
public void testGetSettings() throws Exception {
Setting s1 = new Setting();
s1.setId(0);
s1.setName("Rocket Man");
s1.setType(SettingType.STRING);
s1.setDefaultValue("She packed my bags last night pre-flight");
s1.setValue("Zero hour nine a.m.");
// Add the setting to a list.
List<Setting> settings = new ArrayList<Setting>(1);
settings.add(s1);
// Set the setting to the mock DAO
MockSettingDAO settingDAO = new MockSettingDAO(settings);
// Set the mock DAO on the service.
SettingServiceImpl settingService = new SettingServiceImpl();
settingService.setSettingDAO(settingDAO);
// Set the service on the action.
SettingAction action = createAction(SettingAction.class,
"/configManager", "settingAction_getSettings", "getSettings");
action.setSettingService(settingService);
// Get the JSON string.
String result = action.getSettings();
// Check the result.
assertEquals(result, Action.SUCCESS);
MockHttpServletResponse response = (MockHttpServletResponse)
ServletActionContext
.getResponse();
String jsonResult = response.getContentAsString();
// Validation of jsonResult would happen here.
}
<snip_of_setting_service>
/**
* Get the settings as a JSON string.
*
* @return SUCCESS
*/
public String getSettings() {
List<Setting> settings = this.settingService.findAllSettings();
ActionContext.getContext().put("jsonResult", settings);
return Action.SUCCESS;
}
</snip_of_setting_service>
<snip_of_struts_xml>
<!--Result Types -->
<result-types>
<result-type name="JSONResult"
class="com.caris.sfe.web.configmanager.results.GenericJSONResult" />
</result-types>
</snip_of_struts_xml>
<snip_of_json_result>
@Override
public void execute(ActionInvocation arg0) throws Exception {
// retrieve your response, request and context objects.
ServletActionContext.getResponse().setContentType("text/plain");
HttpServletResponse response = ServletActionContext.getResponse();
Object jsonResult = ActionContext.getContext().get("jsonResult");
// Create your JSON object using third_party GSON library.
Gson gson = new Gson();
String gsonStr = "{\"Response\" : {\"Results\" : "
+ gson.toJson(jsonResult) + "}}";
// write it out to the outputstream so that JSP can read it through
the
// response object.
OutputStreamWriter outStreamWriter = new OutputStreamWriter(response
.getOutputStream());
outStreamWriter.write(gsonStr);
outStreamWriter.flush();
}
</snip_of_json_result>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org
--
_________________________________________________________
Timothy Astle - Development Manager
Web Development Group
CARIS
115 Waggoners Lane, Fredericton, New Brunswick, Canada, E3B 2L4
Tel: +1-506-458-8533 Fax: +1-506-459-3849
_________________________________________________________________________
Join us for CARIS 2010 in Miami, Florida, United States
Email caris2...@caris.com for details today
_________________________________________________________________________
This email and any files transmitted with it are confidential and intended
only for the addressee(s). If you are not the intended recipient(s) please
notify us by email reply. You should not use, disclose, distribute or copy
this communication if received in error.
Any views or opinions expressed in this email are solely those of the
author and do not necessarily represent those of the company. No binding
contract will result from this email until such time as a written document
is signed on behalf of the company.