ExceptionFactoryTest.java revision 2637d96c202372854a7c71466ddcc6e90fc4fc53
1package org.mockito.internal.junit;
2
3import static org.assertj.core.api.Assertions.assertThat;
4import static org.mockitoutil.ClassLoaders.excludingClassLoader;
5
6import java.lang.reflect.Method;
7import org.junit.BeforeClass;
8import org.junit.Test;
9import org.mockito.exceptions.verification.ArgumentsAreDifferent;
10
11public class ExceptionFactoryTest {
12
13    private static ClassLoader classLoaderWithoutJUnit = excludingClassLoader().withCodeSourceUrlOf(ExceptionFactory.class).without("org.junit", "junit").build();
14
15    /** loaded by the current current class loader */
16    private static Class<?> junitArgumentsAreDifferent;
17
18    /** loaded by the custom classloader {@value #classLoaderWithoutJUnit}, which excludes junit-classes */
19    private static Class<?> nonJunitArgumentsAreDifferent;
20
21    @BeforeClass
22    public static void init() throws ClassNotFoundException {
23        nonJunitArgumentsAreDifferent = classLoaderWithoutJUnit.loadClass(ArgumentsAreDifferent.class.getName());
24        junitArgumentsAreDifferent = org.mockito.exceptions.verification.junit.ArgumentsAreDifferent.class;
25    }
26
27    @Test
28    public void createArgumentsAreDifferentException_withoutJUnit() throws Exception {
29        Class<?> exceptionFactory = classLoaderWithoutJUnit.loadClass(ExceptionFactory.class.getName());
30
31        Method m = exceptionFactory.getDeclaredMethod("createArgumentsAreDifferentException", String.class, String.class, String.class);
32        Object e = m.invoke(null, "message", "wanted", "actual");
33
34        assertThat(e).isExactlyInstanceOf(nonJunitArgumentsAreDifferent);
35    }
36
37    @Test
38    public void createArgumentsAreDifferentException_withJUnit() throws Exception {
39        AssertionError e = ExceptionFactory.createArgumentsAreDifferentException("message", "wanted", "actual");
40
41        assertThat(e).isExactlyInstanceOf(junitArgumentsAreDifferent);
42    }
43
44    @Test
45    public void createArgumentsAreDifferentException_withJUnit2x() throws Exception {
46        AssertionError e;
47
48        e = ExceptionFactory.createArgumentsAreDifferentException("message", "wanted", "actual");
49        assertThat(e).isExactlyInstanceOf(junitArgumentsAreDifferent);
50
51        e = ExceptionFactory.createArgumentsAreDifferentException("message", "wanted", "actual");
52        assertThat(e).isExactlyInstanceOf(junitArgumentsAreDifferent);
53    }
54}
55