Struts2单元测试

  • ​​1.官方文档​​
  • ​​2.给出的测试模块​​
  • ​​3.遇到的问题​​
  • ​​3.1 编译问题​​
  • ​​3.2单独一个模块进行测试,运行高效​​
  • ​​3.5.必须引入插件​​


1.官方文档

【六袆 - Java】2022年 Struts2单元测试;struts2官方案例;_java_02

2.给出的测试模块

  • 模块名称:unit-testing
package org.apache.struts.register.action;


import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.StrutsTestCase;
import org.junit.Test;

public class RegisterTest extends StrutsTestCase {

@Test
public void testExecuteValidationPasses() throws Exception {

request.setParameter("personBean.firstName", "Bruce");

request.setParameter("personBean.lastName", "Phillips");

request.setParameter("personBean.email", "bphillips@ku.edu");

request.setParameter("personBean.age", "19");

ActionProxy actionProxy = getActionProxy("/register.action") ;

Register action = (Register) actionProxy.getAction();

assertNotNull("The action is null but should not be.", action);

String result = actionProxy.execute();

assertEquals("The execute method did not return " + ActionSupport.SUCCESS + " but should have.", ActionSupport.SUCCESS, result);

}

}
  • successful
2022-10-26 00:07:14,231 INFO  [main] ognl.OgnlUtil (OgnlUtil.java:274) - OGNL Expression Max Length enabled with 256.
2022-10-26 00:07:14,258 WARN [main] ognl.OgnlUtil (OgnlUtil.java:845) - Working in devMode, using devMode excluded classes and packages!

Process finished with exit code 0

3.遇到的问题

3.1 编译问题

<build>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<doclint>none</doclint>
</configuration>
</plugin>
</plugins>
  • 官方案例使用的是jdk11版本进行编译的,按需修改

3.2单独一个模块进行测试,运行高效

  • 在父工程的pom.xml中,需要测试哪个模块,就打开,如图

【六袆 - Java】2022年 Struts2单元测试;struts2官方案例;_maven_03



【六袆 - Java】2022年 Struts2单元测试;struts2官方案例;_apache_04

3.5.必须引入插件

<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-junit-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>2.3.31</version>
<scope>test</scope>
</dependency>