Skip to content

Commit

Permalink
Simplify the creation of ArgumentsAreDifferent-Exceptions (mockito#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianSchwarz authored and TimvdLippe committed Feb 21, 2017
1 parent 1cbc7f8 commit 8066998
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 78 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/mockito/internal/exceptions/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.mockito.exceptions.verification.*;
import org.mockito.internal.debugging.LocationImpl;
import org.mockito.internal.exceptions.util.ScenarioPrinter;
import org.mockito.internal.junit.JUnitTool;
import org.mockito.internal.junit.ExceptionFactory;
import org.mockito.internal.matchers.LocalizedMatcher;
import org.mockito.internal.util.MockUtil;
import org.mockito.invocation.DescribedInvocation;
Expand Down Expand Up @@ -301,7 +301,7 @@ public static AssertionError argumentsAreDifferent(String wanted, String actual,
""
);

return JUnitTool.createArgumentsAreDifferentException(message, wanted, actual);
return ExceptionFactory.createArgumentsAreDifferentException(message, wanted, actual);
}

public static MockitoAssertionError wantedButNotInvoked(DescribedInvocation wanted) {
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/org/mockito/internal/junit/ExceptionFactory.java
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);
}
}
}

This file was deleted.

23 changes: 0 additions & 23 deletions src/main/java/org/mockito/internal/junit/JUnitDetecter.java

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/java/org/mockito/internal/junit/JUnitTool.java

This file was deleted.

54 changes: 54 additions & 0 deletions src/test/java/org/mockito/internal/junit/ExceptionFactoryTest.java
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);
}
}

0 comments on commit 8066998

Please sign in to comment.