Milad Badran
Milad Badran

Reputation: 21

Wanted but not invoked, Mockito with repository example

I'm using JUnit5 + Mockito. The repository is responsible for maintaining the key and the secret. I'm getting "Actually, there were zero interactions with this mock." error when running the test. The repository is null when calling getSecret method even after calling storeKey method with asserts. I don't know what problem is.

public class KeySecretRepository {
    private final Map<Object, String> keyRepository = new ConcurrentHashMap<>();
    
    public void save(Object key, String secret) {
        keyRepository.put(key, secret);
    }

    public String getSecret(Object key) {
        if(keyRepository.containsKey(key)){
            return keyRepository.get(key);
        }
        throw new BadRequestException("Key not found");
    }
}

public class KeyStore {
    private static final int MAX_KEY_LENGTH = 20;

    private final KeySecretRepository keySecretRepository;

    public KeyStore(KeySecretRepository keySecretRepository) {
        this.keySecretRepository = keySecretRepository;
    }

    public Optional<String> storeKeySecret(String key, String secret) {

        validate(key);
        if (key.length() > MAX_KEY_LENGTH) {
            throw new BadRequestException("key length should be less or equal 20");
        }
        keySecretRepository.save(key.trim().toUpperCase(), secret);
        Optional<String> optionalKey = Optional.ofNullable(key).filter(s -> !s.isEmpty());
        return optionalKey;
    }

    public Optional<Integer> storeKeySecret(Integer key, String secret) {
        if (key == null) {
            throw new BadRequestException("key is null!..");
        }
        keySecretRepository.save(key, secret);
        return Optional.of(key);
    }

    public String getSecret(String key) {
        validate(key);
        return keySecretRepository.getSecret(key);
    }

    public String getSecret(Integer key) { // key is caseSensitive
        if (key == null) {
            throw new BadRequestException("key is null!..");
        }
        return keySecretRepository.getSecret(key);
    }

    public void validate(String key) {
        if (key == null || key.equals("")) {
            throw new BadRequestException("key cannot be empty or null");
        }
    }
}

@ExtendWith(MockitoExtension.class)
@RunWith(JUnitPlatform.class)
public class KeyStoreTest {

    KeyStore keyStore;

    @Mock
    KeySecretRepository keySecretRepository;

    @BeforeEach
    public void init() {
        keyStore = new KeyStore(keySecretRepository);
    }

    @Test
    public void testGetSecret() {
    
        Mockito.verify(keySecretRepository).save(Mockito.eq("mY-key2"), Mockito.eq("test2"));

        assertEquals("test2", keyStore.getSecret("mY-key2"));
    }
}

Upvotes: 0

Views: 860

Answers (1)

GJohannes
GJohannes

Reputation: 1763

Your test method "testGetSecret" does not call any method on keyStore. Therefore there is no interaction with "keySecretRepository".

@Test
public void testGetSecret() {

    keyStore.storeKeySecret("mY-key2","test2")
    Mockito.verify(keySecretRepository).save(Mockito.eq("mY-key2"), Mockito.eq("test2"));
    assertEquals("test2", keyStore.getSecret("mY-key2"));
}

Upvotes: 1

Related Questions