i want to unit test my validation defined with annotation. but it should be light. without loading any configuration files. i’m rather new to struts2 so i’m not sure if it is even possible. i have MyAction with method:
@Validations(...)
public String submit() {...}
and what i want is something like:
@Test
public void submitTest() {
MyAction myAction = createInitializedMyAction(); //new MyAction, setXXX
Map<String, String> httpRequestParams = prepareHttpRequestParams();
TestHelperINeed testHelper = createTestHelper(myAction); // maybe some proxy???
testHelper.execute("submit", httpRequestParams);
assertErrors(testHelper.getErrors());
assertResult(testHelper.getResult());
every solution i found requires configuration files of struts and spring. why do i need them if all the code i want to test is located in a single java file?
i found a solution there: http://bloodredsun.com/2009/10/21/unit-testing-struts2-actions-with-annotation-based-validation/
it requires small refactoring to make it more useful but it works. at least with Struts 2.1.6. i also added some changes to make it works as my production struts:
builder.constant("devMode", "false"); builder.constant("objectFactory.classloader", ActionAnnotationValidatorFactory.class.getClassLoader().getClass()); Container container = builder.create(true); ValueStackFactory ognlValueStackFactory = container.getInstance(ValueStackFactory.class); ((OgnlValueStackFactory) ognlValueStackFactory).setAllowStaticMethodAccess("true");it requires small changes to work with struts 2.1.8.1. before calling
validatei do something like this: