1 package org.junit.tests;
2
3 import static org.hamcrest.CoreMatchers.is;
4 import static org.junit.Assert.assertThat;
5 import static org.junit.Assume.assumeNotNull;
6 import static org.junit.Assume.assumeThat;
7
8 import java.lang.reflect.Method;
9
10 import org.junit.Test;
11 import org.junit.Test.None;
12 import org.junit.experimental.theories.DataPoints;
13 import org.junit.experimental.theories.Theories;
14 import org.junit.experimental.theories.Theory;
15 import org.junit.runner.RunWith;
16 import org.junit.runners.model.FrameworkMethod;
17
18 @RunWith(Theories.class)
19 public class ObjectContractTest {
20 @DataPoints
21 public static Object[] objects = {new FrameworkMethod(toStringMethod()),
22 new FrameworkMethod(toStringMethod()), 3, null};
23
24 @Theory
25 @Test(expected = None.class)
26 public void equalsThrowsNoException(Object a, Object b) {
27 assumeNotNull(a);
28 a.equals(b);
29 }
30
31 @Theory
32 public void equalsMeansEqualHashCodes(Object a, Object b) {
33 assumeNotNull(a, b);
34 assumeThat(a, is(b));
35 assertThat(a.hashCode(), is(b.hashCode()));
36 }
37
38 private static Method toStringMethod() {
39 try {
40 return Object.class.getMethod("toString");
41 } catch (SecurityException e) {
42 } catch (NoSuchMethodException e) {
43 }
44 return null;
45 }
46 }