summaryrefslogtreecommitdiffstats
path: root/junit4/src/test/java/org/junit/tests/experimental/theories/runner/UnsuccessfulWithDataPointFields.java
blob: 790d798d00799a2cda1421401b14ea98916cd5e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package org.junit.tests.experimental.theories.runner;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.failureCountIs;
import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;
import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;
import static org.junit.matchers.JUnitMatchers.both;
import org.junit.Test;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import org.junit.runners.model.TestClass;

public class UnsuccessfulWithDataPointFields {
	@RunWith(Theories.class)
	public static class HasATheory {
		@DataPoint
		public static int ONE= 1;

		@Theory
		public void everythingIsZero(int x) {
			assertThat(x, is(0));
		}
	}

	@Test
	public void theoryClassMethodsShowUp() throws Exception {
		assertThat(new Theories(HasATheory.class).getDescription()
				.getChildren().size(), is(1));
	}

	@Test
	public void theoryAnnotationsAreRetained() throws Exception {
		assertThat(new TestClass(HasATheory.class).getAnnotatedMethods(
				Theory.class).size(), is(1));
	}

	@Test
	public void canRunTheories() throws Exception {
		assertThat(testResult(HasATheory.class),
				hasSingleFailureContaining("Expected"));
	}

	@RunWith(Theories.class)
	public static class DoesntUseParams {
		@DataPoint
		public static int ONE= 1;

		@Theory
		public void everythingIsZero(int x, int y) {
			assertThat(2, is(3));
		}
	}

	@Test
	public void reportBadParams() throws Exception {
		assertThat(testResult(DoesntUseParams.class),
				hasSingleFailureContaining("everythingIsZero(ONE, ONE)"));
	}

	@RunWith(Theories.class)
	public static class NullsOK {
		@DataPoint
		public static String NULL= null;

		@DataPoint
		public static String A= "A";

		@Theory
		public void everythingIsA(String a) {
			assertThat(a, is("A"));
		}
	}

	@Test
	public void nullsUsedUnlessProhibited() throws Exception {
		assertThat(testResult(NullsOK.class),
				hasSingleFailureContaining("null"));
	}

	@RunWith(Theories.class)
	public static class DataPointsMustBeStatic {
		@DataPoint
		int THREE= 3;

		@DataPoint
		int FOUR= 3;

		@Theory
		public void numbers(int x) {

		}
	}

	@Test
	public void dataPointsMustBeStatic() {
		assertThat(
				testResult(DataPointsMustBeStatic.class),
				both(failureCountIs(2))
						.and(
								hasFailureContaining("DataPoint field THREE must be static"))
						.and(
								hasFailureContaining("DataPoint field FOUR must be static")));
	}

	@RunWith(Theories.class)
	public static class TheoriesMustBePublic {
		@DataPoint
		public static int THREE= 3;

		@Theory
		void numbers(int x) {

		}
	}

	@Test
	public void theoriesMustBePublic() {
		assertThat(
				testResult(TheoriesMustBePublic.class),
				hasSingleFailureContaining("public"));
	}
}