msucil
msucil

Reputation: 846

How to inject mock object in spy object?

I have to test a rest api in spring application which has dependency on service class and it also has dependency on dao class. I have created controller instance with @InjectMocks, service with @Spy and dao with @Mock. In my current scenario, the api that I'm testing calls a method in within spy and the called method calls a method in mock. If I don't use spy then the test doesn't work properly and if used then the instance of dao is not injected into service. In this context what should I need to do inject mocked object of dao into spy object?

Upvotes: 0

Views: 3145

Answers (1)

Anton
Anton

Reputation: 1079

You can use both @Spy and @InjectMocks in service class, but it can work's incorrectly. I think best practice it separate your test. You can write one test for rest api and create mock from service and second test for service.

Upvotes: 1

Related Questions