I have the following Spring Service class that I'm trying to test with Mockito:
@Service
public class ObjectExportService {
@Autowired
protected List<SecuredService<? extends SecuredObject>> securedServices;
public void doStuff() {
for(int i = 0; i < this.securedServices.size(); i++){
SecuredService<? extends SecuredObject> securedSrv = this.securedServices.get(i);
//this access works
}
for (SecuredService<? extends SecuredObject> securedSrv : this.securedServices) { //this access does not work
}
}
}
This is my Test class for that service:
@RunWith(MockitoJUnitRunner.class)
public class ObjectExportServiceTest {
@InjectMocks
private ObjectExportService objectExportService;
@Mock
protected List<SecuredService<? extends SecuredObject>> securedServices;
@Test
public void testDoStuff(){
objectExportService.doStuff();
Assert.assertTrue(true);
}
}
When I run the test, I get a NullpointerException, but only in the for-each loop.
First I assumed is a similar problem as described in this thread:
I have Mocked the List and would therefore need to mock the iterator()
call.
The solutions provided in that thread didn't work for me, because I am actually autowiring a List.