Is there any way to inject non-mocked objects with @InjectMocks
?
My Setup has a UserSignupService
with dependencies to a MailService
and a UserRepository
(a Spring Data Repository). I've a unit test creating a spy of the MailService
and I annotated the UserSignupService
with @InjectMocks
. Sadly this won't inject the UserRepository
(non-mocked) into the service class.
Combining @InjectMocks
with @Autowired
won't inject the mocked MailService
, but the bean from the application context.
MockitoAnnotations.initMocks()
is run in AbstractServiceTest.setUp()
. This class also holds the configuration of the the unit test (SpringJunit4TestRunner
, etc.)
public class UserSignupServiceTest extends AbstractServiceTest {
@Autowired @Spy
private MailService<UserActivationMail> mailService;
@InjectMocks
private UserSignupServiceImpl service;
@Before
public void setUp() throws Exception {
super.setUp();
}
}
@Service
public class UserSignupServiceImpl implements UserSignupService {
private final UserRepository repository;
private final MailService<UserActivationMail> mailService;
@Autowired
public UserSignupServiceImpl(UserRepository repository,
MailService<UserActivationMail> mailService) {
this.repository = repository;
this.mailService = mailService;
}
//methods...
}