简体   繁体   English

在基于Mockito的junit测试类中注入嵌套的依赖项

[英]Inject nested dependencies in mockito based junit test class

I am writing Unit test for one controller and here is my code 我正在为一个控制器编写单元测试,这是我的代码

public class MyController
{
    @Inject
    private MyService myService;

    public List<Car> getCars()
    {
        myService.getCars();
    }
}

public class MyServiceImpl implements MyService 
{
    @Inject
    AService aService;

    @Inject
    BService bService;

    public List<Car> getCars()
    {
        aService.getCars();
    }
}


Public class MyControllerTest
{

    private MockMvc standAloneMockMvc;

    @InjectMocks
    MyController myController;

    @Mock
    private MyService myService;

    @Before
    public void initTestObjs() 
    {
        MockitoAnnotations.initMocks(this);

        this.standAloneMockMvc = MockMvcBuilders.standaloneSetup(myController).build();

    }

    @Test
    public void testGetAllCars() throws Exception
    {
        String url = "/car/list";

        List<Car> listCars = new ArrayList<Car>();
        Car car = new Car();
        listCars.add(car);

        Mockito.when(myService.getCars()).thenReturn(listCars);

        MvcResult result = standAloneMockMvc.perform(MockMvcRequestBuilders.get(url))
        .andDo(MockMvcResultHandlers.print())
        .andExpect(MockMvcResultMatchers.status().isOk())
        .andReturn();

        String jsonResult = result.getResponse().getContentAsString();
    }
}

I am facing error in creating bean for myService in MyControllerTest when it tries to load aService and bService. 当尝试加载aService和bService时,在MyControllerTest中为myService创建bean时遇到错误。

Can anyone help out on this? 有人可以帮忙吗? Anyone else faced similar issues? 还有其他人遇到类似的问题吗?

Stacktrace: 堆栈跟踪:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xyz.AService com.xyz.aService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xyz.AService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}  
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:571)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)

You need to provide exactly all the mock implementation, ie. 您需要完全提供所有模拟实现,即。 Your test class is unable to figure out what these AService aService; 您的测试类无法弄清楚这些AService和aService是什么; BService bService; BService bService; are. 是。

@mock will look all the fields to be presented(mocked) @mock将查找要显示的所有字段(模拟)

So, you can provide mock provide mock implementation for them 因此,您可以为他们提供模拟提供模拟执行

.. ..

private MockMvc standAloneMockMvc;

    @InjectMocks
    MyController myController;

    @Mock
    private MyService myService;

    @Mock
    AService aService;

    @Mock
    BService bService;

.... ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM