I have a class where one attribute is getting value from outer repo.
public class Demo() {
private final Repository repository;
private final long attr;
public Demo(Repository repository) {
this.repository = repository;
this.attr = this.getValue();
}
private Long getValue() {
return repository.getValue();
}
... Rest of the code
}
Now I want to write an unit test for it.
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class DemoTest() {
@Mock
private Repository repository;
@InjectMocks
private Demo demo;
@BeforeEach
void setUp() {
registrationUtil = new RegistrationUtil();
MockitoAnnotations.initMocks(this);
}
When I run this snippet, it shows a null pointer exception in repository.getValue() May I know how can I mock the value properly to avoid exception?