Assume i have implemented next service class:
@Service
@RequiredArgsConstructor
@Transactional
public class ItemService {
private final ItemRepository itemRepository;
private final ItemCreatorFactory itemCreatorFactory;
private final CommonClient commonClient;
private final HelperService helperService;
private final PrivilegeUtil privilegeUtil;
public void addItem() {
//How to only mock this in tests and other defaults to normal Autowired injection
if(!privilegeUtil.isAdmin("bla bla")) {
Throw new InvalidOperationException("Only super admin can insert data.");
}
helperService.doSomeRealLogic();
commonClient.callAnotherExternalAPI();
itemRepository.save(myObject);
}
// other methods
}
And i need to test insert new item in database, but before that i am doing some extra logic and if its fine so i can insert safely in database.
Now i need to use mockito so i mock only privilegeUtil class but i need all other instances inside ItemService class to be normally Autowired without mocking it.
So i go for next test class:
@RunWith(SpringRunner.class)
public class ItemTests {
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@InjectMocks//Only mocks privilegeUtil but other instances is all null :(
private ItemService itemService;//So how i get autowired item service instance but only mock privilegeUtil instance inside it and all other instances go for normal instantiation ???
@Mock
private PrivilegeUtil privilegeUtil;
@Test
public void testAddNewItem() {
String email = "super@mail.com";
Mockito.when(privilegeUtil.isSuperAdmin(email)).thenReturn(true);
//Here i need to test adding item normally
//But only skip checking isSuperAdmin part inside addItem method
itemService.addItem(itemRequest, email);
}
}
So how i get autowired item service instance but only mock privilegeUtil instance inside it and all other instances go for normal instantiation ???
Is that possible? and if not so what you can suggest?