Skip to content

Instantly share code, notes, and snippets.

@michal-lipski
Last active December 8, 2016 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michal-lipski/ce2ab90db9001ce1c4919ffca2b51b41 to your computer and use it in GitHub Desktop.
Save michal-lipski/ce2ab90db9001ce1c4919ffca2b51b41 to your computer and use it in GitHub Desktop.
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