diff --git a/core/build.gradle b/core/build.gradle index 28ca8b07..0a9bee79 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -34,6 +34,7 @@ dependencies { testImplementation "commons-lang:commons-lang" testImplementation "gsbase:gsbase" testImplementation "org.mockito:mockito-core" + testImplementation "org.mockito:mockito-inline" testImplementation "org.springframework:spring-test" testImplementation "org.assertj:assertj-core" } diff --git a/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTest.java b/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTest.java index abd0d22b..4c7b1072 100644 --- a/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTest.java +++ b/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTest.java @@ -37,6 +37,7 @@ import org.springframework.ldap.transaction.compensating.manager.TransactionAwar import org.springframework.ldap.transaction.compensating.support.DefaultTempEntryRenamingStrategy; import org.springframework.ldap.transaction.compensating.support.DifferentSubtreeTempEntryRenamingStrategy; import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.util.ReflectionUtils; import javax.management.MBeanServer; import javax.management.ObjectName; @@ -44,11 +45,11 @@ import javax.naming.CannotProceedException; import javax.naming.CommunicationException; import javax.naming.directory.SearchControls; import java.lang.management.ManagementFactory; +import java.lang.reflect.Field; import java.util.Set; import static org.junit.Assert.assertArrayEquals; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.internal.util.reflection.Whitebox.getInternalState; /** * @author Mattias Hellborg Arthursson @@ -73,7 +74,7 @@ public class LdapTemplateNamespaceHandlerTest { assertThat(new String[]{"ldap://localhost:389"}).isEqualTo((Object[]) getInternalState(contextSource, "urls")); assertThat(Boolean.FALSE).isEqualTo(getInternalState(contextSource, "pooled")); assertThat(Boolean.FALSE).isEqualTo(getInternalState(contextSource, "anonymousReadOnly")); - assertThat(getInternalState(contextSource, "referral")).isNull(); + assertThat((Object) getInternalState(contextSource, "referral")).isNull(); assertThat(outerContextSource).isSameAs(getInternalState(ldapTemplate, "contextSource")); assertThat(Boolean.FALSE).isEqualTo(getInternalState(ldapTemplate, "ignorePartialResultException")); @@ -183,7 +184,7 @@ public class LdapTemplateNamespaceHandlerTest { assertThat(outerContextSource instanceof TransactionAwareContextSourceProxy).isTrue(); ContextSource contextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget(); - assertArrayEquals(new String[] { "ldap://a.localhost:389", "ldap://b.localhost:389" }, + assertArrayEquals(new String[] { "ldap://a.localhost:389", "ldap://b.localhost:389" }, (Object[]) getInternalState(contextSource, "urls")); } @@ -283,8 +284,8 @@ public class LdapTemplateNamespaceHandlerTest { assertThat(pooledContextSource instanceof PoolingContextSource).isTrue(); Object objectFactory = getInternalState(pooledContextSource, "dirContextPoolableObjectFactory"); - assertThat(getInternalState(objectFactory, "contextSource")).isNotNull(); - assertThat(getInternalState(objectFactory, "dirContextValidator")).isNull(); + assertThat((Object) getInternalState(objectFactory, "contextSource")).isNotNull(); + assertThat((Object) getInternalState(objectFactory, "dirContextValidator")).isNull(); Set> nonTransientExceptions = (Set>) getInternalState(objectFactory, "nonTransientExceptions"); assertThat(nonTransientExceptions).hasSize(1); @@ -366,11 +367,11 @@ public class LdapTemplateNamespaceHandlerTest { ContextSource pooledContextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget(); assertThat(pooledContextSource).isNotNull(); assertThat(pooledContextSource instanceof PooledContextSource).isTrue(); - assertThat(getInternalState(pooledContextSource, "poolConfig")).isNotNull(); + assertThat((Object) getInternalState(pooledContextSource, "poolConfig")).isNotNull(); Object objectFactory = getInternalState(pooledContextSource, "dirContextPooledObjectFactory"); - assertThat(getInternalState(objectFactory, "contextSource")).isNotNull(); - assertThat(getInternalState(objectFactory, "dirContextValidator")).isNull(); + assertThat((Object) getInternalState(objectFactory, "contextSource")).isNotNull(); + assertThat((Object) getInternalState(objectFactory, "dirContextValidator")).isNull(); Set> nonTransientExceptions = (Set>) getInternalState(objectFactory, "nonTransientExceptions"); assertThat(nonTransientExceptions).hasSize(1); @@ -523,4 +524,10 @@ public class LdapTemplateNamespaceHandlerTest { assertThat(objectPool.getMaxTotalPerKey()).isEqualTo(14); assertThat(objectPool.getNumTestsPerEvictionRun()).isEqualTo(18); } + + private T getInternalState(Object target, String fieldName) { + Field field = ReflectionUtils.findField(target.getClass(), fieldName); + field.setAccessible(true); + return (T) ReflectionUtils.getField(field, target); + } } diff --git a/core/src/test/java/org/springframework/ldap/core/support/SingleContextSourceTest.java b/core/src/test/java/org/springframework/ldap/core/support/SingleContextSourceTest.java index 6fbc7b44..461f0adc 100644 --- a/core/src/test/java/org/springframework/ldap/core/support/SingleContextSourceTest.java +++ b/core/src/test/java/org/springframework/ldap/core/support/SingleContextSourceTest.java @@ -18,14 +18,15 @@ package org.springframework.ldap.core.support; import org.junit.Before; import org.junit.Test; -import org.mockito.internal.util.reflection.Whitebox; import org.springframework.ldap.core.ContextExecutor; import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.core.LdapOperations; +import org.springframework.util.ReflectionUtils; import javax.naming.NamingException; import javax.naming.directory.DirContext; +import java.lang.reflect.Field; import java.lang.reflect.Proxy; import static org.assertj.core.api.Assertions.assertThat; @@ -58,7 +59,7 @@ public class SingleContextSourceTest { operations.executeReadOnly(new ContextExecutor() { @Override public Object executeWithContext(DirContext ctx) throws NamingException { - Object targetContex = Whitebox.getInternalState(Proxy.getInvocationHandler(ctx), "target"); + Object targetContex = getInternalState(Proxy.getInvocationHandler(ctx), "target"); assertThat(targetContex).isSameAs(dirContextMock); return false; } @@ -69,7 +70,7 @@ public class SingleContextSourceTest { operations.executeReadOnly(new ContextExecutor() { @Override public Object executeWithContext(DirContext ctx) throws NamingException { - Object targetContex = Whitebox.getInternalState(Proxy.getInvocationHandler(ctx), "target"); + Object targetContex = getInternalState(Proxy.getInvocationHandler(ctx), "target"); assertThat(targetContex).isSameAs(dirContextMock); return false; } @@ -80,4 +81,9 @@ public class SingleContextSourceTest { }); } + private T getInternalState(Object target, String fieldName) { + Field field = ReflectionUtils.findField(target.getClass(), fieldName); + field.setAccessible(true); + return (T) ReflectionUtils.getField(field, target); + } } diff --git a/core/src/test/java/org/springframework/ldap/odm/core/impl/DefaultObjectDirectoryMapperTest.java b/core/src/test/java/org/springframework/ldap/odm/core/impl/DefaultObjectDirectoryMapperTest.java index 0d760fef..051e93dd 100644 --- a/core/src/test/java/org/springframework/ldap/odm/core/impl/DefaultObjectDirectoryMapperTest.java +++ b/core/src/test/java/org/springframework/ldap/odm/core/impl/DefaultObjectDirectoryMapperTest.java @@ -1,8 +1,6 @@ package org.springframework.ldap.odm.core.impl; import static org.assertj.core.api.Assertions.assertThat; -import static org.powermock.api.mockito.PowerMockito.spy; -import static org.powermock.api.mockito.PowerMockito.when; import static org.springframework.ldap.query.LdapQueryBuilder.query; import java.lang.reflect.Field; @@ -13,18 +11,18 @@ import javax.naming.Name; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.internal.util.reflection.Whitebox; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; import org.springframework.core.SpringVersion; import org.springframework.ldap.support.LdapUtils; +import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; /** * @author Mattias Hellborg Arthursson */ -@RunWith(PowerMockRunner.class) -@PrepareForTest(SpringVersion.class) +@RunWith(MockitoJUnitRunner.class) public class DefaultObjectDirectoryMapperTest { private DefaultObjectDirectoryMapper tested; @@ -37,13 +35,12 @@ public class DefaultObjectDirectoryMapperTest { // LDAP-295 @Test public void springVersionIsNull() { - spy(SpringVersion.class); - when(SpringVersion.getVersion()).thenReturn(null); - - DefaultObjectDirectoryMapper mapper = new DefaultObjectDirectoryMapper(); - - // LDAP-300 - assertThat(Whitebox.getInternalState(mapper,"converterManager")).isNotNull(); + try (MockedStatic version = Mockito.mockStatic(SpringVersion.class)) { + version.when(SpringVersion::getVersion).thenReturn(null); + DefaultObjectDirectoryMapper mapper = new DefaultObjectDirectoryMapper(); + // LDAP-300 + assertThat((Object) getInternalState(mapper,"converterManager")).isNotNull(); + } } @Test @@ -140,4 +137,10 @@ public class DefaultObjectDirectoryMapperTest { } } } + + private T getInternalState(Object target, String fieldName) { + Field field = ReflectionUtils.findField(target.getClass(), fieldName); + field.setAccessible(true); + return (T) ReflectionUtils.getField(field, target); + } } diff --git a/core/src/test/java/org/springframework/ldap/pool/factory/DirContextPoolableObjectFactoryTest.java b/core/src/test/java/org/springframework/ldap/pool/factory/DirContextPoolableObjectFactoryTest.java index e1d0caa8..b8d3f19a 100644 --- a/core/src/test/java/org/springframework/ldap/pool/factory/DirContextPoolableObjectFactoryTest.java +++ b/core/src/test/java/org/springframework/ldap/pool/factory/DirContextPoolableObjectFactoryTest.java @@ -17,13 +17,15 @@ package org.springframework.ldap.pool.factory; import org.junit.Test; import org.mockito.Mockito; -import org.mockito.internal.util.reflection.Whitebox; import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.pool.AbstractPoolTestCase; import org.springframework.ldap.pool.DirContextType; import org.springframework.ldap.pool.validation.DirContextValidator; +import org.springframework.util.ReflectionUtils; import javax.naming.directory.DirContext; + +import java.lang.reflect.Field; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; @@ -101,7 +103,7 @@ public class DirContextPoolableObjectFactoryTest extends AbstractPoolTestCase { final Object createdDirContext = objectFactory.makeObject(DirContextType.READ_ONLY); InvocationHandler invocationHandler = Proxy.getInvocationHandler(createdDirContext); - assertThat(readOnlyContextMock).isEqualTo(Whitebox.getInternalState(invocationHandler, "target")); + assertThat(readOnlyContextMock).isEqualTo(getInternalState(invocationHandler, "target")); } @Test @@ -116,7 +118,7 @@ public class DirContextPoolableObjectFactoryTest extends AbstractPoolTestCase { final Object createdDirContext = objectFactory.makeObject(DirContextType.READ_WRITE); InvocationHandler invocationHandler = Proxy.getInvocationHandler(createdDirContext); - assertThat(readWriteContextMock).isEqualTo(Whitebox.getInternalState(invocationHandler, "target")); + assertThat(readWriteContextMock).isEqualTo(getInternalState(invocationHandler, "target")); } @Test @@ -217,4 +219,10 @@ public class DirContextPoolableObjectFactoryTest extends AbstractPoolTestCase { objectFactory.destroyObject(DirContextType.READ_ONLY, throwingDirContextMock); verify(dirContextMock).close(); } + + private T getInternalState(Object target, String fieldName) { + Field field = ReflectionUtils.findField(target.getClass(), fieldName); + field.setAccessible(true); + return (T) ReflectionUtils.getField(field, target); + } } diff --git a/core/src/test/java/org/springframework/ldap/pool2/factory/DirContextPooledObjectFactoryTest.java b/core/src/test/java/org/springframework/ldap/pool2/factory/DirContextPooledObjectFactoryTest.java index 0adb5e27..be940810 100644 --- a/core/src/test/java/org/springframework/ldap/pool2/factory/DirContextPooledObjectFactoryTest.java +++ b/core/src/test/java/org/springframework/ldap/pool2/factory/DirContextPooledObjectFactoryTest.java @@ -18,13 +18,15 @@ package org.springframework.ldap.pool2.factory; import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.impl.DefaultPooledObject; import org.junit.Test; -import org.mockito.internal.util.reflection.Whitebox; import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.pool2.DirContextType; import org.springframework.ldap.pool2.validation.DirContextValidator; import org.springframework.ldap.pool2.AbstractPoolTestCase; +import org.springframework.util.ReflectionUtils; import javax.naming.directory.DirContext; + +import java.lang.reflect.Field; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; @@ -103,7 +105,7 @@ public class DirContextPooledObjectFactoryTest extends AbstractPoolTestCase { final PooledObject createdDirContext = objectFactory.makeObject(DirContextType.READ_ONLY); InvocationHandler invocationHandler = Proxy.getInvocationHandler(createdDirContext.getObject()); - assertThat(readOnlyContextMock).isEqualTo(Whitebox.getInternalState(invocationHandler, "target")); + assertThat(readOnlyContextMock).isEqualTo(getInternalState(invocationHandler, "target")); } @Test @@ -118,7 +120,7 @@ public class DirContextPooledObjectFactoryTest extends AbstractPoolTestCase { final PooledObject createdDirContext = objectFactory.makeObject(DirContextType.READ_WRITE); InvocationHandler invocationHandler = Proxy.getInvocationHandler(createdDirContext.getObject()); - assertThat(readWriteContextMock).isEqualTo(Whitebox.getInternalState(invocationHandler, "target")); + assertThat(readWriteContextMock).isEqualTo(getInternalState(invocationHandler, "target")); } @Test @@ -227,4 +229,10 @@ public class DirContextPooledObjectFactoryTest extends AbstractPoolTestCase { objectFactory.destroyObject(DirContextType.READ_ONLY, pooledObject); verify(dirContextMock).close(); } + + private T getInternalState(Object target, String fieldName) { + Field field = ReflectionUtils.findField(target.getClass(), fieldName); + field.setAccessible(true); + return (T) ReflectionUtils.getField(field, target); + } }