forked from mockito/mockito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PackagePrivateWithContextClassLoaderTest.java
70 lines (55 loc) · 2.11 KB
/
PackagePrivateWithContextClassLoaderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Copyright (c) 2017 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.bugs.creation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/** Regression tests for issue #2303*/
public class PackagePrivateWithContextClassLoaderTest {
private ClassLoader oldContextClassloader;
public static class PublicClass {
int packagePrivateMethod() {
return 0;
}
}
public interface PublicInterface {}
interface PackagePrivateInterface {}
static class PackagePrivateClass {}
@Before
public void setUp() {
oldContextClassloader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(new ClassLoader(oldContextClassloader) {});
}
@After
public void teardown() {
Thread.currentThread().setContextClassLoader(oldContextClassloader);
}
@Test
public void should_be_able_to_mock_package_private_method() throws Exception {
PublicClass publicClass = mock(PublicClass.class);
when(publicClass.packagePrivateMethod()).thenReturn(3);
assertThat(publicClass.packagePrivateMethod()).isEqualTo(3);
}
@Test
public void should_be_able_to_mock_package_private_class() throws Exception {
PackagePrivateClass mock = mock(PackagePrivateClass.class);
}
@Test
public void should_be_able_to_mock_package_private_interface() throws Exception {
PackagePrivateInterface mock = mock(PackagePrivateInterface.class);
}
@Test
public void should_be_able_to_mock_package_private_extra_interface() throws Exception {
PackagePrivateInterface mock =
(PackagePrivateInterface)
mock(
PublicInterface.class,
withSettings().extraInterfaces(PackagePrivateInterface.class));
}
}