Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8154119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:14:11+00:00 2026-06-06T16:14:11+00:00

I’m just upgrading to Struts 2.3.1.2, and having a few issues with JUnit tests.

  • 0

I’m just upgrading to Struts 2.3.1.2, and having a few issues with JUnit tests.

My old test code was like this….

public class HomeActionTest  {

    @Test
    public void testUserNameErrorMessage() throws Exception {
     HomeAction action = new HomeAction();
     setupMocks(action);
     action.execute();
    }
}

The action class’s execute method has the code

    String text = getText(GLOBAL_SELECT);

This causes a NullPointerExeception though in LocalizedTextUtil since it calls…

   ActionContext.getContext()

Now I could try and do this…

  ActionContext.setContext(new ActionContext(new HashMap()));

But then I’ll get a NullPointerException since the Valuestack from the context is null.
I could go on and on trying to fix these problems, but it seems like a futile task.
There must be a better way?!?

Well….

I follow the new Struts 2.3.x.x testing doc

My struts.xml is wired with spring.

 <action name="secure/home" class="homeAction" method="execute">
            <result>home.jsp</result>
            <result name="input">home.jsp</result>
        <result name="error">error.jsp</result>
 </action>

My Test class

public class HomeActionTest extends StrutsSpringTestCase   {

    @Test
    public void testUserNameErrorMessage() throws Exception {
        ActionProxy proxy = getActionProxy("/secure/home");

        // setup the session
        Map<String, Object> session = new HashMap<String, Object>();  
        ActionContext actionContext = proxy.getInvocation().getInvocationContext();  
        actionContext.setSession(session);  

        HomeAction homeAction = (HomeAction) proxy.getAction();

        proxy.execute();
    }
}

But this now means I get a fully populated Spring injected Action class!
Most people would saying “YEA!!!” at this point.

My problem is that this isn’t a UNIT test class, its now making calls all the way down to the DB layer since that’s how its wired for run-time.

So here is my question, I know I’ve taken a while to get to it, is it possible to get an Action class that has access to resources it needs (for method calls such as getText()), but is not all Spring wired up?

For the moment I’m tempted to go the refection route to remove all methods that match setServicexxxx on my action, at least then I’d get a NullPointerException on running the test and I can mock out that service, but that’s just wrong. I want my tests to be FAST, not spend 2 seconds starting up the Spring context.

How did Struts2.3 end up with a Base test class that doesn’t follow the mantra of what a Unit test is?

This was all fine in Struts 2.0.9 (xwork-2.0.3.jar).

What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:14:12+00:00Added an answer on June 6, 2026 at 4:14 pm

    You should indeed keep your unit test as fast as possible (otherwise, no TDD). So you should do the minimum struts setup: (I’m using mockito and I have this code in a static block, full code in this gist)

    ActionContext actionContext = mock(ActionContext.class);
    ServletContext servletContext = mock(ServletContext.class);
    when(actionContext.getLocale()).thenReturn(Locale.FRENCH);
    ValueStack valueStack = mock(ValueStack.class);
    Map<String, Object> context = new HashMap<String,Object>();
    Container container = mock(Container.class);
    XWorkConverter conv = mock(XWorkConverter.class);
    when(container.getInstance(XWorkConverter.class)).thenReturn(conv);
    when(conv.convertValue(any(Map.class), any(Object.class), any(Class.class))).thenAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) throws Throwable {
            log.info(invocation.getArguments()[1].toString());
            return "VALUE";
        }
    
    });
    context.put(ActionContext.CONTAINER, container);
    when(valueStack.getContext()).thenReturn(context);
    when(actionContext.getValueStack()).thenReturn(valueStack);
    ServletActionContext.setContext(actionContext);
    ServletActionContext.setServletContext(servletContext);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.