-
-
Save alvarogarcia7/46917d8d160d46b145de8262e3965670 to your computer and use it in GitHub Desktop.
Testing randomly generated values (pin codes)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import java.util.Objects; | |
public class PinCode { | |
public final String value; | |
public PinCode (final String value) { | |
this.value = value; | |
} | |
@Override | |
public boolean equals (final Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
final PinCode pinCode = (PinCode) o; | |
return Objects.equals(value, pinCode.value); | |
} | |
@Override | |
public int hashCode () { | |
return Objects.hash(value); | |
} | |
@Override | |
public String toString () { | |
final StringBuffer sb = new StringBuffer("PinCode{"); | |
sb.append("value='").append(value).append('\''); | |
sb.append('}'); | |
return sb.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import com.example.Pincode; | |
import java.net.URI; | |
import java.security.SecureRandom; | |
import java.util.Random; | |
public class PinCodeFactory { | |
private Random random; | |
public PinCodeFactory () { | |
random = new SecureRandom(); | |
} | |
public PinCode aNewPinCode () { | |
final String payload = String.format("%06d", random.nextInt(1_000_000)); | |
final PinCode pinCode = new PinCode(payload); | |
return pinCode; | |
} | |
protected void setGenerator (final Random generator) { | |
this.random = generator; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import org.junit.Before; | |
import org.junit.Test; | |
import java.util.Collection; | |
import java.util.HashSet; | |
import java.util.Random; | |
import java.util.function.Consumer; | |
import static org.hamcrest.core.Is.is; | |
import static org.junit.Assert.assertThat; | |
public class PinCodeFactoryTest { | |
private PinCodeFactory pinCodeFactory; | |
@Before | |
public void setUp () { | |
pinCodeFactory = new PinCodeFactory(); | |
pinCodeFactory.setGenerator(new Random(1L)); | |
} | |
@Test | |
public void there_are_no_repeated_with_the_given_seed () { | |
final int desiredProofSize = 100; | |
verifyAll(pinCodes(desiredProofSize), pinCodes -> assertThat(pinCodes.size(), is(desiredProofSize))); | |
} | |
@Test | |
public void the_numbers_are_left_padded_with_zeros () { | |
verifyEachOf(pinCodes(100), pincode -> assertThat(pincode.length(), is(6))); | |
} | |
@Test | |
public void the_numbers_do_not_contain_spaces () { | |
verifyEachOf(pinCodes(100), pincode -> assertThat(pincode.contains(" "), is(false))); | |
} | |
public void verifyEachOf (final Collection<String> pincodes, final Consumer<String> assertion) { | |
for (final String pincode : pincodes) { | |
assertion.accept(pincode); | |
} | |
} | |
public void verifyAll (final Collection<String> pincodes, final Consumer<Collection<String>> assertion) { | |
assertion.accept(pincodes); | |
} | |
private Set<String> pinCodes (final int desiredProofSize) { | |
return Stream.generate(() -> pinCodeFactory.aNewPinCode().value) | |
.limit(desiredProofSize) | |
.collect(Collectors.toSet()); | |
} | |
} |
Maybe the test would be stronger if it matched a regex pattern instead of just checking for length and the existence of spaces?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool trick, @pierre-jean :) 👍