struts2 StrutsSpringJUnit4TestCase вызывает исключение сервлетов - PullRequest
1 голос
/ 15 декабря 2011

Привет, я получаю следующую ошибку при запуске теста junit.

javax.servlet.ServletException: Error code [500], Error: [null]
    at org.apache.struts2.StrutsJUnit4TestCase.executeAction(StrutsJUnit4TestCase.java:131)
    at com.geography.GeographyActionTestCase.testValidator(GeographyActionTestCase.java:36)

Я получаю предупреждение, подобное этому, в моих журналах.

WARN  [org.springframework.mock.web.MockServletContext][main][] Couldn't get resource paths for class path resource [WEB-INF/content/]
java.io.FileNotFoundException: class path resource [WEB-INF/content/] cannot be resolved to URL because it does not exist
    at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:179)
    at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:48)
    at org.springframework.mock.web.MockServletContext.getResourcePaths(MockServletContext.java:238)
    at org.apache.struts2.convention.DefaultResultMapBuilder.createFromResources(DefaultResultMapBuilder.java:254)
    at org.apache.struts2.convention.DefaultResultMapBuilder.build(DefaultResultMapBuilder.java:191)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.createActionConfig(PackageBasedActionConfigBuilder.java:864)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildConfiguration(PackageBasedActionConfigBuilder.java:650)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildActionConfigs(PackageBasedActionConfigBuilder.java:335)
    at org.apache.struts2.convention.ClasspathPackageProvider.loadPackages(ClasspathPackageProvider.java:53)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:215)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:436)
    at org.apache.struts2.util.StrutsTestCaseHelper.initDispatcher(StrutsTestCaseHelper.java:54)
    at org.apache.struts2.StrutsJUnit4TestCase.initDispatcher(StrutsJUnit4TestCase.java:224)
    at org.apache.struts2.StrutsJUnit4TestCase.setUp(StrutsJUnit4TestCase.java:213)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:119)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:101)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
    at $Proxy0.invoke(Unknown Source)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)

и это

WARN  [org.springframework.mock.web.MockServletContext][main][] Couldn't get resource paths for class path resource [WEB-INF/content/]
java.io.FileNotFoundException: class path resource [WEB-INF/content/] cannot be resolved to URL because it does not exist
    at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:179)
    at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:48)
    at org.springframework.mock.web.MockServletContext.getResourcePaths(MockServletContext.java:238)
    at org.apache.struts2.convention.DefaultResultMapBuilder.createFromResources(DefaultResultMapBuilder.java:254)
    at org.apache.struts2.convention.DefaultResultMapBuilder.build(DefaultResultMapBuilder.java:191)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.createActionConfig(PackageBasedActionConfigBuilder.java:864)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildConfiguration(PackageBasedActionConfigBuilder.java:650)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildActionConfigs(PackageBasedActionConfigBuilder.java:335)
    at org.apache.struts2.convention.ClasspathPackageProvider.loadPackages(ClasspathPackageProvider.java:53)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:215)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:436)
    at org.apache.struts2.util.StrutsTestCaseHelper.initDispatcher(StrutsTestCaseHelper.java:54)
    at org.apache.struts2.StrutsJUnit4TestCase.initDispatcher(StrutsJUnit4TestCase.java:224)
    at org.apache.struts2.StrutsJUnit4TestCase.setUp(StrutsJUnit4TestCase.java:213)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:119)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:101)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
    at $Proxy0.invoke(Unknown Source)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)

Я использую

struts 2.3.1 
spring 3.1.0
hibernate 4 CR7

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-test.xml" })
@TransactionConfiguration(transactionManager = "transactionManager")
public class GeographyActionTestCase extends StrutsSpringJUnit4TestCase<GeographyAction> {
    private static final String EXISTING_GEOGRAPHY     = "Cuba";
    private static final String NON_EXISTING_GEOGRAPHY = "Non existing geography";

    private final Logger        LOGGER                 = LoggerFactory.getLogger(this.getClass());

    @Test
    public void testRequiredField() {
        this.request.setParameter("name", "FD");
        final ActionProxy proxy = this.getActionProxy("/tools/geographies/geography");
        Assert.assertNotNull(proxy);

        if (this.LOGGER.isDebugEnabled()) {
            this.LOGGER.debug("action name {}", proxy.getActionName());
            this.LOGGER.debug("action object {}", proxy.getAction().toString());
            this.LOGGER.debug("class type {} ", proxy.getAction().getClass().getName());
        }

        final GeographyAction geographyAction = GeographyAction.class.cast(proxy.getAction());

        final GeographyPageModel pageModel = new GeographyPageModel(5);
        geographyAction.setPageModel(pageModel);
        Assert.assertEquals(geographyAction.execute(), Action.SUCCESS);
        this.executeAction("/tools/geographies/add"); // error happens here. 
}

Вот мой класс GeograpyAction.

@Namespace("/tools/geographies")
@Action("/geography")
@ExceptionMapping(exception = "com.georaphy.GeographyException", result = ".page.geography")
@Result(name = "success", type = "tiles", location = ".page.geography")
public class GeographyAction extends BaseAction implements ParameterAware {

@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 StrutsActionConstant.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;

}

Что не так?Это их способ протестировать отображение аннотаций в struts2 с помощью junit.

...