Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Struts 2单元测试中获得动作的HTML输出?_Java_Unit Testing_Struts2_Struts2 Junit Plugin - Fatal编程技术网

Java 如何在Struts 2单元测试中获得动作的HTML输出?

Java 如何在Struts 2单元测试中获得动作的HTML输出?,java,unit-testing,struts2,struts2-junit-plugin,Java,Unit Testing,Struts2,Struts2 Junit Plugin,在struts 2单元测试中,类MockHttpServletResponse是可用的,我希望它在执行后应该包含任何给定操作的html输出。我试图获取这个输出并测试它是否存在某个字符串,但是我得到了一个作为输出返回的空字符串 这是我的密码: public class HomeTest extends StrutsJUnit4TestCase<Home> { private ActionProxy proxy; @Before public void

在struts 2单元测试中,类
MockHttpServletResponse
是可用的,我希望它在执行后应该包含任何给定操作的html输出。我试图获取这个输出并测试它是否存在某个字符串,但是我得到了一个作为输出返回的空字符串

这是我的密码:

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) );
    }
}
公共类HomeTest扩展了StrutsJUnit4TestCase
{    
私人代理;
@以前
public void setUp()引发异常
{
super.setUp();
proxy=getActionProxy(“/home”);
}
//此测试通过得很好,问题在下一次测试中:
@试验
public void testExecute()引发异常
{
字符串结果=proxy.execute();
资产质量(“着陆”,“着陆”,结果);
}
@试验
public void testAssets()引发异常
{  
proxy.execute();
字符串输出=response.getContentAsString();
System.out.println(“输出:”+输出);
//以下断言失败,在控制台中,我看到一个空的
//输出的字符串:
assertTrue(output!=null&&!output.isEmpty());
字符串cdnUrl=Config.getCDNUrl();
assertTrue(output.contains(cdnUrl));
}
}
下面是我如何在struts.xml文件中配置此操作的:

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

/landing.jsp

如果我在浏览器中正常访问此操作,我可以很好地看到预期的html输出。但是如果我尝试运行测试,那么使用
response.getContentAsString()
无法获得相同的输出。我做错了什么

它不受支持-可以与Freemarker或任何其他非JEE view技术一起使用。要呈现JSP,必须具有Servlet容器;-)@LukaszLenart如果你想发布一个答案,描述我是如何做到这一切的,这样我就可以在单元测试中测试Struts操作的html输出,我可以接受它(并给你一个奖励),我认为这是做错了;集成测试应该驱动浏览器,特别是因为几乎所有当前网站都将执行JavaScript——这不会通过简单地获取初始响应输出来反映出来。@DaveNewton我正在测试html输出中某些配置值的顺序,这些配置值存储在服务器上。因此,在服务器上而不是作为javascript测试进行此测试是正确的。然后测试配置提供程序或公开配置值的组件。如果您正在尝试完全呈现,特别是使用JSP,那么您应该使用服务器进行测试,可能还需要使用无头或真实浏览器。