Skip to content

Commit defb952

Browse files
committed
Issue ota4j-team#21: Increase Test Coverage
* Provide a lot more tests. * Remove unused methods from AssertionFailedException about actual/expected.
1 parent d39d2a8 commit defb952

12 files changed

+228
-156
lines changed

src/main/java/org/opentest4j/AssertionFailedException.java

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
package org.opentest4j;
1818

19-
import static org.opentest4j.debug.DebugInformation.builder;
20-
import static org.opentest4j.debug.DebugInformationDefaultKey.ACTUAL;
21-
import static org.opentest4j.debug.DebugInformationDefaultKey.EXPECTED;
22-
2319
import org.opentest4j.debug.DebugInformation;
2420
import org.opentest4j.debug.ValueDescriptor;
2521

@@ -41,19 +37,17 @@ public class AssertionFailedException extends DebuggableException {
4137

4238
private static final long serialVersionUID = 1L;
4339

44-
/**
45-
* Constructs an {@code AssertionFailedError} with no message, no cause, and no expected/actual values.
46-
*/
4740
public AssertionFailedException() {
4841
}
4942

50-
/**
51-
* Constructs an {@code AssertionFailedError} with a message, no cause, and no expected/actual values.
52-
*/
5343
public AssertionFailedException(String message) {
5444
super(message);
5545
}
5646

47+
public AssertionFailedException(String message, Throwable cause) {
48+
super(message, cause);
49+
}
50+
5751
public AssertionFailedException(Throwable cause) {
5852
super(cause);
5953
}
@@ -74,64 +68,4 @@ public AssertionFailedException(DebugInformation debugInformation, Throwable cau
7468
super(debugInformation, cause);
7569
}
7670

77-
/**
78-
* Constructs an {@code AssertionFailedError} with a message and expected/actual values but without a cause.
79-
*/
80-
public AssertionFailedException(String message, Object expected, Object actual) {
81-
this(message,
82-
builder().put(EXPECTED.getId(), expected).put(ACTUAL.getId(), actual).build());
83-
}
84-
85-
/**
86-
* Constructs an {@code AssertionFailedError} with a message and a cause but without expected/actual values.
87-
*/
88-
public AssertionFailedException(String message, Throwable cause) {
89-
super(message, cause);
90-
}
91-
92-
/**
93-
* Constructs an {@code AssertionFailedError} with a message, expected/actual values, and a cause.
94-
*/
95-
public AssertionFailedException(String message, Object expected, Object actual, Throwable cause) {
96-
this(message,
97-
builder().put(EXPECTED.getId(), expected).put(ACTUAL.getId(), actual).build(),
98-
cause);
99-
}
100-
101-
/**
102-
* Returns {@code true} if the expected value is defined, i.e. was passed to the constructor.
103-
*
104-
* @see #getExpected()
105-
*/
106-
public boolean isExpectedDefined() {
107-
return hasDebugInformation() && getDebugInformation().containsKey(EXPECTED.getId());
108-
}
109-
110-
/**
111-
* Returns {@code true} if the actual value is defined, i.e. was passed to the constructor.
112-
*
113-
* @see #getActual()
114-
*/
115-
public boolean isActualDefined() {
116-
return hasDebugInformation() && getDebugInformation().containsKey(ACTUAL.getId());
117-
}
118-
119-
/**
120-
* Returns the wrapped expected value if it is defined; otherwise {@code null}.
121-
*
122-
* @see #isExpectedDefined()
123-
*/
124-
public ValueDescriptor getExpected() {
125-
return isExpectedDefined() ? getDebugInformation().get(EXPECTED.getId()) : null;
126-
}
127-
128-
/**
129-
* Returns the wrapped actual value if it is defined; otherwise {@code null}.
130-
*
131-
* @see #isActualDefined()
132-
*/
133-
public ValueDescriptor getActual() {
134-
return isActualDefined() ? getDebugInformation().get(ACTUAL.getId()) : null;
135-
}
136-
13771
}

src/main/java/org/opentest4j/IncompleteExecutionException.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.opentest4j;
1818

19+
import org.opentest4j.debug.DebugInformation;
20+
1921
/**
2022
* {@link RuntimeException} used to indicate that the execution of a test
2123
* was incomplete — for example, that the execution was entirely
@@ -26,9 +28,10 @@
2628
*
2729
* @author Johannes Link
2830
* @author Sam Brannen
31+
* @author Mark Michaelis
2932
* @since 1.0
3033
*/
31-
public class IncompleteExecutionException extends RuntimeException {
34+
public class IncompleteExecutionException extends DebuggableException {
3235

3336
private static final long serialVersionUID = 1L;
3437

@@ -43,4 +46,23 @@ public IncompleteExecutionException(String message, Throwable cause) {
4346
super(message, cause);
4447
}
4548

49+
public IncompleteExecutionException(Throwable cause) {
50+
super(cause);
51+
}
52+
53+
public IncompleteExecutionException(DebugInformation debugInformation) {
54+
super(debugInformation);
55+
}
56+
57+
public IncompleteExecutionException(String message, DebugInformation debugInformation) {
58+
super(message, debugInformation);
59+
}
60+
61+
public IncompleteExecutionException(String message, DebugInformation debugInformation, Throwable cause) {
62+
super(message, debugInformation, cause);
63+
}
64+
65+
public IncompleteExecutionException(DebugInformation debugInformation, Throwable cause) {
66+
super(debugInformation, cause);
67+
}
4668
}

src/main/java/org/opentest4j/TestAbortedException.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616

1717
package org.opentest4j;
1818

19+
import org.opentest4j.debug.DebugInformation;
20+
1921
/**
2022
* Specialization of {@link IncompleteExecutionException} used to indicate
2123
* that a test was aborted during execution (e.g., due to a failed
2224
* <em>assumption</em>).
2325
*
2426
* @author Sam Brannen
2527
* @author Johannes Link
28+
* @author Mark Michaelis
2629
* @since 1.0
2730
* @see TestSkippedException
2831
*/
@@ -41,4 +44,23 @@ public TestAbortedException(String message, Throwable cause) {
4144
super(message, cause);
4245
}
4346

47+
public TestAbortedException(Throwable cause) {
48+
super(cause);
49+
}
50+
51+
public TestAbortedException(DebugInformation debugInformation) {
52+
super(debugInformation);
53+
}
54+
55+
public TestAbortedException(String message, DebugInformation debugInformation) {
56+
super(message, debugInformation);
57+
}
58+
59+
public TestAbortedException(String message, DebugInformation debugInformation, Throwable cause) {
60+
super(message, debugInformation, cause);
61+
}
62+
63+
public TestAbortedException(DebugInformation debugInformation, Throwable cause) {
64+
super(debugInformation, cause);
65+
}
4466
}

src/main/java/org/opentest4j/TestSkippedException.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
package org.opentest4j;
1818

19+
import org.opentest4j.debug.DebugInformation;
20+
1921
/**
20-
* Specialization of {@link IncompleteExecutionException} used to indicate
21-
* that a test was skipped <em>prior</em> to execution (e.g., <em>disabled</em>
22-
* or <em>ignored</em>).
22+
* Specialization of {@link IncompleteExecutionException} used to indicate that a test was skipped <em>prior</em> to
23+
* execution (e.g., <em>disabled</em> or <em>ignored</em>).
2324
*
2425
* @author Sam Brannen
2526
* @author Johannes Link
27+
* @author Mark Michaelis
2628
* @since 1.0
2729
* @see TestAbortedException
2830
*/
@@ -41,4 +43,23 @@ public TestSkippedException(String message, Throwable cause) {
4143
super(message, cause);
4244
}
4345

46+
public TestSkippedException(Throwable cause) {
47+
super(cause);
48+
}
49+
50+
public TestSkippedException(DebugInformation debugInformation) {
51+
super(debugInformation);
52+
}
53+
54+
public TestSkippedException(String message, DebugInformation debugInformation) {
55+
super(message, debugInformation);
56+
}
57+
58+
public TestSkippedException(String message, DebugInformation debugInformation, Throwable cause) {
59+
super(message, debugInformation, cause);
60+
}
61+
62+
public TestSkippedException(DebugInformation debugInformation, Throwable cause) {
63+
super(debugInformation, cause);
64+
}
4465
}

src/main/java/org/opentest4j/debug/DebugInformationDefaultKey.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
package org.opentest4j.debug;
33

44
/**
5+
* Some basic conventions for keys to be used in debug information.
6+
*
57
* @author Mark Michaelis
68
* @since 1.0
9+
* @see DebugInformation
710
*/
811
public enum DebugInformationDefaultKey {
912
// @formatter:off

src/main/java/org/opentest4j/debug/serializer/AbstractValueWrapper.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11

22
package org.opentest4j.debug.serializer;
33

4+
import static java.util.Collections.unmodifiableMap;
5+
46
import java.io.Serializable;
5-
import java.util.Collections;
67
import java.util.HashMap;
78
import java.util.Map;
89

9-
import static java.util.Collections.unmodifiableMap;
10-
1110
/**
1211
* Abstract {@code ValueWrapper} which provides some default handling for additional data.
1312
*
@@ -21,7 +20,7 @@ public Serializable putData(String key, Serializable value) {
2120
return this.data.put(key, value);
2221
}
2322

24-
public void putAllData(Map<String, Serializable> otherMap) {
23+
public void putAllData(Map<String, ? extends Serializable> otherMap) {
2524
this.data.putAll(otherMap);
2625
}
2726

src/test/java/org/opentest4j/AssertionFailedExceptionTests.java

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616

1717
package org.opentest4j;
1818

19-
import static org.junit.Assert.*;
19+
import static org.opentest4j.ExceptionTestHelper.assertBehavesLikeDebuggableException;
2020
import static org.opentest4j.ExceptionTestHelper.assertBehavesLikeStandardException;
2121

22-
import org.junit.Ignore;
2322
import org.junit.Test;
2423

2524
/**
@@ -39,80 +38,8 @@ public void standardConstructorsHaveSameBehaviorAsJavaStandard() throws Exceptio
3938
}
4039

4140
@Test
42-
@Ignore(value = "Having no message for standard exceptions leads to null message - not an empty message.")
43-
public void nullMessageIsConvertedToEmptyString() {
44-
assertEquals("", new AssertionFailedException().getMessage());
45-
assertEquals("", new AssertionFailedException((String)null).getMessage());
46-
assertEquals("", new AssertionFailedException((String)null, (Throwable)null).getMessage());
47-
assertEquals("", new AssertionFailedException(null, "foo", "bar").getMessage());
48-
assertEquals("", new AssertionFailedException(null, "foo", "bar", null).getMessage());
49-
}
50-
51-
@Test
52-
@Ignore(value = "Standard exceptions do not trim messages.")
53-
public void blankMessageIsConvertedToEmptyString() {
54-
assertEquals("", new AssertionFailedException(" ").getMessage());
55-
assertEquals("", new AssertionFailedException(" ", (Throwable)null).getMessage());
56-
assertEquals("", new AssertionFailedException(" ", "foo", "bar").getMessage());
57-
assertEquals("", new AssertionFailedException(" ", "foo", "bar", null).getMessage());
58-
}
59-
60-
@Test
61-
public void messageAndCauseAreStored() {
62-
RuntimeException cause = new RuntimeException("cause");
63-
64-
AssertionFailedException error = new AssertionFailedException("my message", cause);
65-
66-
assertEquals("my message", error.getMessage());
67-
assertEquals(cause, error.getCause());
68-
assertFalse(error.isExpectedDefined());
69-
assertFalse(error.isActualDefined());
70-
}
71-
72-
@Test
73-
public void expectedAndActualValuesAreStored() {
74-
AssertionFailedException errorWithExpectedAndActual = new AssertionFailedException(null, "foo", "bar");
75-
assertTrue(errorWithExpectedAndActual.isExpectedDefined());
76-
assertEquals("foo", errorWithExpectedAndActual.getExpected().getValue());
77-
assertTrue(errorWithExpectedAndActual.isActualDefined());
78-
assertEquals("bar", errorWithExpectedAndActual.getActual().getValue());
79-
}
80-
81-
@Test
82-
public void returnsNullForExpectedAndActualWhenNotPassedToConstructor() {
83-
AssertionFailedException errorWithoutExpectedAndActual = new AssertionFailedException();
84-
assertFalse(errorWithoutExpectedAndActual.isExpectedDefined());
85-
assertNull(errorWithoutExpectedAndActual.getExpected());
86-
assertFalse(errorWithoutExpectedAndActual.isActualDefined());
87-
assertNull(errorWithoutExpectedAndActual.getActual());
88-
}
89-
90-
@Test
91-
public void serializationWorksForAssertionFailedErrorWithMessageAndExpectedAndActualValues() throws Exception {
92-
AssertionFailedException error = serializeAndDeserialize(
93-
new AssertionFailedException("a message", "foo", "bar"));
94-
95-
assertEquals("a message", error.getMessage());
96-
assertTrue(error.isExpectedDefined());
97-
assertEquals("foo", error.getExpected().getValue());
98-
assertTrue(error.isActualDefined());
99-
assertEquals("bar", error.getActual().getValue());
100-
}
101-
102-
@Test
103-
public void serializationWorksForAssertionFailedErrorWithoutAnyValues() throws Exception {
104-
AssertionFailedException originalError = new AssertionFailedException();
105-
AssertionFailedException error = serializeAndDeserialize(originalError);
106-
107-
assertEquals(originalError.getMessage(), error.getMessage());
108-
assertEquals(originalError.isExpectedDefined(), error.isExpectedDefined());
109-
assertEquals(originalError.getExpected(), error.getExpected());
110-
assertEquals(originalError.isActualDefined(), error.isActualDefined());
111-
assertEquals(originalError.getActual(), error.getActual());
112-
}
113-
114-
private AssertionFailedException serializeAndDeserialize(AssertionFailedException originalError) throws Exception {
115-
return (AssertionFailedException) TestSerializationHelper.serializeAndDeserialize(originalError);
41+
public void debugInformationConstructorsBehaveAsExpected() throws Exception {
42+
assertBehavesLikeDebuggableException(AssertionFailedException.class);
11643
}
11744

11845
}

src/test/java/org/opentest4j/ExceptionTestHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static void assertBehavesLikeStandardException(Class<? extends Throwable>
3737
assertMessageCauseConstructorBehavesAsExpected(exceptionClass);
3838
}
3939

40-
public static void assertBehavesLikeDebuggableException(Class<DebuggableException> exceptionClass)
40+
public static void assertBehavesLikeDebuggableException(Class<? extends DebuggableException> exceptionClass)
4141
throws Exception {
4242
assertBehavesLikeStandardException(exceptionClass);
4343

@@ -111,7 +111,7 @@ private static void assertMessageCauseConstructorBehavesAsExpected(Class<? exten
111111
//------------------------------------------------------------------------------------------------------------------
112112

113113
private static void assertOnlyDebugInformationConstructorBehavesAsExpected(
114-
Class<DebuggableException> exceptionClass) throws Exception {
114+
Class<? extends DebuggableException> exceptionClass) throws Exception {
115115
DebuggableException exceptionInstance = exceptionClass.getConstructor(DebugInformation.class).newInstance(
116116
DEBUG_INFORMATION_REFERENCE);
117117

0 commit comments

Comments
 (0)