Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hasMessage now fails with an AssertionFailedError if available and wi…
…ll report actual and expected messages which can be visually compared in IDEs (the assertion error message is still the same). If AssertionFailedError is not available, simply fallback to a simple AssertionError as previously.
- Loading branch information
1 parent
0547bfc
commit 1e40b38
Showing
4 changed files
with
108 additions
and
36 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/org/assertj/core/error/AssertionErrorCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2012-2018 the original author or authors. | ||
*/ | ||
package org.assertj.core.error; | ||
|
||
import static org.assertj.core.util.Arrays.array; | ||
|
||
import java.util.Optional; | ||
|
||
import org.assertj.core.util.VisibleForTesting; | ||
|
||
// TODO deprecate AssertionErrorFactory | ||
public class AssertionErrorCreator { | ||
|
||
private static final Class<?>[] MSG_ARG_TYPES_FOR_ASSERTION_FAILED_ERROR = array(String.class, Object.class, Object.class); | ||
|
||
@VisibleForTesting | ||
static ConstructorInvoker constructorInvoker = new ConstructorInvoker(); | ||
|
||
public static AssertionError assertionError(String message, Object actual, Object expected) { | ||
return assertionFailedError(message, actual, expected).orElse(assertionError(message)); | ||
} | ||
|
||
private static Optional<AssertionError> assertionFailedError(String message, Object actual, Object expected) { | ||
try { | ||
Object o = constructorInvoker.newInstance("org.opentest4j.AssertionFailedError", | ||
MSG_ARG_TYPES_FOR_ASSERTION_FAILED_ERROR, | ||
message, | ||
expected, | ||
actual); | ||
if (o instanceof AssertionError) { | ||
AssertionError assertionError = (AssertionError) o; | ||
return Optional.of(assertionError); | ||
} | ||
// TODO: single return | ||
return Optional.empty(); | ||
} catch (Throwable e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private static AssertionError assertionError(String message) { | ||
return new AssertionError(message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters