diff --git a/pom.xml b/pom.xml
index ad76c5c70..6e2ac5100 100644
--- a/pom.xml
+++ b/pom.xml
@@ -350,6 +350,13 @@
4.13.2
test
+
+
+ org.mockito
+ mockito-core
+ 3.9.0
+ test
+
diff --git a/src/test/java/org/apache/commons/beanutils2/SuppressPropertiesBeanIntrospectorTestCase.java b/src/test/java/org/apache/commons/beanutils2/SuppressPropertiesBeanIntrospectorTestCase.java
index 354ceaa97..52a77eb11 100644
--- a/src/test/java/org/apache/commons/beanutils2/SuppressPropertiesBeanIntrospectorTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/SuppressPropertiesBeanIntrospectorTestCase.java
@@ -24,6 +24,10 @@
import java.util.Set;
import junit.framework.TestCase;
+import org.mockito.Mockito;
+import org.mockito.ArgumentCaptor;
+import static org.mockito.Mockito.*;
+import org.apache.commons.beanutils2.IntrospectionContext;
/**
* Test class for {@code SuppressPropertiesBeanIntrospector}.
@@ -49,14 +53,35 @@ public void testRemovePropertiesDuringIntrospection() throws IntrospectionExcept
final String[] properties = { "test", "other", "oneMore" };
final SuppressPropertiesBeanIntrospector introspector = new SuppressPropertiesBeanIntrospector(
Arrays.asList(properties));
- final IntrospectionContextTestImpl context = new IntrospectionContextTestImpl();
+ // Create variables for tracking behaviors of mock object
+ Set contextRemovedProperties = new HashSet<>();
+ // Construct mock object
+ final IntrospectionContext context = mock(IntrospectionContext.class);
+ // Method Stubs
+ doThrow(new UnsupportedOperationException("Unexpected method call!")).when(context)
+ .addPropertyDescriptors(any(PropertyDescriptor[].class));
+ doThrow(new UnsupportedOperationException("Unexpected method call!")).when(context)
+ .addPropertyDescriptor(any(PropertyDescriptor.class));
+ doAnswer((stubInvo) -> {
+ String name = stubInvo.getArgument(0);
+ contextRemovedProperties.add(name);
+ return null;
+ }).when(context).removePropertyDescriptor(any(String.class));
+ when(context.getTargetClass()).thenAnswer((stubInvo) -> {
+ throw new UnsupportedOperationException("Unexpected method call!");
+ });
+ when(context.hasProperty(any(String.class)))
+ .thenThrow(new UnsupportedOperationException("Unexpected method call!"));
+ when(context.getPropertyDescriptor(any(String.class)))
+ .thenThrow(new UnsupportedOperationException("Unexpected method call!"));
+ when(context.propertyNames()).thenAnswer((stubInvo) -> {
+ throw new UnsupportedOperationException("Unexpected method call!");
+ });
introspector.introspect(context);
- assertEquals("Wrong number of removed properties", properties.length, context
- .getRemovedProperties().size());
+ assertEquals("Wrong number of removed properties", properties.length, contextRemovedProperties.size());
for (final String property : properties) {
- assertTrue("Property not removed: " + property, context
- .getRemovedProperties().contains(property));
+ assertTrue("Property not removed: " + property, contextRemovedProperties.contains(property));
}
}
@@ -70,13 +95,35 @@ public void testPropertyNamesDefensiveCopy() throws IntrospectionException {
final SuppressPropertiesBeanIntrospector introspector = new SuppressPropertiesBeanIntrospector(
properties);
properties.add("prop2");
- final IntrospectionContextTestImpl context = new IntrospectionContextTestImpl();
+ // Create variables for tracking behaviors of mock object
+ Set contextRemovedProperties = new HashSet<>();
+ // Construct mock object
+ final IntrospectionContext context = mock(IntrospectionContext.class);
+ // Method Stubs
+ doThrow(new UnsupportedOperationException("Unexpected method call!")).when(context)
+ .addPropertyDescriptors(any(PropertyDescriptor[].class));
+ doThrow(new UnsupportedOperationException("Unexpected method call!")).when(context)
+ .addPropertyDescriptor(any(PropertyDescriptor.class));
+ doAnswer((stubInvo) -> {
+ String name = stubInvo.getArgument(0);
+ contextRemovedProperties.add(name);
+ return null;
+ }).when(context).removePropertyDescriptor(any(String.class));
+ when(context.getTargetClass()).thenAnswer((stubInvo) -> {
+ throw new UnsupportedOperationException("Unexpected method call!");
+ });
+ when(context.hasProperty(any(String.class)))
+ .thenThrow(new UnsupportedOperationException("Unexpected method call!"));
+ when(context.getPropertyDescriptor(any(String.class)))
+ .thenThrow(new UnsupportedOperationException("Unexpected method call!"));
+ when(context.propertyNames()).thenAnswer((stubInvo) -> {
+ throw new UnsupportedOperationException("Unexpected method call!");
+ });
introspector.introspect(context);
- assertEquals("Wrong number of removed properties", 1, context
- .getRemovedProperties().size());
+ assertEquals("Wrong number of removed properties", 1, contextRemovedProperties.size());
assertTrue("Wrong removed property",
- context.getRemovedProperties().contains("prop1"));
+ contextRemovedProperties.contains("prop1"));
}
/**
@@ -93,57 +140,4 @@ public void testGetSuppressedPropertiesModify() {
// ok
}
}
-
- /**
- * A test implementation of IntrospectionContext which collects the properties which
- * have been removed.
- */
- private static class IntrospectionContextTestImpl implements IntrospectionContext {
- /** Stores the names of properties which have been removed. */
- private final Set removedProperties = new HashSet<>();
-
- /**
- * Returns the names of properties which have been removed.
- *
- * @return the set with removed properties
- */
- public Set getRemovedProperties() {
- return removedProperties;
- }
-
- @Override
- public Class> getTargetClass() {
- throw new UnsupportedOperationException("Unexpected method call!");
- }
-
- @Override
- public void addPropertyDescriptor(final PropertyDescriptor desc) {
- throw new UnsupportedOperationException("Unexpected method call!");
- }
-
- @Override
- public void addPropertyDescriptors(final PropertyDescriptor[] descriptors) {
- throw new UnsupportedOperationException("Unexpected method call!");
- }
-
- @Override
- public boolean hasProperty(final String name) {
- throw new UnsupportedOperationException("Unexpected method call!");
- }
-
- @Override
- public PropertyDescriptor getPropertyDescriptor(final String name) {
- throw new UnsupportedOperationException("Unexpected method call!");
- }
-
- @Override
- public void removePropertyDescriptor(final String name) {
- removedProperties.add(name);
- }
-
- @Override
- public Set propertyNames() {
- throw new UnsupportedOperationException("Unexpected method call!");
- }
- }
}