1

私はユニットテストのためにspring mvc testを使用したいと思いますが、次のエラーが発生しました。春のMVCモックエラー

Java.lang.NoSuchMethodError: org.springframework.beans.factory.support.RootBeanDefinition.setTargetType(Lorg/springframework/core/ResolvableType;)V

at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.createBeanDefinition(MockitoPostProcessor.java:208) 
    at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.registerMock(MockitoPostProcessor.java:187) 
    at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.register(MockitoPostProcessor.java:178) 
    at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.postProcessBeanFactory(MockitoPostProcessor.java:148) 
    at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.postProcessBeanFactory(MockitoPostProcessor.java:135) 
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284) 
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:174) 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) 
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) 
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:111) 
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) 
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) 
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) 
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189) 
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131) 
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 


Process finished with exit code 255 
@RunWith(SpringJUnit4ClassRunner.class) 
@SpringBootTest(classes = WebAPIController.class) 
@AutoConfigureMockMvc 
public class WebAPIControllerTest { 

    @Autowired 
    private WebApplicationContext ctx; 

    private MockMvc mockMvc; 

    @MockBean(classes = NotifyTaskService.class) 
    private NotifyTaskService notifyTaskService; 

    @Autowired 
    private WebAPIController webAPIController; 

    @Before 
    public void before() throws Exception { 
     this.mockMvc = MockMvcBuilders.webAppContextSetup(this.ctx).build(); 
    } 

    @After 
    public void after() throws Exception { 
    } 

    /** 
    * Method: sendNotifyAPI(String systemKey, String user, String type, String mechanism, String defaultMethod, String msgContent) 
    */ 
    @Test 
    public void testSendNotifyAPI() throws Exception { 

    } 
+0

'Java.lang.NoSuchMethodError'は依存関係の問題のように聞こえ、依存関係を投稿する可能性があります。 – Jaiwo99

+0

私はpom.xmlをチェックしましたが、まだ問題があります。 –

答えて

0

Beanにモックオブジェクトを注入する方法を別の質問、。

public class WebAPIController { 
     @Autowired 
    private HttpServletRequest request; 
    @Autowired 
    private NotifyTaskService notifyTaskService; 
    ....... 
} 

public class WebAPIControllerTest { 
    @Mock 
    private NotifyTaskService notifyTaskService; 

    @Autowired 
    private WebAPIController webAPIController; 
.......... 
} 

「webAPIController」に「notifyTaskService」を挿入する方法はありますか?

+0

あなたは答えで質問しないでください:)。あなたは@MockBean注釈を使う必要があります。Springは自動的に虚偽のbeanを挿入します。あなたは元の質問でこれを行いました。正確な問題は何ですか? –

0

あなたは春・ブート・スターターテスト、次のガイドを通過使用する場合: Testing Web Layer with Spring Boot Test

を私は@WebMvcTestを使用する最後の例を好む、ここであなたは@MockBeanを使用してNotifyTaskServiceを模擬することができます。