Skip to content

Commit 61a8cbe

Browse files
Switch to PrivateConstructorChecker for "testNotInstantiable" tests
1 parent 2e44d56 commit 61a8cbe

File tree

6 files changed

+422
-61
lines changed

6 files changed

+422
-61
lines changed

src/test/java/rx/functions/ActionsTest.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
import static org.junit.Assert.*;
1919

20-
import java.lang.reflect.*;
2120
import java.util.Arrays;
2221
import java.util.concurrent.atomic.AtomicLong;
2322

2423
import org.junit.Test;
24+
import rx.util.PrivateConstructorChecker;
2525

2626
public class ActionsTest {
2727

@@ -269,22 +269,13 @@ public void call(Object... args) {
269269
assertEquals(i, value.get());
270270
}
271271
}
272-
272+
273273
@Test
274274
public void testNotInstantiable() {
275-
try {
276-
Constructor<?> c = Actions.class.getDeclaredConstructor();
277-
c.setAccessible(true);
278-
Object instance = c.newInstance();
279-
fail("Could instantiate Actions! " + instance);
280-
} catch (NoSuchMethodException ex) {
281-
ex.printStackTrace();
282-
} catch (InvocationTargetException ex) {
283-
ex.printStackTrace();
284-
} catch (InstantiationException ex) {
285-
ex.printStackTrace();
286-
} catch (IllegalAccessException ex) {
287-
ex.printStackTrace();
288-
}
275+
PrivateConstructorChecker
276+
.forClass(Actions.class)
277+
.expectedTypeOfException(IllegalStateException.class)
278+
.expectedExceptionMessage("No instances!")
279+
.check();
289280
}
290281
}

src/test/java/rx/functions/FunctionsTest.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,20 @@
1717

1818
import static org.junit.Assert.*;
1919

20-
import java.lang.reflect.*;
2120
import java.util.Arrays;
2221
import java.util.concurrent.atomic.AtomicLong;
2322

2423
import org.junit.Test;
24+
import rx.util.PrivateConstructorChecker;
2525

2626
public class FunctionsTest {
2727
@Test
2828
public void testNotInstantiable() {
29-
try {
30-
Constructor<?> c = Functions.class.getDeclaredConstructor();
31-
c.setAccessible(true);
32-
Object instance = c.newInstance();
33-
fail("Could instantiate Actions! " + instance);
34-
} catch (NoSuchMethodException ex) {
35-
ex.printStackTrace();
36-
} catch (InvocationTargetException ex) {
37-
ex.printStackTrace();
38-
} catch (InstantiationException ex) {
39-
ex.printStackTrace();
40-
} catch (IllegalAccessException ex) {
41-
ex.printStackTrace();
42-
}
29+
PrivateConstructorChecker
30+
.forClass(Functions.class)
31+
.expectedTypeOfException(IllegalStateException.class)
32+
.expectedExceptionMessage("No instances!")
33+
.check();
4334
}
4435

4536
@Test(expected = RuntimeException.class)

src/test/java/rx/observers/ObserversTest.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,22 @@
1818
import static org.junit.Assert.*;
1919
import static org.mockito.Mockito.*;
2020

21-
import java.lang.reflect.*;
2221
import java.util.concurrent.atomic.*;
2322

2423
import org.junit.Test;
2524

2625
import rx.exceptions.*;
2726
import rx.functions.*;
27+
import rx.util.PrivateConstructorChecker;
2828

2929
public class ObserversTest {
3030
@Test
3131
public void testNotInstantiable() {
32-
try {
33-
Constructor<?> c = Observers.class.getDeclaredConstructor();
34-
c.setAccessible(true);
35-
Object instance = c.newInstance();
36-
fail("Could instantiate Actions! " + instance);
37-
} catch (NoSuchMethodException ex) {
38-
ex.printStackTrace();
39-
} catch (InvocationTargetException ex) {
40-
ex.printStackTrace();
41-
} catch (InstantiationException ex) {
42-
ex.printStackTrace();
43-
} catch (IllegalAccessException ex) {
44-
ex.printStackTrace();
45-
}
32+
PrivateConstructorChecker
33+
.forClass(Observers.class)
34+
.expectedTypeOfException(IllegalStateException.class)
35+
.expectedExceptionMessage("No instances!")
36+
.check();
4637
}
4738

4839
@Test

src/test/java/rx/observers/SubscribersTest.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,22 @@
1818
import static org.junit.Assert.*;
1919
import static org.mockito.Mockito.*;
2020

21-
import java.lang.reflect.*;
2221
import java.util.concurrent.atomic.*;
2322

2423
import org.junit.Test;
2524

2625
import rx.exceptions.*;
2726
import rx.functions.*;
27+
import rx.util.PrivateConstructorChecker;
2828

2929
public class SubscribersTest {
3030
@Test
3131
public void testNotInstantiable() {
32-
try {
33-
Constructor<?> c = Subscribers.class.getDeclaredConstructor();
34-
c.setAccessible(true);
35-
Object instance = c.newInstance();
36-
fail("Could instantiate Actions! " + instance);
37-
} catch (NoSuchMethodException ex) {
38-
ex.printStackTrace();
39-
} catch (InvocationTargetException ex) {
40-
ex.printStackTrace();
41-
} catch (InstantiationException ex) {
42-
ex.printStackTrace();
43-
} catch (IllegalAccessException ex) {
44-
ex.printStackTrace();
45-
}
32+
PrivateConstructorChecker
33+
.forClass(Subscribers.class)
34+
.expectedTypeOfException(IllegalStateException.class)
35+
.expectedExceptionMessage("No instances!")
36+
.check();
4637
}
4738

4839
@Test
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
package rx.util;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.fail;
7+
8+
public class PrivateConstructionCheckerTest {
9+
10+
@Test
11+
public void builderShouldThrowExceptionIfNullWasPassedAsExpectedTypeOfException() {
12+
try {
13+
PrivateConstructorChecker
14+
.forClass(Object.class)
15+
.expectedTypeOfException(null);
16+
17+
fail();
18+
} catch (IllegalArgumentException expected) {
19+
assertEquals("expectedTypeOfException can not be null", expected.getMessage());
20+
}
21+
}
22+
23+
@Test
24+
public void builderShouldThrowExceptionIfNullWasPassedAsExpectedExceptionMessage() {
25+
try {
26+
PrivateConstructorChecker
27+
.forClass(Object.class)
28+
.expectedExceptionMessage(null);
29+
30+
fail();
31+
} catch (IllegalArgumentException expected) {
32+
assertEquals("expectedExceptionMessage can not be null", expected.getMessage());
33+
}
34+
}
35+
36+
static class ClassWithoutDefaultConstructor {
37+
private ClassWithoutDefaultConstructor(String someParam) {
38+
}
39+
}
40+
41+
@Test
42+
public void shouldThrowExceptionIfClassHasNonDefaultConstructor() {
43+
try {
44+
PrivateConstructorChecker
45+
.forClass(ClassWithoutDefaultConstructor.class)
46+
.check();
47+
48+
fail();
49+
} catch (AssertionError expected) {
50+
assertEquals(
51+
"Class has non-default constructor with some parameters",
52+
expected.getMessage()
53+
);
54+
}
55+
}
56+
57+
static class ClassWithPrivateConstructor {
58+
private ClassWithPrivateConstructor() {
59+
}
60+
}
61+
62+
@Test
63+
public void shouldAssertThatConstructorIsPrivateAndDoesNotThrowExceptions() {
64+
PrivateConstructorChecker
65+
.forClass(ClassWithPrivateConstructor.class)
66+
.check();
67+
}
68+
69+
static class ClassWithDefaultProtectedConstructor {
70+
ClassWithDefaultProtectedConstructor() {
71+
}
72+
}
73+
74+
@Test
75+
public void shouldThrowExceptionBecauseConstructorHasDefaultModifier() {
76+
try {
77+
PrivateConstructorChecker
78+
.forClass(ClassWithDefaultProtectedConstructor.class)
79+
.check();
80+
81+
fail();
82+
} catch (AssertionError expected) {
83+
assertEquals("Constructor must be private", expected.getMessage());
84+
}
85+
}
86+
87+
static class ClassWithProtectedConstructor {
88+
protected ClassWithProtectedConstructor() {
89+
}
90+
}
91+
92+
@Test
93+
public void shouldThrowExceptionBecauseConstructorHasProtectedModifier() {
94+
try {
95+
PrivateConstructorChecker
96+
.forClass(ClassWithProtectedConstructor.class)
97+
.check();
98+
99+
fail();
100+
} catch (AssertionError expected) {
101+
assertEquals("Constructor must be private", expected.getMessage());
102+
}
103+
}
104+
105+
static class ClassWithPublicConstructor {
106+
public ClassWithPublicConstructor() {
107+
}
108+
}
109+
110+
@Test
111+
public void shouldThrowExceptionBecauseConstructorHasPublicModifier() {
112+
try {
113+
PrivateConstructorChecker
114+
.forClass(ClassWithPublicConstructor.class)
115+
.check();
116+
117+
fail();
118+
} catch (AssertionError expected) {
119+
assertEquals("Constructor must be private", expected.getMessage());
120+
}
121+
}
122+
123+
static class ClassWithConstructorThatThrowsException {
124+
private ClassWithConstructorThatThrowsException() {
125+
throw new IllegalStateException("test exception");
126+
}
127+
}
128+
129+
@Test
130+
public void shouldCheckThatConstructorThrowsExceptionWithoutCheckingMessage() {
131+
PrivateConstructorChecker
132+
.forClass(ClassWithConstructorThatThrowsException.class)
133+
.expectedTypeOfException(IllegalStateException.class)
134+
.check();
135+
}
136+
137+
@Test
138+
public void shouldCheckThatConstructorThrowsExceptionWithExpectedMessage() {
139+
PrivateConstructorChecker
140+
.forClass(ClassWithConstructorThatThrowsException.class)
141+
.expectedTypeOfException(IllegalStateException.class)
142+
.expectedExceptionMessage("test exception")
143+
.check();
144+
}
145+
146+
@Test
147+
public void shouldCheckThatConstructorThrowsExceptionWithExpectedMessageButWithoutExpectedExceptionType() {
148+
PrivateConstructorChecker
149+
.forClass(ClassWithConstructorThatThrowsException.class)
150+
.expectedExceptionMessage("test exception")
151+
.check(); // without checking exception's type
152+
}
153+
154+
@Test
155+
public void shouldThrowExceptionBecauseTypeOfExpectedExceptionDoesNotMatch() {
156+
try {
157+
PrivateConstructorChecker
158+
.forClass(ClassWithConstructorThatThrowsException.class)
159+
.expectedTypeOfException(IllegalArgumentException.class) // Incorrect type
160+
.check();
161+
162+
fail();
163+
} catch (IllegalStateException expected) {
164+
assertEquals("Expected exception of type = class java.lang.IllegalArgumentException, " +
165+
"but was exception of type = class java.lang.IllegalStateException",
166+
expected.getMessage()
167+
);
168+
}
169+
}
170+
171+
@Test
172+
public void shouldThrowExceptionBecauseExpectedMessageDoesNotMatch() {
173+
try {
174+
PrivateConstructorChecker
175+
.forClass(ClassWithConstructorThatThrowsException.class)
176+
.expectedTypeOfException(IllegalStateException.class) // Correct type
177+
.expectedExceptionMessage("lol, not something that you've expected?") // Incorrect message
178+
.check();
179+
180+
fail();
181+
} catch (IllegalStateException expected) {
182+
assertEquals("Expected exception message = 'lol, not something that you've expected?', " +
183+
"but was = 'test exception'",
184+
expected.getMessage()
185+
);
186+
}
187+
}
188+
189+
@Test
190+
public void shouldThrowExceptionBecauseConstructorThrownUnexpectedException() {
191+
try {
192+
PrivateConstructorChecker
193+
.forClass(ClassWithConstructorThatThrowsException.class)
194+
.check(); // We don't expect exception, but it will be thrown
195+
196+
fail();
197+
} catch (IllegalStateException expected) {
198+
assertEquals("No exception was expected", expected.getMessage());
199+
}
200+
}
201+
202+
static class ClassWith2Constructors {
203+
// This is good constructor for the checker
204+
private ClassWith2Constructors() {
205+
206+
}
207+
208+
// This is bad constructor
209+
private ClassWith2Constructors(String str) {
210+
211+
}
212+
}
213+
214+
@Test
215+
public void shouldThrowExceptionBecauseClassHasMoreThanOneConstructor() {
216+
try {
217+
PrivateConstructorChecker
218+
.forClass(ClassWith2Constructors.class)
219+
.check();
220+
221+
fail();
222+
} catch (AssertionError expected) {
223+
assertEquals("Class has more than one constructor", expected.getMessage());
224+
}
225+
}
226+
227+
static class ClassWithoutDeclaredConstructor {
228+
229+
}
230+
231+
@Test
232+
public void shouldThrowExceptionBecauseClassDoesNotHaveDeclaredConstructors() {
233+
try {
234+
PrivateConstructorChecker
235+
.forClass(ClassWithoutDeclaredConstructor.class)
236+
.check();
237+
238+
fail();
239+
} catch (AssertionError expected) {
240+
assertEquals("Constructor must be private", expected.getMessage());
241+
}
242+
}
243+
}

0 commit comments

Comments
 (0)