0

In struts 2 unit tests, the class MockHttpServletResponse is available which I'm expecting, should contain the html output of any given action after it is executed. I'm trying to obtain this output and test it for the presence of a certain string, however I'm getting an empty string returned as the output.

Here's my code:

public class HomeTest extends StrutsJUnit4TestCase<Home>
{    
    private ActionProxy proxy;

    @Before
    public void setUp() throws Exception
    {
        super.setUp();
        proxy = getActionProxy("/home");
    }

    //This test passes fine, the problem is in the next test:
    @Test
    public void testExecute() throws Exception
    {
       String result = proxy.execute();        
       assertEquals("Landing", "landing", result);                
    }

    @Test
    public void testAssets() throws Exception
    {  
        proxy.execute();

        String output = response.getContentAsString();
        System.out.println("output : " + output);

        //The following assertion fails, and in the console, I see an empty
        //String for the output:
        assertTrue( output != null && ! output.isEmpty() );

        String cdnUrl = Config.getCDNUrl();
        assertTrue( output.contains(cdnUrl) );
    }
}

Here's how I configured this action in my struts.xml file:

 <action name="home" class="net.myProj.actions.Home" method="execute">
    <result name="landing">/landing.jsp</result>
 </action>       

If I visit this action normally in my browser, I can see the expected html output just fine. But if I try to run the test, then I can't get the same output using response.getContentAsString(). What am I doing wrong?

Ali
  • 261,656
  • 265
  • 575
  • 769
  • 1
    It isn't supported - it can be with Freemarker or any other non-JEE view technology. To render JSP you must have Servlet Container ;-) – Lukasz Lenart Oct 16 '13 at 07:50
  • @LukaszLenart If you'd like to post an answer describing how I can do it all so I can test the html output of Struts actions in unit tests, I can accept it (and give you a bounty) – Ali Oct 17 '13 at 10:51
  • IMO this is doing it wrong; integration tests should be driving a browser, particularly since almost any current website will also be executing JavaScript--which wouldn't be reflected by simply getting the initial response output. – Dave Newton Oct 17 '13 at 18:18
  • @DaveNewton I'm testing for the precense of certain config values in the html output, and those config values are stored on the server. So this test is correct to be done on the server rather than as a javascript test. – Ali Oct 17 '13 at 18:20
  • Then test your config provider or the component that exposes the config value. If you're trying full rendering, particularly with JSP, you should be testing with a server, and probably with a headless or real browser. – Dave Newton Oct 17 '13 at 20:29
  • There is a JSP plugin you can use. Not sure if you ever tried/looked it? – Kevin Rave Nov 12 '14 at 20:39

0 Answers0