これはプロジェクトのセットアップです
TestController.java
@Component
public class TestController {
public String getDefaultMessage() {
return "Some default msg";
}
}
TestControllerTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-servlet.xml")
public class TestControllerTest {
TestController controller;
@Test
public void itShouldTest(){
String msg = controller.getDefaultMessage();
assertThat(msg, is("Some default msg"));
}
}
問題 1 : spring-servlet.xml の正しい場所
/WEB-INF/spring-servlet.xml とすると、ファイルが見つかりませんと表示されます。プロジェクト プロパティ > 展開アセンブリを選択すると、/WebContent の展開パスが / であることがわかります。
「classpath:/WEB-INF/spring-servlet.xml」、「classpath:spring-servlet.xml」、「classpath:/spring-servlet.xml」も試しました
問題 2 : spring-servlet.xml を test.edu.amty.testdemo.controllers にコピーすると、それは見つかりますが、JUnit ウィンドウに次のエラーが表示されます。
java.lang.NoClassDefFoundError: javax/servlet/ServletContext
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getDeclaredMethods(Unknown Source)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.buildLifecycleMetadata(InitDestroyAnnotationBeanPostProcessor.java:196)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.findLifecycleMetadata(InitDestroyAnnotationBeanPostProcessor.java:178)
at ...
コンソールでフォロー
09:15:14,288 DEBUG SpringJUnit4ClassRunner:106 - SpringJUnit4ClassRunner constructor called with [class test.edu.amty.testdemo.controllers.TestControllerTest].
09:15:14,309 DEBUG TestContext:97 - Retrieved @ContextConfiguration [@org.springframework.test.context.ContextConfiguration(classes=[], locations=[], loader=interface org.springframework.test.context.ContextLoader, value=[spring-servlet.xml], inheritLocations=true)] for class [class test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,321 DEBUG ContextLoaderUtils:130 - Processing ContextLoader for @ContextConfiguration [@org.springframework.test.context.ContextConfiguration(classes=[], locations=[], loader=interface org.springframework.test.context.ContextLoader, value=[spring-servlet.xml], inheritLocations=true)] and declaring class [class test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,322 DEBUG ContextLoaderUtils:150 - Using default ContextLoader class [org.springframework.test.context.support.DelegatingSmartContextLoader] for test class [class test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,372 DEBUG ContextLoaderUtils:196 - Retrieved @ContextConfiguration [@org.springframework.test.context.ContextConfiguration(classes=[], locations=[], loader=interface org.springframework.test.context.ContextLoader, value=[spring-servlet.xml], inheritLocations=true)] for declaring class [class test.edu.amty.testdemo.controllers.TestControllerTest].
09:15:14,391 DEBUG ContextLoaderUtils:203 - Resolved context configuration attributes: [ContextConfigurationAttributes@4ca42b declaringClass = 'test.edu.amty.testdemo.controllers.TestControllerTest', locations = '{spring-servlet.xml}', classes = '{}', inheritLocations = true, contextLoaderClass = 'org.springframework.test.context.ContextLoader']
09:15:14,392 DEBUG ContextLoaderUtils:305 - Processing locations and classes for context configuration attributes [[ContextConfigurationAttributes@4ca42b declaringClass = 'test.edu.amty.testdemo.controllers.TestControllerTest', locations = '{spring-servlet.xml}', classes = '{}', inheritLocations = true, contextLoaderClass = 'org.springframework.test.context.ContextLoader']]
09:15:14,393 DEBUG DelegatingSmartContextLoader:75 - Delegating to GenericXmlContextLoader to process context configuration [ContextConfigurationAttributes@4ca42b declaringClass = 'test.edu.amty.testdemo.controllers.TestControllerTest', locations = '{spring-servlet.xml}', classes = '{}', inheritLocations = true, contextLoaderClass = 'org.springframework.test.context.ContextLoader'].
09:15:14,404 DEBUG ContextLoaderUtils:236 - Could not find an 'annotation declaring class' for annotation type [interface org.springframework.test.context.ActiveProfiles] and class [class test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,409 INFO TestContextManager:185 - @TestExecutionListeners is not present for class [class test.edu.amty.testdemo.controllers.TestControllerTest]: using defaults.
09:15:14,441 DEBUG TestContextManager:137 - Registering TestExecutionListener: org.springframework.test.context.support.DependencyInjectionTestExecutionListener@b80017
09:15:14,441 DEBUG TestContextManager:137 - Registering TestExecutionListener: org.springframework.test.context.support.DirtiesContextTestExecutionListener@239525
09:15:14,442 DEBUG TestContextManager:137 - Registering TestExecutionListener: org.springframework.test.context.transaction.TransactionalTestExecutionListener@497536
09:15:14,457 DEBUG ProfileValueUtils:68 - Retrieved @ProfileValueSourceConfiguration [null] for test class [test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,458 DEBUG ProfileValueUtils:80 - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,465 DEBUG ProfileValueUtils:68 - Retrieved @ProfileValueSourceConfiguration [null] for test class [test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,466 DEBUG ProfileValueUtils:80 - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,474 DEBUG ProfileValueUtils:68 - Retrieved @ProfileValueSourceConfiguration [null] for test class [test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,475 DEBUG ProfileValueUtils:80 - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,478 DEBUG ProfileValueUtils:68 - Retrieved @ProfileValueSourceConfiguration [null] for test class [test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,478 DEBUG ProfileValueUtils:80 - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,480 DEBUG ProfileValueUtils:68 - Retrieved @ProfileValueSourceConfiguration [null] for test class [test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,483 DEBUG ProfileValueUtils:80 - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,487 DEBUG TestContextManager:282 - beforeTestClass(): class [class test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,489 DEBUG ProfileValueUtils:68 - Retrieved @ProfileValueSourceConfiguration [null] for test class [test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,489 DEBUG ProfileValueUtils:80 - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:14,494 DEBUG TestContextManager:315 - prepareTestInstance(): instance [test.edu.amty.testdemo.controllers.TestControllerTest@1ab6c1c]
09:15:14,495 DEBUG DependencyInjectionTestExecutionListener:73 - Performing dependency injection for test context [[TestContext@dd7786 testClass = TestControllerTest, testInstance = test.edu.amty.testdemo.controllers.TestControllerTest@1ab6c1c, testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1d23e75 testClass = TestControllerTest, locations = '{classpath:/test/edu/amty/testdemo/controllers/spring-servlet.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']]].
09:15:14,496 DEBUG DelegatingSmartContextLoader:226 - Delegating to GenericXmlContextLoader to load context from [MergedContextConfiguration@1d23e75 testClass = TestControllerTest, locations = '{classpath:/test/edu/amty/testdemo/controllers/spring-servlet.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader'].
09:15:14,497 DEBUG AbstractGenericContextLoader:93 - Loading ApplicationContext for merged context configuration [[MergedContextConfiguration@1d23e75 testClass = TestControllerTest, locations = '{classpath:/test/edu/amty/testdemo/controllers/spring-servlet.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']].
09:15:14,633 DEBUG StandardEnvironment:114 - Initializing new StandardEnvironment
09:15:14,636 DEBUG StandardEnvironment:103 - Adding [systemProperties] PropertySource with lowest search precedence
09:15:14,640 DEBUG StandardEnvironment:103 - Adding [systemEnvironment] PropertySource with lowest search precedence
09:15:14,642 DEBUG StandardEnvironment:120 - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
09:15:14,808 DEBUG StandardEnvironment:114 - Initializing new StandardEnvironment
09:15:14,809 DEBUG StandardEnvironment:103 - Adding [systemProperties] PropertySource with lowest search precedence
09:15:14,810 DEBUG StandardEnvironment:103 - Adding [systemEnvironment] PropertySource with lowest search precedence
09:15:14,811 DEBUG StandardEnvironment:120 - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
09:15:14,830 INFO XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [test/edu/amty/testdemo/controllers/spring-servlet.xml]
09:15:14,881 DEBUG DefaultDocumentLoader:72 - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
09:15:14,965 DEBUG PluggableSchemaResolver:105 - Trying to resolve XML entity with public id [null] and system id [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd]
09:15:14,967 DEBUG PluggableSchemaResolver:140 - Loading schema mappings from [META-INF/spring.schemas]
09:15:14,976 DEBUG PluggableSchemaResolver:146 - Loaded schema mappings: {http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd=org/springframework/oxm/config/spring-oxm-3.0.xsd, ...
09:15:14,980 DEBUG PluggableSchemaResolver:118 - Found XML schema [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-3.0.xsd
09:15:15,094 DEBUG PluggableSchemaResolver:105 - Trying to resolve XML entity with public id [null] and system id [http://www.springframework.org/schema/context/spring-context-3.0.xsd]
09:15:15,096 DEBUG PluggableSchemaResolver:118 - Found XML schema [http://www.springframework.org/schema/context/spring-context-3.0.xsd] in classpath: org/springframework/context/config/spring-context-3.0.xsd
09:15:15,107 DEBUG PluggableSchemaResolver:105 - Trying to resolve XML entity with public id [null] and system id [http://www.springframework.org/schema/tool/spring-tool-3.0.xsd]
09:15:15,109 DEBUG PluggableSchemaResolver:118 - Found XML schema [http://www.springframework.org/schema/tool/spring-tool-3.0.xsd] in classpath: org/springframework/beans/factory/xml/spring-tool-3.0.xsd
09:15:15,134 DEBUG DefaultBeanDefinitionDocumentReader:108 - Loading bean definitions
09:15:15,173 DEBUG DefaultNamespaceHandlerResolver:156 - Loaded NamespaceHandler mappings: {http://www.springframework.org/schema/p=org.springframework.beans.factory.xml.SimplePropertyNamespaceHandler, http://www.springframework.org/schema/mvc=org.springframework.web.servlet.config.MvcNamespaceHandler, http://www.springframework.org/schema/util=org.springframework.beans.factory.xml.UtilNamespaceHandler, http://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler, http://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler, http://www.springframework.org/schema/oxm=org.springframework.oxm.config.OxmNamespaceHandler, http://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler, http://www.springframework.org/schema/c=org.springframework.beans.factory.xml.SimpleConstructorNamespaceHandler, http://www.springframework.org/schema/tx=org.springframework.transaction.config.TxNamespaceHandler, http://www.springframework.org/schema/jms=org.springframework.jms.config.JmsNamespaceHandler, http://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler, http://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler, http://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler}
09:15:15,226 DEBUG PathMatchingResourcePatternResolver:550 - Looking for matching resources in directory tree [I:\Running\workspaces\projectSetup\TestDemo\build\classes\main\edu\amty\testdemo]
09:15:15,227 DEBUG PathMatchingResourcePatternResolver:612 - Searching directory [I:\Running\workspaces\projectSetup\TestDemo\build\classes\main\edu\amty\testdemo] for files matching pattern [I:/Running/workspaces/projectSetup/TestDemo/build/classes/main/edu/amty/testdemo/**/*.class]
09:15:15,233 DEBUG PathMatchingResourcePatternResolver:612 - Searching directory [I:\Running\workspaces\projectSetup\TestDemo\build\classes\main\edu\amty\testdemo\controllers] for files matching pattern [I:/Running/workspaces/projectSetup/TestDemo/build/classes/main/edu/amty/testdemo/**/*.class]
09:15:15,239 DEBUG PathMatchingResourcePatternResolver:351 - Resolved location pattern [classpath*:main/edu/amty/testdemo/**/*.class] to resources [file [I:\Running\workspaces\projectSetup\TestDemo\build\classes\main\edu\amty\testdemo\controllers\TestController.class]]
09:15:15,240 DEBUG ClassPathBeanDefinitionScanner:233 - Scanning file [I:\Running\workspaces\projectSetup\TestDemo\build\classes\main\edu\amty\testdemo\controllers\TestController.class]
09:15:15,310 DEBUG ClassPathBeanDefinitionScanner:244 - Identified candidate component class: file [I:\Running\workspaces\projectSetup\TestDemo\build\classes\main\edu\amty\testdemo\controllers\TestController.class]
09:15:15,350 DEBUG XmlBeanDefinitionReader:216 - Loaded 6 bean definitions from location pattern [classpath:/test/edu/amty/testdemo/controllers/spring-servlet.xml]
09:15:15,354 INFO GenericApplicationContext:495 - Refreshing org.springframework.context.support.GenericApplicationContext@ae7b77: startup date [Thu Feb 28 09:15:15 IST 2013]; root of context hierarchy
09:15:15,355 DEBUG GenericApplicationContext:525 - Bean factory for org.springframework.context.support.GenericApplicationContext@ae7b77: org.springframework.beans.factory.support.DefaultListableBeanFactory@a4871e: defining beans [testController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,viewResolver]; root of factory hierarchy
09:15:15,386 DEBUG DefaultListableBeanFactory:217 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
09:15:15,387 DEBUG DefaultListableBeanFactory:430 - Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
09:15:15,437 DEBUG DefaultListableBeanFactory:504 - Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
09:15:15,440 DEBUG DefaultListableBeanFactory:458 - Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
09:15:15,491 DEBUG DefaultListableBeanFactory:217 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
09:15:15,492 DEBUG DefaultListableBeanFactory:430 - Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
09:15:15,494 DEBUG DefaultListableBeanFactory:504 - Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references
09:15:15,495 DEBUG DefaultListableBeanFactory:458 - Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
09:15:15,495 DEBUG DefaultListableBeanFactory:217 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
09:15:15,496 DEBUG DefaultListableBeanFactory:430 - Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
09:15:15,497 DEBUG DefaultListableBeanFactory:504 - Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references
09:15:15,498 DEBUG DefaultListableBeanFactory:458 - Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
09:15:15,498 DEBUG DefaultListableBeanFactory:217 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
09:15:15,499 DEBUG DefaultListableBeanFactory:430 - Creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
09:15:15,508 DEBUG DefaultListableBeanFactory:504 - Eagerly caching bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' to allow for resolving potential circular references
09:15:15,509 DEBUG DefaultListableBeanFactory:458 - Finished creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
09:15:15,510 DEBUG DefaultListableBeanFactory:217 - Creating shared instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0'
09:15:15,510 DEBUG DefaultListableBeanFactory:430 - Creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0'
09:15:15,511 DEBUG DefaultListableBeanFactory:504 - Eagerly caching bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0' to allow for resolving potential circular references
09:15:15,511 DEBUG DefaultListableBeanFactory:458 - Finished creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0'
09:15:15,517 DEBUG GenericApplicationContext:794 - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@c7eb9d]
09:15:15,523 DEBUG GenericApplicationContext:818 - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@1f68572]
09:15:15,525 INFO DefaultListableBeanFactory:557 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@a4871e: defining beans [testController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,viewResolver,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
09:15:15,526 DEBUG DefaultListableBeanFactory:217 - Creating shared instance of singleton bean 'testController'
09:15:15,527 DEBUG DefaultListableBeanFactory:430 - Creating instance of bean 'testController'
09:15:15,534 DEBUG DefaultListableBeanFactory:504 - Eagerly caching bean 'testController' to allow for resolving potential circular references
09:15:15,539 DEBUG CachedIntrospectionResults:222 - Getting BeanInfo for class [main.edu.amty.testdemo.controllers.TestController]
09:15:15,550 DEBUG CachedIntrospectionResults:238 - Caching PropertyDescriptors for class [main.edu.amty.testdemo.controllers.TestController]
09:15:15,557 DEBUG CachedIntrospectionResults:250 - Found bean property 'class' of type [java.lang.Class]
09:15:15,559 DEBUG CachedIntrospectionResults:250 - Found bean property 'defaultMessage' of type [java.lang.String]
09:15:15,568 DEBUG DefaultListableBeanFactory:458 - Finished creating instance of bean 'testController'
09:15:15,569 DEBUG DefaultListableBeanFactory:245 - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
09:15:15,570 DEBUG DefaultListableBeanFactory:245 - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
09:15:15,570 DEBUG DefaultListableBeanFactory:245 - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
09:15:15,571 DEBUG DefaultListableBeanFactory:245 - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
09:15:15,571 DEBUG DefaultListableBeanFactory:217 - Creating shared instance of singleton bean 'viewResolver'
09:15:15,572 DEBUG DefaultListableBeanFactory:430 - Creating instance of bean 'viewResolver'
09:15:15,590 DEBUG TestContextManager:439 - afterTestClass(): class [class test.edu.amty.testdemo.controllers.TestControllerTest]
09:15:15,591 DEBUG DirtiesContextTestExecutionListener:113 - After test class: context [[TestContext@dd7786 testClass = TestControllerTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1d23e75 testClass = TestControllerTest, locations = '{classpath:/test/edu/amty/testdemo/controllers/spring-servlet.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']]], dirtiesContext [false].
*Mavenやその他の構築ツールを使用せずに依存関係を手動で追加しています