Skip to content

Commit 919a840

Browse files
committed
Fixing issue 14 (conflict with powermock on the classpath) by setting GwtMockito as the context class loader during initialization
1 parent d8f81bd commit 919a840

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

gwtmockito/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,13 @@
3636
<artifactId>javassist</artifactId>
3737
</dependency>
3838

39+
<!-- We don't actually depend on PowerMock, but we put it in the classpath
40+
for tests to ensure that it doesn't cause a conflict. See
41+
https://github.com/google/gwtmockito/issues/14. -->
42+
<dependency>
43+
<groupId>org.powermock</groupId>
44+
<artifactId>powermock-api-mockito</artifactId>
45+
</dependency>
46+
3947
</dependencies>
4048
</project>

gwtmockito/src/main/java/com/google/gwtmockito/GwtMockitoTestRunner.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ public GwtMockitoTestRunner(Class<?> unitTestClass) throws InitializationError {
118118
}
119119
gwtMockitoClassLoader = new GwtMockitoClassLoader(getParentClassloader(), classPool);
120120

121+
// Use this custom classloader as the context classloader during the rest of the initialization
122+
// process so that classes loaded via the context classloader will be compatible with the ones
123+
// used during test.
124+
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
125+
Thread.currentThread().setContextClassLoader(gwtMockitoClassLoader);
126+
121127
try {
122128
// Reload the test class with our own custom class loader that does things like remove
123129
// final modifiers, allowing GWT Elements to be mocked. Also load GwtMockito itself so we can
@@ -133,6 +139,8 @@ public GwtMockitoTestRunner(Class<?> unitTestClass) throws InitializationError {
133139
testClassField.set(this, new TestClass(customLoadedTestClass));
134140
} catch (Exception e) {
135141
throw new InitializationError(e);
142+
} finally {
143+
Thread.currentThread().setContextClassLoader(originalClassLoader);
136144
}
137145
}
138146

@@ -231,8 +239,11 @@ public void run(RunNotifier notifier) {
231239
// classloader than the class being mocked.
232240
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
233241
Thread.currentThread().setContextClassLoader(gwtMockitoClassLoader);
234-
super.run(notifier);
235-
Thread.currentThread().setContextClassLoader(originalClassLoader);
242+
try {
243+
super.run(notifier);
244+
} finally {
245+
Thread.currentThread().setContextClassLoader(originalClassLoader);
246+
}
236247
}
237248

238249
/**

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@
8080
<artifactId>gwtmockito</artifactId>
8181
<version>${project.version}</version>
8282
</dependency>
83+
84+
<!-- We don't actually depend on PowerMock, but we put it in the classpath
85+
for tests to ensure that it doesn't cause a conflict. See
86+
https://github.com/google/gwtmockito/issues/14. -->
87+
<dependency>
88+
<groupId>org.powermock</groupId>
89+
<artifactId>powermock-api-mockito</artifactId>
90+
<version>1.5.1</version>
91+
<scope>test</scope>
92+
</dependency>
8393
</dependencies>
8494
</dependencyManagement>
8595

0 commit comments

Comments
 (0)