Last active
December 8, 2016 14:52
-
-
Save michal-lipski/ce2ab90db9001ce1c4919ffca2b51b41 to your computer and use it in GitHub Desktop.
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.pragmatists; | |
import junitparams.JUnitParamsRunner; | |
import junitparams.Parameters; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import static org.assertj.core.api.Assertions.assertThat; | |
@RunWith(JUnitParamsRunner.class) | |
public abstract class ValueObjectContractTest { | |
protected abstract Object instance(); | |
protected abstract Object[] equalInstances(); | |
protected abstract Object[] nonEqualInstances(); | |
@Test | |
@Parameters(method = "equalInstances") | |
public void shouldBeEqualIfHasSameValues(Object equalInstance) throws Exception { | |
// given: | |
final Object some = instance(); | |
// when: | |
boolean areEqual = some.equals(equalInstance); | |
// then | |
assertThat(areEqual).isTrue(); | |
} | |
@Test | |
@Parameters(method = "nonEqualInstances") | |
public void shouldNotBeEqualIfDifferentValues(Object nonEqual) throws Exception { | |
// given: | |
final Object some = instance(); | |
// when: | |
boolean result = some.equals(nonEqual); | |
// then | |
assertThat(result).isFalse(); | |
} | |
@Test | |
public void shouldNotBeEqualIfDifferentClassPassed() throws Exception { | |
// given: | |
final Object some = instance(); | |
// when: | |
boolean result = some.equals("somethingDifferent"); | |
// then | |
assertThat(result).isFalse(); | |
} | |
@Test | |
public void shouldNotBeEqualIfNullPassed() throws Exception { | |
// given: | |
final Object some = instance(); | |
// when: | |
boolean result = some.equals(null); | |
// then | |
assertThat(result).isFalse(); | |
} | |
@Test | |
@Parameters(method = "equalInstances") | |
public void shouldHaveSameHashCodesIfObjectsEquals(Object equalInstance) throws Exception { | |
// given: | |
final Object some = instance(); | |
// when: | |
boolean areEqual = some.hashCode() == equalInstance.hashCode(); | |
// then | |
assertThat(areEqual).isTrue(); | |
} | |
@Test | |
@Parameters(method = "nonEqualInstances") | |
public void shouldHaveDifferentHashCodesIfNonEqualInstances(Object nonEqualInstance) throws Exception { | |
// given: | |
final Object some = instance(); | |
// when: | |
boolean areEqual = some.hashCode() == nonEqualInstance.hashCode(); | |
// then | |
assertThat(areEqual).isFalse(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment