###前提・実現したいこと
お世話になります
Eclipse Spring Mavenプロジェクトを
JUnitでテストする際にエラーが出ます。
トレースを確認したところ、
「java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.clearCache()V」
というエラーで、使用しているSpring Coreが重複などしているとなるようです。
昨日"Failed to load ApplicationContext"のエラーを解消するために
ビルド・パスのspring-coreを3.2.3から4.1.6に差し替えました。
こちらはpom.xmlの設定などで直せるものでしょうか?
どうぞよろしくお願いいたします。
###発生している問題・エラーメッセージ
java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.clearCache()V at org.springframework.context.support.AbstractApplicationContext.resetCommonCaches(AbstractApplicationContext.java:879) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:563) at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:128) at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:60) at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100) at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248) at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64) at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91) at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122) at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105) at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) 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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
###該当のソースコード
xml
1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>example</groupId> 5 <artifactId>yes</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>yes Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <dependencyManagement> 11 <dependencies> 12 <dependency> 13 <groupId>io.spring.platform</groupId> 14 <artifactId>platform-bom</artifactId> 15 <version>2.0.5.RELEASE</version> 16 <type>pom</type> 17 <scope>import</scope> 18 </dependency> 19 </dependencies> 20 </dependencyManagement> 21 <dependencies> 22 <dependency> 23 <groupId>javax.servlet</groupId> 24 <artifactId>javax.servlet-api</artifactId> 25 <scope>provided</scope> 26 </dependency> 27 <dependency> 28 <groupId>org.apache.taglibs</groupId> 29 <artifactId>taglibs-standard-jstlel</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework</groupId> 33 <artifactId>spring-webmvc</artifactId> 34 </dependency> 35 <dependency> 36 <groupId>org.hibernate</groupId> 37 <artifactId>hibernate-validator</artifactId> 38 </dependency> 39 <dependency> 40 <groupId>org.slf4j</groupId> 41 <artifactId>jcl-over-slf4j</artifactId> 42 </dependency> 43 <dependency> 44 <groupId>ch.qos.logback</groupId> 45 <artifactId>logback-classic</artifactId> 46 </dependency> 47 <dependency> 48 <groupId>junit</groupId> 49 <artifactId>junit</artifactId> 50 <scope>test</scope> 51 </dependency> 52 </dependencies> 53 54 <build> 55 <finalName>yes</finalName> 56 <pluginManagement> 57 <plugins> 58 <plugin> 59 <artifactId>maven-compiler-plugin</artifactId> 60 <configuration> 61 <source>1.8</source> 62 <target>1.8</target> 63 </configuration> 64 </plugin> 65 </plugins> 66 </pluginManagement> 67 68 </build> 69</project> 70
コントローラー
java
1package example.app; 2 3import org.springframework.stereotype.Controller; 4import org.springframework.ui.Model; 5import org.springframework.web.bind.annotation.RequestMapping; 6import org.springframework.web.bind.annotation.RequestMethod; 7 8@Controller 9@RequestMapping("echo") 10public class EchoController { 11 12 @RequestMapping(method = RequestMethod.GET) 13 public String viewInput(Model model) { 14 EchoForm form = new EchoForm(); 15 model.addAttribute(form); 16 return "echo/input"; 17 18 } 19 20 @RequestMapping(method = RequestMethod.POST) 21 public String echo(EchoForm form) { 22 return "echo/output"; 23 24 } 25}
テスト
java
1package example.apptest; 2 3 4 5import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 6import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 7 8import org.junit.Before; 9import org.junit.Test; 10import org.junit.runner.RunWith; 11import org.mockito.InjectMocks; 12import org.springframework.beans.factory.annotation.Autowired; 13import org.springframework.test.context.ContextConfiguration; 14import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 15import org.springframework.test.context.web.WebAppConfiguration; 16import org.springframework.test.web.servlet.MockMvc; 17import org.springframework.test.web.servlet.setup.MockMvcBuilders; 18import org.springframework.web.context.WebApplicationContext; 19 20import example.app.EchoController; 21 22@RunWith(SpringJUnit4ClassRunner.class) 23@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/ApplicationContext.xml" }) 24@WebAppConfiguration 25public class EchoControllerTest { 26 27 @Autowired 28 WebApplicationContext context; 29 MockMvc mockMvc; 30 31 @InjectMocks 32 EchoController controller; 33 34 @Before 35 public void setupMockMvc() { 36 this.mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); 37 } 38 @Test 39 public void testEchoController() throws Exception { 40 // EchoController uri = new EchoController(); 41 // String actualInputUri = uri.viewInput(null); 42 mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(forwardedUrl("/WEB-INF/echo/input.jsp")); 43 } 44 45}
ApplicationContext.xml
xml
1<?xml version="1.0" encoding="UTF-8"?> 2<beans:beans xmlns="http://www.springframework.org/schema/mvc" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 8 9 <annotation-driven /> 10 11 <resources mapping="/resources/**" location="/resources/" /> 12 13 <beans:bean 14 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 15 <beans:property name="prefix" value="/WEB-INF/echo/" /> 16 <beans:property name="suffix" value=".jsp" /> 17 </beans:bean> 18 19 <beans:bean id="appProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 20 <beans:property name="ignoreResourceNotFound" value="true"/> 21 <beans:property name="locations" value="classpath:app.properties"/> 22 </beans:bean> 23 24 <context:component-scan base-package="example.app" /> 25 26</beans:beans> 27
回答1件
あなたの回答
tips
プレビュー