'Implement logic from JUnit tests

I lost some Java code. I keep only several JUnit tests for hashing. I tried to implement this:

public interface HmacService {
    String hash(final String number);

    String getCheckhash(final String hash);
}
...
class HmacServiceImpl implements HmacService {

    public String hash(String number) {
        try {
            return Hashing.sha256().hashString(plainCardNumber, StandardCharsets.UTF_8).toString();
        } catch (Exception var3) {            
        }
    }

    public String getNonCreditCardSha1Hmac(String hash) {
        try {
          return Hashing.sha1().hashString(hash, StandardCharsets.UTF_8).toString();
        } catch (Exception var3) {            
        }
    }


   final class HashPair{
        String firstHash;
        String secondHash;

        public HashPair(String firstHash, String secondHash) {
            this.firstHash = firstHash;
            this.secondHash = secondHash;
        }
        
        // perform hash comparison here
    }
}

JUnit tests:

public HmacService.HashPair generateQuickHash(final String cardNum) {
    return cryptoService.generateHashPair(cardNum);
}

if(new HmacService.HashPair(oldHash, oldHash)) == null){
// some error message
}

Do you know how I can implement the code based on the JUnit tests?



Solution 1:[1]

This is testing HMAC-SHA-256 and there are countless Java implementations of it. The strange thing just is, that no signature is being passed - neither when hashing, nor when comparing the hashes. I don't think that the code has any real purpose alike this.

Solution 2:[2]

Depends on how strong your coverage of that part of the code was.

If it had high coverage, you can follow a TDD like approach doing Red-Green-Refactor cycles.

To start I would comment all failing tests but one (the simplest one) [first red] an then do the implementation to make it pass [first green].

Then uncomment the next test and implement again.

Rinse and repeat until all test are uncommented and passing.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Zeitler
Solution 2 Gonzalo Matheu