dimanche 8 novembre 2020

Spring in unit test cannot find a tertiary file and context cannot be taken

The test starts at context definition. It finds the xml file, starts to look for components in it, finds CacheFilter class that implements javax.servlet.Filter and fails not finding the last. (at normal, not test launching, everything if found OK).

If I exclude from scanning all classes based on that Filter, the scanning itself fails at that point, not finding the same Filter class.

It seems, the class javax.servlet.Filter is invisible for ContextConfiguration, either in xml or as a reference from another class. Why?


The test class:

package amcssz.spr.srv.main.jobs;

import amcssz.spr.srv.main.service.U01UhradaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

@Test
@ContextConfiguration(locations = { "classpath:META-INF/spring/spr-main.xml" })

public class OdeslatADostatJakobyAutomatickyUhradu extends AbstractTestNGSpringContextTests {
    @Autowired( required = true )
    U01UhradaService service;
    ...

The xml config:

<!-- <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:trx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:sec="http://www.springframework.org/schema/security" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:ctx="http://www.springframework.org/schema/context" xmlns:jms="http://www.springframework.org/schema/jms"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.4.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
">

    <!-- @Component, @Autowired,... -->
    <ctx:annotation-config />
    

    <!-- Components scan -->
    <ctx:component-scan base-package="test" />

    <ctx:component-scan base-package="amcssz.spr" >
        <ctx:exclude-filter type="assignable" expression="javax,servlet.Filter"/>
        <ctx:exclude-filter type="assignable" expression="amcssz.spr.srv.main.service.rules.TlacitkaRulesServant"/>
    </ctx:component-scan>

The result is:

TST-[2020/11/08 12:55:16.833] xml.XmlBeanDefinitionReader Loading XML bean definitions from class path resource [META-INF/spring/spr-main.xml]
TST-[2020/11/08 12:55:17.109] annotation.ClassPathBeanDefinitionScanner JSR-330 'javax.inject.Named' annotation found and supported for component scanning
TST-[2020/11/08 12:55:17.122] annotation.ClassPathBeanDefinitionScanner JSR-330 'javax.inject.Named' annotation found and supported for component scanning
TST-[2020/11/08 12:55:22.713] context.TestContextManager Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@18682406] to prepare test instance [amcssz.spr.srv.main.jobs.OdeslatADostatJakobyAutomatickyUhradu@2c79809]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:133)
    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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)
    at org.testng.SuiteRunner.run(SuiteRunner.java:254)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [C:\Users\gangn_000\IdeaProjects\CSSZ_SPR\apv\share-web\target\classes\amcssz\spr\web\share\filter\CacheFilter.class]; nested exception is java.io.FileNotFoundException: class path resource [javax/servlet/Filter.class] cannot be opened because it does not exist

If I try to exclude all files based on Filter, by

<ctx:component-scan base-package="amcssz.spr" >
    <ctx:exclude-filter type="assignable" expression="javax,servlet.Filter"/>
    <ctx:exclude-filter type="assignable" expression="amcssz.spr.srv.main.service.rules.TlacitkaRulesServant"/>
</ctx:component-scan>

again that class cannot be found:

TST-[2020/11/08 13:02:56.842] xml.XmlBeanDefinitionReader Loading XML bean definitions from class path resource [META-INF/spring/spr-main.xml]
TST-[2020/11/08 13:02:57.110] annotation.ClassPathBeanDefinitionScanner JSR-330 'javax.inject.Named' annotation found and supported for component scanning
TST-[2020/11/08 13:02:57.123] annotation.ClassPathBeanDefinitionScanner JSR-330 'javax.inject.Named' annotation found and supported for component scanning
TST-[2020/11/08 13:02:57.131] context.TestContextManager Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@3caa4b] to prepare test instance [amcssz.spr.srv.main.jobs.OdeslatADostatJakobyAutomatickyUhradu@2c79809]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:133)
    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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)
    at org.testng.SuiteRunner.run(SuiteRunner.java:254)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Type filter class not found: javax.servlet.Filter; nested exception is java.lang.ClassNotFoundException: javax.servlet.Filter
Offending resource: class path resource [META-INF/spring/spr-main.xml]; nested exception is java.lang.ClassNotFoundException: javax.servlet.Filter

Aucun commentaire:

Enregistrer un commentaire