forked from mockito/mockito
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify the creation of ArgumentsAreDifferent-Exceptions (mockito#908)
- Loading branch information
1 parent
1cbc7f8
commit 8066998
Showing
6 changed files
with
108 additions
and
78 deletions.
There are no files selected for viewing
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
52 changes: 52 additions & 0 deletions
52
src/main/java/org/mockito/internal/junit/ExceptionFactory.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,52 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito.internal.junit; | ||
|
||
import junit.framework.ComparisonFailure; | ||
import org.mockito.exceptions.verification.ArgumentsAreDifferent; | ||
|
||
public class ExceptionFactory { | ||
|
||
private final static boolean hasJUnit = canLoadJunitClass(); | ||
|
||
private ExceptionFactory() { | ||
} | ||
|
||
/** | ||
* If JUnit is used, an AssertionError is returned that extends from JUnit {@link ComparisonFailure} and hence provide a better IDE support as the comparison result is comparable | ||
*/ | ||
public static AssertionError createArgumentsAreDifferentException(String message, String wanted, String actual) { | ||
if (hasJUnit) { | ||
return createJUnitArgumentsAreDifferent(message, wanted, actual); | ||
} | ||
return new ArgumentsAreDifferent(message); | ||
} | ||
|
||
private static AssertionError createJUnitArgumentsAreDifferent(String message, String wanted, String actual) { | ||
return JUnitArgsAreDifferent.create(message, wanted, actual); | ||
} | ||
|
||
private static boolean canLoadJunitClass() { | ||
try { | ||
JUnitArgsAreDifferent.create("message", "wanted", "actual"); | ||
} catch (NoClassDefFoundError onlyIfJUnitIsNotAvailable) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Don't inline this class! It allows create the JUnit-ArgumentsAreDifferent exception without the need to use reflection. | ||
* <p> | ||
* If JUnit is not available a call to {@link #create(String, String, String)} will throw a {@link NoClassDefFoundError}. | ||
* The {@link NoClassDefFoundError} will be thrown by the class loader cause the JUnit class {@link ComparisonFailure} | ||
* can't be loaded which is a upper class of ArgumentsAreDifferent. | ||
*/ | ||
private static class JUnitArgsAreDifferent { | ||
static AssertionError create(String message, String wanted, String actual) { | ||
return new org.mockito.exceptions.verification.junit.ArgumentsAreDifferent(message, wanted, actual); | ||
} | ||
} | ||
} |
35 changes: 0 additions & 35 deletions
35
src/main/java/org/mockito/internal/junit/FriendlyExceptionMaker.java
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
src/main/java/org/mockito/internal/junit/JUnitDetecter.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
src/test/java/org/mockito/internal/junit/ExceptionFactoryTest.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,54 @@ | ||
package org.mockito.internal.junit; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockitoutil.ClassLoaders.excludingClassLoader; | ||
|
||
import java.lang.reflect.Method; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.mockito.exceptions.verification.ArgumentsAreDifferent; | ||
|
||
public class ExceptionFactoryTest { | ||
|
||
private static ClassLoader classLoaderWithoutJUnit = excludingClassLoader().withCodeSourceUrlOf(ExceptionFactory.class).without("org.junit", "junit").build(); | ||
|
||
/** loaded by the current current class loader */ | ||
private static Class<?> junitArgumentsAreDifferent; | ||
|
||
/** loaded by the custom classloader {@value #classLoaderWithoutJUnit}, which excludes junit-classes */ | ||
private static Class<?> nonJunitArgumentsAreDifferent; | ||
|
||
@BeforeClass | ||
public static void init() throws ClassNotFoundException { | ||
nonJunitArgumentsAreDifferent = classLoaderWithoutJUnit.loadClass(ArgumentsAreDifferent.class.getName()); | ||
junitArgumentsAreDifferent = org.mockito.exceptions.verification.junit.ArgumentsAreDifferent.class; | ||
} | ||
|
||
@Test | ||
public void createArgumentsAreDifferentException_withoutJUnit() throws Exception { | ||
Class<?> exceptionFactory = classLoaderWithoutJUnit.loadClass(ExceptionFactory.class.getName()); | ||
|
||
Method m = exceptionFactory.getDeclaredMethod("createArgumentsAreDifferentException", String.class, String.class, String.class); | ||
Object e = m.invoke(null, "message", "wanted", "actual"); | ||
|
||
assertThat(e).isExactlyInstanceOf(nonJunitArgumentsAreDifferent); | ||
} | ||
|
||
@Test | ||
public void createArgumentsAreDifferentException_withJUnit() throws Exception { | ||
AssertionError e = ExceptionFactory.createArgumentsAreDifferentException("message", "wanted", "actual"); | ||
|
||
assertThat(e).isExactlyInstanceOf(junitArgumentsAreDifferent); | ||
} | ||
|
||
@Test | ||
public void createArgumentsAreDifferentException_withJUnit2x() throws Exception { | ||
AssertionError e; | ||
|
||
e = ExceptionFactory.createArgumentsAreDifferentException("message", "wanted", "actual"); | ||
assertThat(e).isExactlyInstanceOf(junitArgumentsAreDifferent); | ||
|
||
e = ExceptionFactory.createArgumentsAreDifferentException("message", "wanted", "actual"); | ||
assertThat(e).isExactlyInstanceOf(junitArgumentsAreDifferent); | ||
} | ||
} |