简体   繁体   中英

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.

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; BService bService; are.

@mock will look all the fields to be presented(mocked)

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;

....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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