MockMvc - 配置错误:org.springframework.beans.factory.UnsatisfiedDependencyException

时间:2018-02-02 08:29:08

标签: spring junit mockmvc

我已经经历了很多解决方案,但我无法弄清楚问题是什么,我也是MockMvc的新手所以我可能会遗漏一些基本的东西,但不知道是什么。

问题: 我有现有的应用程序我正在尝试使用MockMvc JUnit进行集成测试,我有以下结构,

@RestController
public class MyController{
    @Autowired
    private MyService myService;

//requestMappingMethods..

}

public interface MyService{
    public List<LocationMstrModel> getData();
}

@Service
public class MyServiceImpl implements MyService{
    @Autowired
    LocationMstrDAO locationMstrDAO;
//calling DAO methods and ding bussiness operations..
}

public interface MyDAO{
public List<LocationMstrModel> getData();
}

@Repository
public class MyDAO{
@Override
public List<LocationMstrModel> getData(){
}
}

下面是我写的测试类以测试上面的流程。

//Required imports..
RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath*:testContext.xml",
    "classpath*:dispatcher-servlet.xml"

})
@WebAppConfiguration
@EnableWebMvc
public class LocationServiceImplTest {

    private MockMvc mockMvc;


    @Autowired
    MyService myService;


    @Autowired
    WebApplicationContext wContext;


    @Before
    public void before() {

        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wContext).build();
    }

    @Test
    public void test() {
        /*fail("Not yet implemented");*/
    }

    @Test
    public void checkinsanceOfMyService() throws Exception{
        MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/public/location");
        ResultActions result = mockMvc.perform(request);
        result.andDo(print())
        .andExpect(status().isOk());

    }
}

testContext.xml

<bean id="myService" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="com.service.MyService"/>
</bean>

任何帮助都很受欢迎..

编辑:
错误如瓷砖中所述, 这里有完整的staack跟踪,

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.test.LocationServiceImplTest': Unsatisfied dependency expressed through field 'locationService';
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [com.fpl.techCafe.service.LocationService]:
 expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    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.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)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [com.fpl.techCafe.service.LocationService]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1463)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1094)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
    ... 28 more

0 个答案:

没有答案