I writing a unit test for one of my validators which use an Integer type as an @Autowired
dependency.
Now, while writing test I am not able to @Mock
it as Spring says -
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class java.lang.Integer
Mockito cannot mock/spy following:
- final classes
- anonymous classes
- primitive types
How should I get around it?
My validator looks like this-
public class MyValidator {
@Autowired
@Qualifier(value = "intervalInDays")
private Integer intervalInDays; // does not get mocked!!
@Autowired
private LanguageValidator languageValidator;
@Autowired
private IdValidator idValidator;
public void validate(Request request) {
//does some validations
}
}
The test for this validator class looks like this-
@RunWith(MockitoJUnitRunner.class)
public class MyValidatorTest {
@InjectMocks
private MyValidator myValidator; //isnt mocked at all
@Mock
private LanguageValidator languageValidator; //works fine
@Mock
private IdValidator idValidator; //works fine
@Mock(name = "intervalInDays")
private Integer intervalInDays;
//some tests here
}
Please suggest a solution as the intervalInDays
fails to mock citing the reason stated above for Mockito.