标签: struts2-junit-plugin

Struts 2.3.14 junit测试

我正在使用Struts 2.3.14作为我的应用程序...我需要在我的应用程序中进行一些单元测试...所以我使用的是junit4 ...但是当我ActionProxy在我的测试中使用时,它会显示NullPointerException...

我的测试代码是:

ActionProxy proxy=getActionProxy("/customize/saveRewards");
    SaveRewardsPlus rewards=(SaveRewardsPlus)proxy.getAction();
    request.setParameter("list[0].img_id", "1");        
    request.setParameter("list[0].tag", "Test Tag");
    request.setParameter("list[0].desc", "Test Desc");        
    String result=proxy.execute();        
    assertEquals("success", result);  
Run Code Online (Sandbox Code Playgroud)

我的行动班是:

private List<RewardsBean> list;    

public String execute() {
    try {
        Class.forName(DRIVER);
        con = DriverManager.getConnection(CONSTR, USER, PWD);
        for (RewardsBean rb : list) {
            sql = "update rewards set tag=?,description=? where img_id=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, rb.getTag());
            ps.setString(2, rb.getDesc());
            ps.setInt(3, rb.getImg_id());
            ps.executeUpdate();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return …
Run Code Online (Sandbox Code Playgroud)

java struts2 junit4 struts2-junit-plugin

5
推荐指数
1
解决办法
1977
查看次数

struts2 junit 2.3.12插件 - 无法在struts2 junit4中编写成功的测试

我正在使用struts2 junit 2.3.12插件.

  1. 如果我直接运行测试,那么我得到:

java.lang.NoClassDefFoundError: javax/servlet/ServletContext

所以我包括

<dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

在我的POM中,之后我得到:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException
Run Code Online (Sandbox Code Playgroud)

所以,我删除上面的依赖项并添加:

 <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-6.0</artifactId>
            <version>1.0.0.Final</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

之后我得到了

SEVERE:   [53:22.549] Dispatcher initialization failed
Run Code Online (Sandbox Code Playgroud)

以下是我的测试:

public class TestAction extends StrutsTestCase {

    @Test
    public void testRegister() throws Exception {
        ActionProxy proxy = getActionProxy("/gotoregister");
        assertNotNull(proxy);

        proxy = getActionProxy("/registeraction");
        UserAction action = (UserAction) proxy.getAction();
        assertNotNull(action);
        request.setParameter("usernameexists", "true");
        request.setParameter("emailexists", "false");
        request.setParameter("basicinfo.firstname", "fname");
        request.setParameter("basicinfo.lastname", "lname"); …
Run Code Online (Sandbox Code Playgroud)

struts2 junit4 java-ee maven struts2-junit-plugin

2
推荐指数
1
解决办法
1797
查看次数

Struts2.3.12 junit4 testcase请求和会话为空

我已经完成了这个解决方案: Struts 2 JUnit Plugin v2.2.3:Test Class Extending StrutsTestCase; 'request'为空

但是我已经没有struts2-spring插件,而且我的应用程序中都没有使用spring.

我有3个测试用例,2个请求失败,为null,一个失败,会话为null

第一个Stacktrace

java.lang.NullPointerException
    at org.apache.struts2.interceptor.MessageStoreInterceptor.before(MessageStoreInterceptor.java:228)
    at org.apache.struts2.interceptor.MessageStoreInterceptor.intercept(MessageStoreInterceptor.java:193)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    at actions.TestIoOptimizationAction.login(TestIoOptimizationAction.java:29)
Run Code Online (Sandbox Code Playgroud)

对应于

            Collection actionErrors = (Collection) session.get(actionErrorsSessionKey);
Run Code Online (Sandbox Code Playgroud)

第二和第三测试堆栈跟踪

java.lang.NullPointerException
    at org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
    at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
    at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
    at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
    at org.apache.struts2.StrutsJUnit4TestCase.getActionProxy(StrutsJUnit4TestCase.java:149)
Run Code Online (Sandbox Code Playgroud)

对应于

        String contextPath = ServletActionContext.getRequest().getContextPath();
Run Code Online (Sandbox Code Playgroud)

我相信存在一些依赖性问题.我尝试逐个删除一些依赖项并组合使用,但都没有工作.

这是我的POM的样子:

<dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.paypal.sdk</groupId>
            <artifactId>rest-api-sdk</artifactId>
            <version>0.7.1</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.htmlunit</groupId>
            <artifactId>htmlunit</artifactId>
            <version>2.12</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils-core</artifactId>
            <version>1.8.3</version> …
Run Code Online (Sandbox Code Playgroud)

java spring struts2 maven struts2-junit-plugin

2
推荐指数
1
解决办法
3399
查看次数

标签 统计

struts2 ×3

struts2-junit-plugin ×3

java ×2

junit4 ×2

maven ×2

java-ee ×1

spring ×1