Struts2单元测试注释验证器。

时间:2011-02-19 17:31:40

标签: junit struts2 annotations validation

您好我有以下页面模型类。

public class CarModel extends PageModel {
    private static final long serialVersionUID = 4766706536002862335L;

    private String            name;
    private String            description;


    @RequiredStringValidator(type = ValidatorType.FIELD, key = "errors.required.name", trim = true)
    public String getName() {
        return this.name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    @RequiredStringValidator(type = ValidatorType.FIELD, key = "errors.required.description", trim = true)
    public String getDescription() {
        return this.description;
    }

    public void setDescription(final String description) {
        this.description = description;
    }
}

我有一个动作类。

@Component(value = "CarAction")
@Namespace(value = "/car")
@Action(value = "/car")
@ExceptionMapping(exception = "com.cars.AutomobileException", result = ".page.car")
@Result(name = "success", type = "tiles", location = ".page.car")
public class CarAction extends BaseAction implements ParameterAware {
   private CarModel pageModel;

   @Action(value = "/add", results = @Result(name = "addResult", params = { "actionName",
            "addResult" }, type = "chain"))
    @InputConfig(methodName = "execute")
    @Validations(visitorFields = { @VisitorFieldValidator(shortCircuit = true, message = "Their are errors to be corrected", fieldName = "pageModel") })
    public String add() {
        this.validate();
        return SUCCESS;
    }
}

这是我的基本行动。

@InterceptorRefs({
        @InterceptorRef(value = "createSession"),
        @InterceptorRef(value = "tokenSession", params = { "includeMethods", "add,addResult" }),
        @InterceptorRef(value = "exception"),
        @InterceptorRef(value = "alias"),
        @InterceptorRef(value = "i18n"),
        @InterceptorRef(value = "chain"),
        @InterceptorRef(value = "timer"),
        @InterceptorRef(value = "logger"),
        @InterceptorRef(value = "checkbox"),
        @InterceptorRef(value = "staticParams"),
        @InterceptorRef(value = "actionMappingParams"),
        @InterceptorRef(value = "params", params = { "excludeParams", "dojo\\..*,^struts\\..*" }),
        @InterceptorRef(value = "conversionError"),
        @InterceptorRef(value = "validation", params = { "excludeMethods", "cancel,execute,reset,next,previous,updateItemPerPage,goToPage,browse", "validateAnnotatedMethodOnly", "true" }),
        @InterceptorRef(value = "annotationWorkflow", params = { "excludeMethods", "input,back,cancel,browse" }),
        @InterceptorRef(value = "execAndWait") })
@ExceptionMappings({
        @ExceptionMapping(exception = "org.springframework.dao.DataAccessException", result = "..page.data.error"),
        @ExceptionMapping(result = ".page.servlet.error", exception = "javax.servlet.ServletException"),
        @ExceptionMapping(result = ".page.runtime.error", exception = "java.lang.NullPointerException"),
        @ExceptionMapping(result = ".page.runtime.error", exception = "java.lang.outOfMemory"),
        @ExceptionMapping(result = ".page.runtime.error", exception = "java.lang.ArrayIndexOutOfBoundsException"),
        @ExceptionMapping(result = ".page.runtime.error", exception = "java.lang.ArithmeticException") })
@Results({
        @Result(name = "invalid.token", location = ".page.runtime.error", type = "tiles"),
        @Result(name = "wait", location = ".page.wait", type = "tiles") })
@ParentPackage("tiles-default")
public abstract class BaseAction extends ActionSupport {
    private static final long serialVersionUID = -5898754579799304669L;

}

在我的junit测试中

this.request.setParameter("name", "FD");
final ActionProxy proxy = this.getActionProxy("/car/car");
Assert.assertNotNull(proxy);

final CarAction carAction = CarAction.class.cast(proxy.getAction());

final CarModel pageModel = new CarModel(5);

carAction.setPageModel(pageModel);
Assert.assertEquals(carAction.execute(), Action.SUCCESS);

-- this is where it fails. 
final Map<String, List<String>> fieldErrors = carAction.getFieldErrors();
Assert.assertFalse(fieldErrors.isEmpty());

有人知道如何在junit测试中启动注释验证吗?

由于

0 个答案:

没有答案