Skip to content

Commit 20ffda2

Browse files
committed
Remove PowerMock
Issue gh-611
1 parent c376c57 commit 20ffda2

File tree

6 files changed

+64
-31
lines changed

6 files changed

+64
-31
lines changed

core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dependencies {
3434
testImplementation "commons-lang:commons-lang"
3535
testImplementation "gsbase:gsbase"
3636
testImplementation "org.mockito:mockito-core"
37+
testImplementation "org.mockito:mockito-inline"
3738
testImplementation "org.springframework:spring-test"
3839
testImplementation "org.assertj:assertj-core"
3940
}

core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTest.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,19 @@
3737
import org.springframework.ldap.transaction.compensating.support.DefaultTempEntryRenamingStrategy;
3838
import org.springframework.ldap.transaction.compensating.support.DifferentSubtreeTempEntryRenamingStrategy;
3939
import org.springframework.transaction.PlatformTransactionManager;
40+
import org.springframework.util.ReflectionUtils;
4041

4142
import javax.management.MBeanServer;
4243
import javax.management.ObjectName;
4344
import javax.naming.CannotProceedException;
4445
import javax.naming.CommunicationException;
4546
import javax.naming.directory.SearchControls;
4647
import java.lang.management.ManagementFactory;
48+
import java.lang.reflect.Field;
4749
import java.util.Set;
4850

4951
import static org.junit.Assert.assertArrayEquals;
5052
import static org.assertj.core.api.Assertions.assertThat;
51-
import static org.mockito.internal.util.reflection.Whitebox.getInternalState;
5253

5354
/**
5455
* @author Mattias Hellborg Arthursson
@@ -73,7 +74,7 @@ public void verifyParseWithDefaultValues() {
7374
assertThat(new String[]{"ldap://localhost:389"}).isEqualTo((Object[]) getInternalState(contextSource, "urls"));
7475
assertThat(Boolean.FALSE).isEqualTo(getInternalState(contextSource, "pooled"));
7576
assertThat(Boolean.FALSE).isEqualTo(getInternalState(contextSource, "anonymousReadOnly"));
76-
assertThat(getInternalState(contextSource, "referral")).isNull();
77+
assertThat((Object) getInternalState(contextSource, "referral")).isNull();
7778

7879
assertThat(outerContextSource).isSameAs(getInternalState(ldapTemplate, "contextSource"));
7980
assertThat(Boolean.FALSE).isEqualTo(getInternalState(ldapTemplate, "ignorePartialResultException"));
@@ -183,7 +184,7 @@ public void supportsSpelMultiUrls() {
183184
assertThat(outerContextSource instanceof TransactionAwareContextSourceProxy).isTrue();
184185
ContextSource contextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
185186

186-
assertArrayEquals(new String[] { "ldap://a.localhost:389", "ldap://b.localhost:389" },
187+
assertArrayEquals(new String[] { "ldap://a.localhost:389", "ldap://b.localhost:389" },
187188
(Object[]) getInternalState(contextSource, "urls"));
188189

189190
}
@@ -283,8 +284,8 @@ public void verifyParsePoolingDefaults() {
283284
assertThat(pooledContextSource instanceof PoolingContextSource).isTrue();
284285

285286
Object objectFactory = getInternalState(pooledContextSource, "dirContextPoolableObjectFactory");
286-
assertThat(getInternalState(objectFactory, "contextSource")).isNotNull();
287-
assertThat(getInternalState(objectFactory, "dirContextValidator")).isNull();
287+
assertThat((Object) getInternalState(objectFactory, "contextSource")).isNotNull();
288+
assertThat((Object) getInternalState(objectFactory, "dirContextValidator")).isNull();
288289
Set<Class<? extends Throwable>> nonTransientExceptions =
289290
(Set<Class<? extends Throwable>>) getInternalState(objectFactory, "nonTransientExceptions");
290291
assertThat(nonTransientExceptions).hasSize(1);
@@ -366,11 +367,11 @@ public void verifyParsePooling2Defaults() {
366367
ContextSource pooledContextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
367368
assertThat(pooledContextSource).isNotNull();
368369
assertThat(pooledContextSource instanceof PooledContextSource).isTrue();
369-
assertThat(getInternalState(pooledContextSource, "poolConfig")).isNotNull();
370+
assertThat((Object) getInternalState(pooledContextSource, "poolConfig")).isNotNull();
370371

371372
Object objectFactory = getInternalState(pooledContextSource, "dirContextPooledObjectFactory");
372-
assertThat(getInternalState(objectFactory, "contextSource")).isNotNull();
373-
assertThat(getInternalState(objectFactory, "dirContextValidator")).isNull();
373+
assertThat((Object) getInternalState(objectFactory, "contextSource")).isNotNull();
374+
assertThat((Object) getInternalState(objectFactory, "dirContextValidator")).isNull();
374375
Set<Class<? extends Throwable>> nonTransientExceptions =
375376
(Set<Class<? extends Throwable>>) getInternalState(objectFactory, "nonTransientExceptions");
376377
assertThat(nonTransientExceptions).hasSize(1);
@@ -523,4 +524,10 @@ public void verifyParsePool2WithPlaceholders() {
523524
assertThat(objectPool.getMaxTotalPerKey()).isEqualTo(14);
524525
assertThat(objectPool.getNumTestsPerEvictionRun()).isEqualTo(18);
525526
}
527+
528+
private <T> T getInternalState(Object target, String fieldName) {
529+
Field field = ReflectionUtils.findField(target.getClass(), fieldName);
530+
field.setAccessible(true);
531+
return (T) ReflectionUtils.getField(field, target);
532+
}
526533
}

core/src/test/java/org/springframework/ldap/core/support/SingleContextSourceTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818

1919
import org.junit.Before;
2020
import org.junit.Test;
21-
import org.mockito.internal.util.reflection.Whitebox;
2221
import org.springframework.ldap.core.ContextExecutor;
2322
import org.springframework.ldap.core.ContextSource;
2423
import org.springframework.ldap.core.LdapOperations;
24+
import org.springframework.util.ReflectionUtils;
2525

2626
import javax.naming.NamingException;
2727
import javax.naming.directory.DirContext;
2828

29+
import java.lang.reflect.Field;
2930
import java.lang.reflect.Proxy;
3031

3132
import static org.assertj.core.api.Assertions.assertThat;
@@ -58,7 +59,7 @@ public Object doWithLdapOperations(LdapOperations operations) {
5859
operations.executeReadOnly(new ContextExecutor<Object>() {
5960
@Override
6061
public Object executeWithContext(DirContext ctx) throws NamingException {
61-
Object targetContex = Whitebox.getInternalState(Proxy.getInvocationHandler(ctx), "target");
62+
Object targetContex = getInternalState(Proxy.getInvocationHandler(ctx), "target");
6263
assertThat(targetContex).isSameAs(dirContextMock);
6364
return false;
6465
}
@@ -69,7 +70,7 @@ public Object executeWithContext(DirContext ctx) throws NamingException {
6970
operations.executeReadOnly(new ContextExecutor<Object>() {
7071
@Override
7172
public Object executeWithContext(DirContext ctx) throws NamingException {
72-
Object targetContex = Whitebox.getInternalState(Proxy.getInvocationHandler(ctx), "target");
73+
Object targetContex = getInternalState(Proxy.getInvocationHandler(ctx), "target");
7374
assertThat(targetContex).isSameAs(dirContextMock);
7475
return false;
7576
}
@@ -80,4 +81,9 @@ public Object executeWithContext(DirContext ctx) throws NamingException {
8081
});
8182
}
8283

84+
private <T> T getInternalState(Object target, String fieldName) {
85+
Field field = ReflectionUtils.findField(target.getClass(), fieldName);
86+
field.setAccessible(true);
87+
return (T) ReflectionUtils.getField(field, target);
88+
}
8389
}

core/src/test/java/org/springframework/ldap/odm/core/impl/DefaultObjectDirectoryMapperTest.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.springframework.ldap.odm.core.impl;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4-
import static org.powermock.api.mockito.PowerMockito.spy;
5-
import static org.powermock.api.mockito.PowerMockito.when;
64
import static org.springframework.ldap.query.LdapQueryBuilder.query;
75

86
import java.lang.reflect.Field;
@@ -13,18 +11,18 @@
1311
import org.junit.Before;
1412
import org.junit.Test;
1513
import org.junit.runner.RunWith;
16-
import org.mockito.internal.util.reflection.Whitebox;
17-
import org.powermock.core.classloader.annotations.PrepareForTest;
18-
import org.powermock.modules.junit4.PowerMockRunner;
14+
import org.mockito.MockedStatic;
15+
import org.mockito.Mockito;
16+
import org.mockito.junit.MockitoJUnitRunner;
1917
import org.springframework.core.SpringVersion;
2018
import org.springframework.ldap.support.LdapUtils;
19+
import org.springframework.util.ReflectionUtils;
2120
import org.springframework.util.StringUtils;
2221

2322
/**
2423
* @author Mattias Hellborg Arthursson
2524
*/
26-
@RunWith(PowerMockRunner.class)
27-
@PrepareForTest(SpringVersion.class)
25+
@RunWith(MockitoJUnitRunner.class)
2826
public class DefaultObjectDirectoryMapperTest {
2927

3028
private DefaultObjectDirectoryMapper tested;
@@ -37,13 +35,12 @@ public void prepareTestedInstance() {
3735
// LDAP-295
3836
@Test
3937
public void springVersionIsNull() {
40-
spy(SpringVersion.class);
41-
when(SpringVersion.getVersion()).thenReturn(null);
42-
43-
DefaultObjectDirectoryMapper mapper = new DefaultObjectDirectoryMapper();
44-
45-
// LDAP-300
46-
assertThat(Whitebox.getInternalState(mapper,"converterManager")).isNotNull();
38+
try (MockedStatic<SpringVersion> version = Mockito.mockStatic(SpringVersion.class)) {
39+
version.when(SpringVersion::getVersion).thenReturn(null);
40+
DefaultObjectDirectoryMapper mapper = new DefaultObjectDirectoryMapper();
41+
// LDAP-300
42+
assertThat((Object) getInternalState(mapper,"converterManager")).isNotNull();
43+
}
4744
}
4845

4946
@Test
@@ -140,4 +137,10 @@ private void assertField(DefaultObjectDirectoryMapper.EntityData entityData,
140137
}
141138
}
142139
}
140+
141+
private <T> T getInternalState(Object target, String fieldName) {
142+
Field field = ReflectionUtils.findField(target.getClass(), fieldName);
143+
field.setAccessible(true);
144+
return (T) ReflectionUtils.getField(field, target);
145+
}
143146
}

core/src/test/java/org/springframework/ldap/pool/factory/DirContextPoolableObjectFactoryTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
import org.junit.Test;
1919
import org.mockito.Mockito;
20-
import org.mockito.internal.util.reflection.Whitebox;
2120
import org.springframework.ldap.core.ContextSource;
2221
import org.springframework.ldap.pool.AbstractPoolTestCase;
2322
import org.springframework.ldap.pool.DirContextType;
2423
import org.springframework.ldap.pool.validation.DirContextValidator;
24+
import org.springframework.util.ReflectionUtils;
2525

2626
import javax.naming.directory.DirContext;
27+
28+
import java.lang.reflect.Field;
2729
import java.lang.reflect.InvocationHandler;
2830
import java.lang.reflect.Proxy;
2931

@@ -101,7 +103,7 @@ public void testMakeObjectReadOnly() throws Exception {
101103

102104
final Object createdDirContext = objectFactory.makeObject(DirContextType.READ_ONLY);
103105
InvocationHandler invocationHandler = Proxy.getInvocationHandler(createdDirContext);
104-
assertThat(readOnlyContextMock).isEqualTo(Whitebox.getInternalState(invocationHandler, "target"));
106+
assertThat(readOnlyContextMock).isEqualTo(getInternalState(invocationHandler, "target"));
105107
}
106108

107109
@Test
@@ -116,7 +118,7 @@ public void testMakeObjectReadWrite() throws Exception {
116118
final Object createdDirContext = objectFactory.makeObject(DirContextType.READ_WRITE);
117119

118120
InvocationHandler invocationHandler = Proxy.getInvocationHandler(createdDirContext);
119-
assertThat(readWriteContextMock).isEqualTo(Whitebox.getInternalState(invocationHandler, "target"));
121+
assertThat(readWriteContextMock).isEqualTo(getInternalState(invocationHandler, "target"));
120122
}
121123

122124
@Test
@@ -217,4 +219,10 @@ public void testDestroyObject() throws Exception {
217219
objectFactory.destroyObject(DirContextType.READ_ONLY, throwingDirContextMock);
218220
verify(dirContextMock).close();
219221
}
222+
223+
private <T> T getInternalState(Object target, String fieldName) {
224+
Field field = ReflectionUtils.findField(target.getClass(), fieldName);
225+
field.setAccessible(true);
226+
return (T) ReflectionUtils.getField(field, target);
227+
}
220228
}

core/src/test/java/org/springframework/ldap/pool2/factory/DirContextPooledObjectFactoryTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
import org.apache.commons.pool2.PooledObject;
1919
import org.apache.commons.pool2.impl.DefaultPooledObject;
2020
import org.junit.Test;
21-
import org.mockito.internal.util.reflection.Whitebox;
2221
import org.springframework.ldap.core.ContextSource;
2322
import org.springframework.ldap.pool2.DirContextType;
2423
import org.springframework.ldap.pool2.validation.DirContextValidator;
2524
import org.springframework.ldap.pool2.AbstractPoolTestCase;
25+
import org.springframework.util.ReflectionUtils;
2626

2727
import javax.naming.directory.DirContext;
28+
29+
import java.lang.reflect.Field;
2830
import java.lang.reflect.InvocationHandler;
2931
import java.lang.reflect.Proxy;
3032

@@ -103,7 +105,7 @@ public void testMakeObjectReadOnly() throws Exception {
103105

104106
final PooledObject createdDirContext = objectFactory.makeObject(DirContextType.READ_ONLY);
105107
InvocationHandler invocationHandler = Proxy.getInvocationHandler(createdDirContext.getObject());
106-
assertThat(readOnlyContextMock).isEqualTo(Whitebox.getInternalState(invocationHandler, "target"));
108+
assertThat(readOnlyContextMock).isEqualTo(getInternalState(invocationHandler, "target"));
107109
}
108110

109111
@Test
@@ -118,7 +120,7 @@ public void testMakeObjectReadWrite() throws Exception {
118120
final PooledObject createdDirContext = objectFactory.makeObject(DirContextType.READ_WRITE);
119121

120122
InvocationHandler invocationHandler = Proxy.getInvocationHandler(createdDirContext.getObject());
121-
assertThat(readWriteContextMock).isEqualTo(Whitebox.getInternalState(invocationHandler, "target"));
123+
assertThat(readWriteContextMock).isEqualTo(getInternalState(invocationHandler, "target"));
122124
}
123125

124126
@Test
@@ -227,4 +229,10 @@ public void testDestroyObject() throws Exception {
227229
objectFactory.destroyObject(DirContextType.READ_ONLY, pooledObject);
228230
verify(dirContextMock).close();
229231
}
232+
233+
private <T> T getInternalState(Object target, String fieldName) {
234+
Field field = ReflectionUtils.findField(target.getClass(), fieldName);
235+
field.setAccessible(true);
236+
return (T) ReflectionUtils.getField(field, target);
237+
}
230238
}

0 commit comments

Comments
 (0)