I have a Java service which has few things injected by Guice.
public class RecoveryServiceImpl implements RecoveryService {
@Inject
public RecoveryServiceImpl(SessionInstanceCache sessionInstanceCache, AttendeeCache attendeeCache, MessagingStreamConfiguration messagingConfig) {
this.sessionInstanceCache = sessionInstanceCache;
this.attendeeCache = attendeeCache;
this.messagingConfig = messagingConfig;
}
@Override
public SessionInstanceData recoverSessionInstance(SessionInstanceDto sessionInstance) {
SessionInstanceData sessionInstanceData = SessionInstanceHelper.recoverSessionInstance(sessionInstance);
if (sessionInstanceData.getDeepstreamServerKey() == null) {
String dsKey = SessionInstanceHelper.pickRandomDeepstreamServerKey(
sessionInstanceData, messagingConfig);
And this does:
public static String pickRandomDeepstreamServerKey(
SessionInstanceData sessionInstanceData, MessagingStreamConfiguration dsConfig) {
// NPE occurs here
List<String> dsKeys = new ArrayList(dsConfig.getBaseUrls().keySet());
This is tested with Spock.
I added the messagingConfig and now I am struggling making it mocked in a Spock test:
class RecoveryServiceImplTest extends Specification {
...
MessagingStreamConfiguration msgConfig = Mock(MessagingStreamConfiguration);
RecoveryService recoveryService = new RecoveryServiceImpl(sessionInstanceCache, attendeeCache, msgConfig);
def "Recover session instance"() {
...
def dsMap = new HashMap<String, URL>();
dsMap.put("ds1", new URL("http://ilovemocking.com/"));
when:
msgConfig.getBaseUrls() >> dsMap;
//msgConfig.getBaseUrls().keySet() >> new HashSet(){{add("ds1")}};
recoveryService.recoverSessionInstance(sessionInstanceDto)
In the call to recoverSessionInstance(), I get a NPE because getBaseUrls() returns null.
I have tried other way, instantiating the msgConfig as a normal object (AKA "stubbing"), but same result.
How should I make Spock Mock to return the dsMap instead of null?
SessionInstanceHelper.recoverSessionInstance()to see which expression throws NPE? - Alexander TerekhovmsgConfig.getBaseUrls() >> dsMapfromwhenblock tothen: 1 * msgConfig.getBaseUrls() >> dsMap- OpalMessagingStreamConfiguration msgConfig = Mock(MessagingStreamConfiguration) { getBaseUrls() >> dsMap }butdsMapmust be declared before the mock is defined. - Opal