Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Slightly improved documentation and code coverage (100%, except for the

proxy package).
  • Loading branch information...
commit d4a5753f431ed71dc48f2d9a77e7129a945979f4 1 parent a7c0364
Thomas Mueller authored
3  assertthrows/src/main/java/org/junit/contrib/assertthrows/AssertThrows.java
@@ -43,8 +43,7 @@
43 43
44 44 /**
45 45 * Verify an exception or error is thrown. Internally, the constructor calls
46   - * {@link #verify}, which calls {@link #test} and verifies it throws an
47   - * exception or error.
  46 + * calls {@link #test} and verifies it throws an exception or error.
48 47 * <p>
49 48 * For the test to pass, the {@link #test} method must throw any kind of
50 49 * {@link Exception} or {@link Error} ( {@link AssertionError},
11 assertthrows/src/main/java/org/junit/contrib/assertthrows/verify/ExceptionVerifier.java
@@ -72,10 +72,6 @@ public ExceptionVerifier(Exception expectedException) {
72 72 throw new NullPointerException("The passed exception is null");
73 73 }
74 74 this.expectedExceptionClass = expectedException.getClass();
75   - if (expectedExceptionClass == null) {
76   - // overcareful
77   - throw new NullPointerException("The passed exception class is null");
78   - }
79 75 this.expectedException = expectedException;
80 76 }
81 77
@@ -241,8 +237,9 @@ public static Throwable getLastThrown() {
241 237 this.verifier = verifier;
242 238 this.obj = obj;
243 239 this.called = new AssertionError(
244   - "A proxy for the class " + obj.getClass().getName() + " was created, " +
245   - "but then no overridable method was called on it. " +
  240 + "A proxy for the class\n" +
  241 + obj.getClass().getName() + "\n" +
  242 + "was created, but then no overridable method was called on it.\n" +
246 243 "See the stack trace for where the proxy was created.");
247 244 }
248 245
@@ -262,6 +259,8 @@ public void finalize() {
262 259 }
263 260 public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
264 261 if ("finalize".equals(method.getName())) {
  262 + // we _could_ support this method, but then detecting
  263 + // that no method was called would no longer work
265 264 return method.invoke(obj, args);
266 265 }
267 266 called = null;
19 assertthrows/src/site/resources/index.html
@@ -126,14 +126,15 @@ <h2 id="Proxy">Proxy</h2>
126 126 <h3>Limitations and Class Proxies</h3>
127 127 <p>
128 128 Static methods and constructors can't be tested in this way.
  129 +Testing the <code>finalize</code> method is not supported.
129 130 This solution works best with interfaces (as in this example the <code>List</code> interface).
130 131 By default, <code>assertThrows</code> will try to create a proxy
131 132 using the <code>java.lang.reflect.Proxy</code> mechanism.
132 133 For classes that do not implement an interface, and to test methods that are not part of any interface,
133 134 a class proxy is required (a proxy that extends a class, and not just implements interfaces).
134 135 Because this is tricky, <code>assertThrows</code> doesn't do that by default.
135   -To explicitly use a class proxy, use <code>AssertThrows.useClassProxy</code>.
136   -As an example, if you declare the <code>emptyList</code> as an instance
  136 +To explicitly use a class proxy, call <code>AssertThrows.useClassProxy</code>.
  137 +As an example, if you declare <code>emptyList</code> to be an instance
137 138 of the concrete class <code>ArrayList</code>, you need to tell the tool to use a class proxy:
138 139 </p>
139 140 <pre>
@@ -143,7 +144,7 @@ <h2 id="Proxy">Proxy</h2>
143 144 </pre>
144 145 <p>
145 146 Once such a class proxy is created, it is used for all future <code>assertThrows</code> calls
146   -where the provided object is of exactly this class.
  147 +with object is of this class.
147 148 Creating a proxy that extends a class is a bit challenging.
148 149 It is not possibly to create a proxy for a final class, because extending a final class is not allowed.
149 150 Also, final methods can not be overridden.
@@ -159,20 +160,20 @@ <h2 id="Proxy">Proxy</h2>
159 160 </p>
160 161 <h3>Calling a Final Method, and Forgetting to Call a Method</h3>
161 162 <p>
162   -Final method can not be overridden, and therefore the tool can not verify
163   -it threw an exception. However, such cases are detected
  163 +A final method can not be overridden, and therefore the tool can not verify
  164 +whether it threw an exception. However, such cases are detected
164 165 on the next usage (the next call to <code>assertThrows</code>, and the
165 166 next time an <code>AssertThrows</code> object is created),
166 167 or when the proxy is garbage collected.
167 168 So if you made a mistake and called a final method, or did not call method
168   -on the proxy <code>assertThrows</code> statement, the tool will detect it.
  169 +on the proxy <code>assertThrows</code> object, the tool will detect it.
169 170 As an example, the following code will throw an exception saying
170 171 that no method was called on the proxy:
171 172 </p>
172 173 <pre>
173 174 List<String> list = new ArrayList<String>();
174 175 assertThrows(list);
175   -assertThrows(list);
  176 +assertThrows(list).get(0);
176 177 </pre>
177 178
178 179 <h2 id="AnonymousClass">Anonymous Class</h2>
@@ -212,7 +213,7 @@ <h2 id="AnonymousClass">Anonymous Class</h2>
212 213
213 214 <h2 id="LastThrown">Getting the Last Throwable</h2>
214 215 <p>
215   -If you need to verify the exception that in more detail
  216 +If you need to verify the exception in more detail
216 217 (for example, match the message against a pattern, or verify the cause),
217 218 you can get the last exception or error as follows:
218 219 </p>
@@ -233,7 +234,7 @@ <h2 id="Dependencies">Dependencies</h2>
233 234 This project does not required additional libraries. However, if you want to
234 235 test using class proxies, it is suggested to add the libraries Cglib and Objenesis to the classpath while running the tests.
235 236 Please note those are optional dependencies only; within the described limitations, this tool works without them.
236   -When using Maven, use:
  237 +To add the dependencies to a Maven project, use:
237 238 </p>
238 239 <pre>
239 240 &lt;dependency&gt;
2  assertthrows/src/test/java/org/junit/contrib/assertthrows/DocCodeSamplesTest.java
@@ -88,7 +88,7 @@ public void testDetectNoMethodWasCalled() {
88 88 new AssertThrows() { public void test() {
89 89 List<String> list = new ArrayList<String>();
90 90 assertThrows(list);
91   - assertThrows(list);
  91 + assertThrows(list).get(0);
92 92 }};
93 93 }
94 94
50 assertthrows/src/test/java/org/junit/contrib/assertthrows/ProxyTest.java
@@ -18,6 +18,7 @@
18 18
19 19 import static org.junit.Assert.assertEquals;
20 20 import static org.junit.Assert.assertNull;
  21 +import static org.junit.Assert.assertTrue;
21 22 import static org.junit.contrib.assertthrows.AssertThrows.assertThrows;
22 23 import java.io.ByteArrayOutputStream;
23 24 import java.io.PrintStream;
@@ -49,6 +50,7 @@ public void testExpectedExceptionClass() {
49 50 @Test
50 51 public void testExpectedExceptionAsObject() {
51 52 final List<String> list = new ArrayList<String>();
  53 + assertThrows(new NullPointerException(), list).addAll(null);
52 54 assertThrows(
53 55 new IndexOutOfBoundsException("Index: 0, Size: 0"), list).
54 56 get(0);
@@ -105,12 +107,12 @@ public void testWrongException() {
105 107 assertEquals(IndexOutOfBoundsException.class, e.getCause().getClass());
106 108
107 109 new AssertThrows() { public void test() {
108   - assertThrows(new Exception("Index: 0, Size: 0"), list).get(0);
  110 + assertThrows(new Exception("Index: 0, Size: 0"), list).set(10, null);
109 111 }};
110 112 e = AssertThrows.getLastThrown();
111 113 assertEquals("Expected an exception of type\n" +
112 114 "Exception to be thrown,\n" +
113   - "but the method get(0) threw an exception of type\n" +
  115 + "but the method set(10, null) threw an exception of type\n" +
114 116 "IndexOutOfBoundsException " +
115 117 "(see in the 'Caused by' for the exception that was thrown)",
116 118 e.getMessage());
@@ -190,9 +192,23 @@ public void testForgotToCallMethodDetectedOnNextCall() {
190 192 assertThrows(list).get(0);
191 193 }};
192 194 Throwable t = AssertThrows.getLastThrown();
193   - assertEquals("A proxy for the class " +
194   - "org.junit.contrib.assertthrows.ProxyTest$TestClass was created, " +
195   - "but then no overridable method was called on it. " +
  195 + assertEquals("A proxy for the class\n" +
  196 + "org.junit.contrib.assertthrows.ProxyTest$ClassWithFinalMethod\n" +
  197 + "was created, but then no overridable method was called on it.\n" +
  198 + "See the stack trace for where the proxy was created.", t.getMessage());
  199 + }
  200 +
  201 + @Test
  202 + public void testFinalizeNotSupported() {
  203 + new AssertThrows() { public void test() {
  204 + ClassWithFinalizer test = new ClassWithFinalizer();
  205 + assertThrows(test).finalize();
  206 + assertThrows(test).finalize();
  207 + }};
  208 + Throwable t = AssertThrows.getLastThrown();
  209 + assertEquals("A proxy for the class\n" +
  210 + "org.junit.contrib.assertthrows.ProxyTest$ClassWithFinalizer\n" +
  211 + "was created, but then no overridable method was called on it.\n" +
196 212 "See the stack trace for where the proxy was created.", t.getMessage());
197 213 }
198 214
@@ -214,11 +230,11 @@ public void testForgotToCallMethodDetectedOnFinalize() {
214 230 String error = new String(buff.toByteArray());
215 231 if (error.length() > 0) {
216 232 // only verify the message if it _did_ work
217   - String firstLine = error.substring(0, error.indexOf('\n'));
218   - assertEquals("java.lang.AssertionError: A proxy for the class " +
219   - "org.junit.contrib.assertthrows.ProxyTest$TestClass was created, " +
220   - "but then no overridable method was called on it. " +
221   - "See the stack trace for where the proxy was created.", firstLine);
  233 + assertTrue(error,
  234 + error.startsWith("java.lang.AssertionError: A proxy for the class\n" +
  235 + "org.junit.contrib.assertthrows.ProxyTest$ClassWithFinalMethod\n" +
  236 + "was created, but then no overridable method was called on it.\n" +
  237 + "See the stack trace for where the proxy was created."));
222 238 }
223 239 } finally {
224 240 System.setErr(oldErr);
@@ -226,7 +242,7 @@ public void testForgotToCallMethodDetectedOnFinalize() {
226 242 }
227 243
228 244 private void testFinalMethod() {
229   - TestClass test = new TestClass();
  245 + ClassWithFinalMethod test = new ClassWithFinalMethod();
230 246 assertThrows(test).finalTestMethod();
231 247 }
232 248
@@ -234,10 +250,20 @@ private void testFinalMethod() {
234 250 * A test method with a final method. Final methods can't be overridden, to
235 251 * they can't be tested using <code>assertThrows</code>.
236 252 */
237   - public static class TestClass {
  253 + public static class ClassWithFinalMethod {
238 254 public final void finalTestMethod() {
239 255 // do nothing
240 256 }
241 257 }
242 258
  259 + /**
  260 + * A test method with a final method. Final methods can't be overridden, to
  261 + * they can't be tested using <code>assertThrows</code>.
  262 + */
  263 + public static class ClassWithFinalizer {
  264 + public void finalize() {
  265 + // do nothing
  266 + }
  267 + }
  268 +
243 269 }
27 assertthrows/src/test/java/org/junit/contrib/assertthrows/proxy/ReflectionUtilsTest.java
@@ -32,6 +32,9 @@
32 32 public void testDefaultValue() {
33 33 assertNull(ReflectionUtils.getDefaultValue(Integer.class));
34 34 assertEquals(
  35 + null,
  36 + ReflectionUtils.getDefaultValue(void.class));
  37 + assertEquals(
35 38 Boolean.valueOf(false),
36 39 ReflectionUtils.getDefaultValue(boolean.class));
37 40 assertEquals(
@@ -58,6 +61,30 @@ public void testDefaultValue() {
58 61 }
59 62
60 63 @Test
  64 + public void testNonPrimitiveClass() {
  65 + assertEquals(Object.class,
  66 + ReflectionUtils.getNonPrimitiveClass(Object.class));
  67 + assertEquals(Void.class,
  68 + ReflectionUtils.getNonPrimitiveClass(void.class));
  69 + assertEquals(Boolean.class,
  70 + ReflectionUtils.getNonPrimitiveClass(boolean.class));
  71 + assertEquals(Byte.class,
  72 + ReflectionUtils.getNonPrimitiveClass(byte.class));
  73 + assertEquals(Character.class,
  74 + ReflectionUtils.getNonPrimitiveClass(char.class));
  75 + assertEquals(Short.class,
  76 + ReflectionUtils.getNonPrimitiveClass(short.class));
  77 + assertEquals(Integer.class,
  78 + ReflectionUtils.getNonPrimitiveClass(int.class));
  79 + assertEquals(Long.class,
  80 + ReflectionUtils.getNonPrimitiveClass(long.class));
  81 + assertEquals(Float.class,
  82 + ReflectionUtils.getNonPrimitiveClass(float.class));
  83 + assertEquals(Double.class,
  84 + ReflectionUtils.getNonPrimitiveClass(double.class));
  85 + }
  86 +
  87 + @Test
61 88 public void testPackageName() {
62 89 assertEquals("", ReflectionUtils.getPackageName(int.class));
63 90 assertEquals("java.lang", ReflectionUtils.getPackageName(Integer.class));

0 comments on commit d4a5753

Please sign in to comment.
Something went wrong with that request. Please try again.