1. Yes. The LoginAction is annotated with @Namespace("/login"), and
the register() action is annotated with @Action(value="register").

2. I'm not sure what you're saying. Since StrutsTestCase initializes
this to a new object, why do I need to check for null?

On Sat, Mar 12, 2011 at 7:43 PM, Martin Gainty <mgai...@hotmail.com> wrote:
>
> 1)does /login/register exist in your struts webapp?
>
> 2)in StrutsTestCase
>    protected void initServletMockObjects() {
>        request = new MockHttpServletRequest();
> //check request for null before referencing the request object later on
>
> Martin Gainty
> ______________________________________________
> Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
>
> Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger 
> sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung 
> oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich 
> dem Austausch von Informationen und entfaltet keine rechtliche 
> Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen 
> wir keine Haftung fuer den Inhalt uebernehmen.
> Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le 
> destinataire prévu, nous te demandons avec bonté que pour satisfaire informez 
> l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci 
> est interdite. Ce message sert à l'information seulement et n'aura pas 
> n'importe quel effet légalement obligatoire. Étant donné que les email 
> peuvent facilement être sujets à la manipulation, nous ne pouvons accepter 
> aucune responsabilité pour le contenu fourni.
>
>
>
>
>> Date: Sat, 12 Mar 2011 17:16:29 -0600
>> Subject: NullPointerException Using StrutsSpringTestCase
>> From: fergusonja...@gmail.com
>> To: user@struts.apache.org
>>
>> (Sorry, hit the wrong key combination and hit send without meaning to...)
>>
>> I am attempting to do some out-of-container testing of a Struts Action
>> which extends StrutsSpringTestCase. I've tried configuring the
>> application context two separate ways: via the @ContextConfiguration
>> annotation and by overriding getContextLocations(), with no luck.
>>
>> The project is set up on a "standard" Maven2 layout, so the
>> /src/test/resources directory has its own copy of
>> applicationContext.xml. However, on attempting to run the unit test, I
>> get a NullPointerException when my code tries to call
>> getActionProxy(). Here's the Exception text:
>>
>> java.lang.NullPointerException
>>       at 
>> org.apache.struts2.StrutsTestCase.getActionProxy(StrutsTestCase.java:130)
>>       at 
>> org.jason.application.web.actions.struts2.TestLoginAction.testRegister(TestLoginAction.java:72)
>>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>       at 
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>>       at 
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>>       at 
>> org.springframework.test.context.junit4.SpringTestMethod.invoke(SpringTestMethod.java:160)
>>
>> Here's the code:
>>
>> @RunWith(SpringJUnit4ClassRunner.class)
>> @ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
>> public class TestLoginAction extends StrutsSpringTestCase {
>>
>>     private AccountService accountService;  // Mock AccountService
>> object, we'll need it for testing register()
>>
>>     @Before
>>     public void setUp() {
>>
>>         accountService = EasyMock.createMock(AccountService.class);
>>
>>     }
>>
>>     @Override
>>     protected String getContextLocations() {
>>         return "classpath*:applicationContext.xml";
>>     }
>>
>>     @Test
>>     public void testRegister() throws Exception {
>>
>>         // setup the account we expect to be a duplicate
>>         Account duplicateAccount = new Account();
>>         duplicateAccount.setFirstName("Duplicate");
>>         duplicateAccount.setLastName("Account");
>>         duplicateAccount.setOrganization("None");
>>
>>         // setup the account we expect to be good
>>         Account goodAccount = new Account();
>>         duplicateAccount.setFirstName("John");
>>         duplicateAccount.setLastName("Smith");
>>         duplicateAccount.setOrganization("None");
>>
>>         Account goodAccountPopulated = goodAccount;
>>         goodAccount.setId(501);
>>
>>         // setup the mock object
>>         EasyMock.expect(accountService.createAccount(duplicateAccount))
>>                 .andThrow(new DuplicateUsernameException("Account name
>> already exists"));
>>         
>> EasyMock.expect(accountService.createAccount(goodAccount)).andReturn(goodAccountPopulated).anyTimes();
>>
>>         EasyMock.replay();
>>
>>         // get the action
>>         ActionProxy proxy = getActionProxy("/login/register");
>>         LoginAction action = (LoginAction) proxy.getAction();
>>
>>         // inject the mock into the action
>>        action.setAccountService(accountService);
>>
>>         // test for validation errors
>>         request.setParameter("model.lastName", "MyLastName");
>>         request.setParameter("model.organization", "None");
>>         String result0 = proxy.execute();
>>         assertTrue("Problem: result of registration attempt without
>> required fields should have returned INPUT",
>>                 result0.equals("input"));
>>         assertTrue("Problem: field model.firstName not included in
>> fieldErrors but should have been",
>>                 action.getFieldErrors().containsKey("model.firstName"));
>>
>>         // test for duplicate registration
>>         request.setParameter("model.firstName","Duplicate");
>>         request.setParameter("model.lastName", "Username");
>>         request.setParameter("model.organization", "None");
>>
>>         String result1 = proxy.execute();
>>         assertTrue("Problem: result of duplicate registration attempt
>> should have returned INPUT"
>>                     ,result1.equals("input"));  // ensure the error
>> result was returned
>>         assertTrue("Problem: no errors were present in fieldErrors but
>> should have been", action.getFieldErrors().size() > 0);
>>
>>     }
>> }
>>
>> Any ideas out there?
>>
>> Jason
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
>> For additional commands, e-mail: user-h...@struts.apache.org
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to