From 07bdac4b29a91c344eaf6b9c80b812ecf8a372a1 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 15 Jun 2018 12:55:48 +0100 Subject: [PATCH] Refactoring test to use random string supplier --- .../net/kemitix/gitdb/test/GitDBTest.java | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java index dec963d..adfe4de 100644 --- a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java @@ -16,14 +16,17 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.io.*; import java.nio.file.*; import java.util.Optional; +import java.util.UUID; +import java.util.function.Supplier; import static org.assertj.core.api.Assumptions.assumeThat; @ExtendWith(MockitoExtension.class) class GitDBTest implements WithAssertions { - private String userName = "user name"; - private String userEmailAddress = "user@email.com"; + private final Supplier stringSupplier = UUID.randomUUID()::toString; + private String userName = stringSupplier.get(); + private String userEmailAddress = stringSupplier.get(); // When initialising a repo in a dir that doesn't exist then a bare repo is created @Test @@ -242,12 +245,14 @@ class GitDBTest implements WithAssertions { @Test void getValue_whenExists_thenReturnValueInOptional() throws IOException, ClassNotFoundException { //given + final String key = stringSupplier.get(); + final String value = stringSupplier.get(); final GitDBBranch originalBranch = gitDBBranch(); - final GitDBBranch updatedBranch = originalBranch.put("key-name", "value"); + final GitDBBranch updatedBranch = originalBranch.put(key, value); //when - final Optional result = updatedBranch.get("key-name"); + final Optional result = updatedBranch.get(key); //then - assertThat(result).contains("value"); + assertThat(result).contains(value); } // When removing a key that does not exist then the GitDbBranch is returned @@ -265,33 +270,43 @@ class GitDBTest implements WithAssertions { @Test void removeKey_whenExists_thenReturnUpdatedBranch() throws IOException { //given - final GitDBBranch originalBranch = gitDBBranch().put("key-name", "value"); + final String key = stringSupplier.get(); + final String value = stringSupplier.get(); + final GitDBBranch originalBranch = gitDBBranchWithKeyValue(key, value); //when - final GitDBBranch updatedBranch = originalBranch.remove("key-name"); + final GitDBBranch updatedBranch = originalBranch.remove(key); //then assertThat(updatedBranch).isNotSameAs(originalBranch); } + private GitDBBranch gitDBBranchWithKeyValue(final String key, final String value) throws IOException { + return gitDBBranch().put(key, value); + } + // When removing a key that does exist then original GitDbBranch can still find it @Test void removeKey_whenExists_thenOriginalCanStillFind() throws IOException { //given - final GitDBBranch originalBranch = gitDBBranch().put("key-name", "value"); + final String key = stringSupplier.get(); + final String value = stringSupplier.get(); + final GitDBBranch originalBranch = gitDBBranchWithKeyValue(key, value); //when - final GitDBBranch updatedBranch = originalBranch.remove("key-name"); + final GitDBBranch updatedBranch = originalBranch.remove(key); //then - assertThat(originalBranch.get("key-name")).contains("value"); + assertThat(originalBranch.get(key)).contains(value); } // When removing a key that does exist then the updated GitDbBranch can't find it @Test void removeKey_whenExists_thenUpdatedCanNotFind() throws IOException { //given - final GitDBBranch originalBranch = gitDBBranch().put("key-name", "value"); + final String key = stringSupplier.get(); + final String value = stringSupplier.get(); + final GitDBBranch originalBranch = gitDBBranchWithKeyValue(key, value); //when - final GitDBBranch updatedBranch = originalBranch.remove("key-name"); + final GitDBBranch updatedBranch = originalBranch.remove(key); //then - assertThat(updatedBranch.get("key-name")).isEmpty(); + assertThat(updatedBranch.get(key)).isEmpty(); } // When starting a named transaction then GitDbTransaction is returned