From 684cdc06ae62df99183ca0a6f01c3a8c462b49f5 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 26 Jun 2018 07:10:07 +0100 Subject: [PATCH] New and updated hashcode tests --- src/test/java/net/kemitix/mon/MaybeTest.java | 9 +++++++-- src/test/java/net/kemitix/mon/ResultTest.java | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/test/java/net/kemitix/mon/MaybeTest.java b/src/test/java/net/kemitix/mon/MaybeTest.java index 61941ee..575826a 100644 --- a/src/test/java/net/kemitix/mon/MaybeTest.java +++ b/src/test/java/net/kemitix/mon/MaybeTest.java @@ -51,8 +51,13 @@ public class MaybeTest implements WithAssertions { } @Test - public void testHashCode() { - assertThat(just(1).hashCode()).isEqualTo(Objects.hashCode(1)); + public void justHashCode() { + assertThat(just(1).hashCode()).isNotEqualTo(just(2).hashCode()); + } + + @Test + public void nothingHashCode() { + assertThat(nothing().hashCode()).isEqualTo(maybe(null).hashCode()); } @Test diff --git a/src/test/java/net/kemitix/mon/ResultTest.java b/src/test/java/net/kemitix/mon/ResultTest.java index 0f28501..5e56fca 100644 --- a/src/test/java/net/kemitix/mon/ResultTest.java +++ b/src/test/java/net/kemitix/mon/ResultTest.java @@ -18,6 +18,22 @@ public class ResultTest implements WithAssertions { assertThat(Result.ok(1).equals("1")).isFalse(); } + @Test + public void successHashCode() { + assertThat(Result.ok(1).hashCode()).isNotEqualTo(Result.ok(2).hashCode()); + } + + @Test + public void errorHashCode() { + // despite having 'equivalent' exceptions, the exceptions are distinct instances, so should be considered unique + final RuntimeException exception1 = new RuntimeException("message"); + final RuntimeException exception2 = new RuntimeException("message"); + assertThat(exception1.hashCode()).isNotEqualTo(exception2.hashCode()); + final Result error1 = Result.error(exception1); + final Result error2 = Result.error(exception2); + assertThat(error1.hashCode()).isNotEqualTo(error2.hashCode()); + } + @Test public void createSuccess_isSuccess() { //when