SGF-609 - Fix test failures related to Mockito version 2.7.

This commit is contained in:
John Blum
2017-04-12 19:18:11 -07:00
parent 6abac9af12
commit 979a7895c6
7 changed files with 117 additions and 74 deletions

View File

@@ -32,7 +32,6 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import java.io.InputStream;
@@ -58,8 +57,19 @@ import org.springframework.data.gemfire.config.xml.GemfireConstants;
import org.springframework.data.gemfire.util.ArrayUtils;
/**
* Unit tests for {@link ClientRegionFactoryBean}.
*
* @author David Turanski
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.data.gemfire.client.ClientRegionFactoryBean
* @see org.apache.geode.cache.EvictionAttributes
* @see org.apache.geode.cache.ExpirationAttributes
* @see org.apache.geode.cache.Region
* @see org.apache.geode.cache.client.ClientCache
* @see org.apache.geode.cache.client.ClientRegionFactory
* @see org.apache.geode.cache.client.Pool
*/
public class ClientRegionFactoryBeanTest {
@@ -215,7 +225,6 @@ public class ClientRegionFactoryBeanTest {
verify(mockBeanFactory, times(1)).containsBean(eq(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME));
verify(mockClientCache, times(1)).createClientRegionFactory(eq(ClientRegionShortcut.CACHING_PROXY));
verify(mockClientRegionFactory, times(1)).create(eq("TestRegion"));
verifyZeroInteractions(mockRegion);
}
@Test
@@ -244,7 +253,6 @@ public class ClientRegionFactoryBeanTest {
verify(mockBeanFactory, times(1)).containsBean(eq(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME));
verify(mockClientCache, times(1)).createClientRegionFactory(eq(ClientRegionShortcut.PROXY));
verify(mockClientRegionFactory, times(1)).createSubregion(eq(mockRegion), eq("TestSubRegion"));
verifyZeroInteractions(mockSubRegion);
}
@Test
@@ -271,7 +279,6 @@ public class ClientRegionFactoryBeanTest {
verify(mockClientCache, times(1)).createClientRegionFactory(eq(ClientRegionShortcut.LOCAL_HEAP_LRU));
verify(mockClientRegionFactory, times(1)).create(eq("TestRegion"));
verify(mockClientRegionFactory, never()).setPoolName(any(String.class));
verifyZeroInteractions(mockRegion);
}
@Test

View File

@@ -22,11 +22,14 @@ import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@@ -41,6 +44,7 @@ import org.apache.geode.cache.lucene.LuceneIndexFactory;
import org.apache.geode.cache.lucene.LuceneService;
import org.apache.geode.cache.query.Index;
import org.apache.geode.cache.query.QueryService;
import org.apache.lucene.analysis.Analyzer;
import org.junit.After;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
@@ -82,6 +86,21 @@ public class EnableIndexingConfigurationUnitTests {
indexes.clear();
}
private static String[] asArray(List<String> list) {
return list.toArray(new String[list.size()]);
}
private static String[] toStringArray(Object[] array) {
String[] stringArray = new String[array.length];
int index = 0;
for (Object element : array) {
stringArray[index++] = String.valueOf(element);
}
return stringArray;
}
/* (non-Javadoc) */
protected void assertIndex(Index index, String name, String expression, String from, IndexType indexType) {
assertThat(index).isNotNull();
@@ -96,8 +115,8 @@ public class EnableIndexingConfigurationUnitTests {
assertThat(index).isNotNull();
assertThat(index.getName()).isEqualTo(name);
assertThat(index.getRegionPath()).isEqualTo(regionPath);
assertThat(index.getFieldNames()).contains(fields);
assertThat(index.getFieldNames()).hasSize(fields.length);
assertThat(index.getFieldNames()).contains(fields);
}
/* (non-Javadoc) */
@@ -206,36 +225,48 @@ public class EnableIndexingConfigurationUnitTests {
}
@Bean
@SuppressWarnings("unchecked")
LuceneService luceneService() {
LuceneService mockLuceneService = mock(LuceneService.class);
LuceneIndexFactory mockLuceneIndexFactory = mock(LuceneIndexFactory.class);
when(mockLuceneService.createIndexFactory()).thenAnswer(invocation -> {
LuceneIndexFactory mockLuceneIndexFactory = mock(LuceneIndexFactory.class);
doReturn(mockLuceneIndexFactory).when(mockLuceneService).createIndexFactory();
List<String> fieldNames = new ArrayList<>();
doAnswer(invocation -> {
LuceneIndex mockLuceneIndex = mock(LuceneIndex.class);
when(mockLuceneIndexFactory.setFields((String[]) any())).thenAnswer(setFieldsInvocation -> {
Collections.addAll(fieldNames, toStringArray(setFieldsInvocation.getArguments()));
return mockLuceneIndexFactory;
});
String indexName = invocation.getArgument(0);
String regionPath = invocation.getArgument(1);
Map<String, Analyzer> fieldAnalyzers = new HashMap<>();
when(mockLuceneIndex.getName()).thenReturn(indexName);
when(mockLuceneIndex.getRegionPath()).thenReturn(regionPath);
when(mockLuceneIndex.getFieldNames()).thenReturn(resolveFieldNames(invocation));
when(mockLuceneService.getIndex(eq(indexName), eq(regionPath))).thenReturn(mockLuceneIndex);
when(mockLuceneIndexFactory.setFields(any(Map.class))).thenAnswer(setFieldsInvocation -> {
fieldAnalyzers.putAll(setFieldsInvocation.getArgument(0));
return mockLuceneIndexFactory;
});
return mockLuceneIndex;
}).when(mockLuceneIndexFactory).create(anyString(), anyString());
doAnswer(createInvocation -> {
LuceneIndex mockLuceneIndex = mock(LuceneIndex.class);
String indexName = createInvocation.getArgument(0);
String regionPath = createInvocation.getArgument(1);
when(mockLuceneIndex.getName()).thenReturn(indexName);
when(mockLuceneIndex.getRegionPath()).thenReturn(regionPath);
when(mockLuceneIndex.getFieldAnalyzers()).thenReturn(fieldAnalyzers);
when(mockLuceneIndex.getFieldNames()).thenReturn(asArray(fieldNames));
when(mockLuceneService.getIndex(eq(indexName), eq(regionPath))).thenReturn(mockLuceneIndex);
return mockLuceneIndex;
}).when(mockLuceneIndexFactory).create(anyString(), anyString());
return mockLuceneIndexFactory;
});
return mockLuceneService;
}
@SuppressWarnings("all")
String[] resolveFieldNames(InvocationOnMock invocation) {
String[] fieldNames = new String[invocation.getArguments().length - 2];
System.arraycopy(invocation.getArguments(), 2, fieldNames, 0, fieldNames.length);
return fieldNames;
}
}
static abstract class AbstractIndexAnswer implements Answer<Index> {

View File

@@ -22,14 +22,16 @@ import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -40,7 +42,6 @@ import org.apache.geode.cache.lucene.LuceneService;
import org.apache.lucene.analysis.Analyzer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
@@ -58,6 +59,10 @@ import org.springframework.test.context.junit4.SpringRunner;
* @author John Blum
* @see org.junit.Test
* @see org.junit.runner.RunWith
* @see org.apache.geode.cache.lucene.LuceneIndex
* @see org.apache.geode.cache.lucene.LuceneService
* @see org.springframework.data.gemfire.config.xml.LuceneIndexParser
* @see org.springframework.data.gemfire.config.xml.LuceneServiceParser
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringRunner
* @since 1.1.0
@@ -88,6 +93,21 @@ public class LuceneNamespaceUnitTests {
@Qualifier("IndexFour")
private LuceneIndex luceneIndexFour;
private static String[] asArray(List<String> list) {
return list.toArray(new String[list.size()]);
}
private static String[] toStringArray(Object[] array) {
String[] stringArray = new String[array.length];
int index = 0;
for (Object element : array) {
stringArray[index++] = String.valueOf(element);
}
return stringArray;
}
protected void assertLuceneIndex(LuceneIndex index, String name, String regionPath) {
assertThat(index).isNotNull();
assertThat(index.getName()).isEqualTo(name);
@@ -110,10 +130,9 @@ public class LuceneNamespaceUnitTests {
}
@Test
@SuppressWarnings({ "deprecation", "unchecked" })
public void luceneServiceConfigurationAndInteractionsAreCorrect() {
assertThat(this.luceneService).isNotNull();
verify(this.luceneService, times(2)).createIndexFactory();
verify(this.luceneService, times(4)).createIndexFactory();
verify(this.luceneService, never()).destroyIndex(anyString(), anyString());
}
@@ -170,10 +189,22 @@ public class LuceneNamespaceUnitTests {
when(this.luceneService.createIndexFactory()).thenAnswer(invocation -> {
LuceneIndexFactory mockLuceneIndexFactory = mock(LuceneIndexFactory.class);
doReturn(mockLuceneIndexFactory).when(mockLuceneIndexFactory).setFields((String[]) any());
doReturn(mockLuceneIndexFactory).when(mockLuceneIndexFactory).setFields(any(Map.class));
List<String> fieldNames = new ArrayList<>();
Answer<LuceneIndex> mockLuceneIndex = mockLuceneIndex(this.luceneService);
when(mockLuceneIndexFactory.setFields((String[]) any())).thenAnswer(setFieldsInvocation -> {
Collections.addAll(fieldNames, toStringArray(setFieldsInvocation.getArguments()));
return mockLuceneIndexFactory;
});
Map<String, Analyzer> fieldAnalyzers = new HashMap<>();
when(mockLuceneIndexFactory.setFields(any(Map.class))).thenAnswer(setFieldsInvocation -> {
fieldAnalyzers.putAll(setFieldsInvocation.getArgument(0));
return mockLuceneIndexFactory;
});
Answer<LuceneIndex> mockLuceneIndex =
mockLuceneIndex(this.luceneService, fieldAnalyzers, fieldNames);
doAnswer(mockLuceneIndex).when(mockLuceneIndexFactory).create(anyString(), anyString());
@@ -185,46 +216,25 @@ public class LuceneNamespaceUnitTests {
}
@SuppressWarnings("unchecked")
private Answer<LuceneIndex> mockLuceneIndex(LuceneService mockLuceneService) {
return (invocationOnMock) -> {
String indexName = invocationOnMock.getArgument(0);
String regionPath = invocationOnMock.getArgument(1);
private Answer<LuceneIndex> mockLuceneIndex(LuceneService mockLuceneService,
Map<String, Analyzer> fieldAnalyzers, List<String> fieldNames) {
return invocation -> {
String indexName = invocation.getArgument(0);
String regionPath = invocation.getArgument(1);
LuceneIndex mockLuceneIndex = mock(LuceneIndex.class, indexName);
when(mockLuceneIndex.getName()).thenReturn(indexName);
when(mockLuceneIndex.getRegionPath()).thenReturn(regionPath);
if (invocationOnMock.getArguments().length > 2) {
Object fields = invocationOnMock.getArgument(2);
if (fields instanceof Map) {
when(mockLuceneIndex.getFieldAnalyzers()).thenReturn((Map<String, Analyzer>) fields);
when(mockLuceneIndex.getFieldNames()).thenReturn(EMPTY_STRING_ARRAY);
}
else {
when(mockLuceneIndex.getFieldAnalyzers()).thenReturn(Collections.emptyMap());
when(mockLuceneIndex.getFieldNames()).thenReturn(extractFields(invocationOnMock));
}
}
when(mockLuceneIndex.getFieldAnalyzers()).thenReturn(fieldAnalyzers);
when(mockLuceneIndex.getFieldNames()).thenReturn(asArray(fieldNames));
when(mockLuceneService.getIndex(eq(indexName), eq(regionPath))).thenReturn(mockLuceneIndex);
return mockLuceneIndex;
};
}
private String[] asStringArray(Object fields) {
return (fields instanceof String[] ? (String[]) fields : String.valueOf(fields).split(", "));
}
@SuppressWarnings("all")
private String[] extractFields(InvocationOnMock invocationOnMock) {
String[] fields = new String[invocationOnMock.getArguments().length - 2];
System.arraycopy(invocationOnMock.getArguments(), 2, fields, 0, fields.length);
return fields;
}
@Override
public Class<?> getObjectType() {
return Optional.ofNullable(this.luceneService).<Class<?>>map(LuceneService::getClass)

View File

@@ -104,7 +104,6 @@ public class AbstractSliceSupportTests {
Pageable mockPageable = mock(Pageable.class);
when(mockPageable.previousOrFirst()).thenReturn(mockPageable);
when(mockPageable.hasPrevious()).thenReturn(false);
doReturn(mockPageable).when(mockSlice).previousPageable();
assertThat(mockSlice.getNumber()).isEqualTo(2);
@@ -119,9 +118,7 @@ public class AbstractSliceSupportTests {
Pageable mockPageableTwo = mock(Pageable.class, "Page Two");
when(mockPageableOne.previousOrFirst()).thenReturn(mockPageableOne);
when(mockPageableOne.hasPrevious()).thenReturn(false);
when(mockPageableTwo.previousOrFirst()).thenReturn(mockPageableOne);
when(mockPageableTwo.hasPrevious()).thenReturn(true);
doReturn(mockPageableTwo).when(mockSlice).previousPageable();
assertThat(mockSlice.getNumber()).isEqualTo(3);

View File

@@ -33,9 +33,7 @@ import org.springframework.data.gemfire.repository.sample.Algorithm;
import org.springframework.data.gemfire.repository.sample.Animal;
/**
* The DefaultGemfireEntityInformationTest class is a test suite of test cases testing the contract and functionality
* of the DefaultGemfireEntityInformation class used to extract entity information during persistence/mapping operations
* during data access to the underlying data store (GemFire).
* Unit tests for {@link DefaultGemfireEntityInformation}.
*
* @author John Blum
* @see org.junit.Test
@@ -106,7 +104,7 @@ public class DefaultGemfireEntityInformationTest {
newEntityInformation(newPersistentEntity(ConfusedDomainEntity.class));
assertNotNull(entityInfo);
assertEquals("MyConfusedDomainEntity", entityInfo.getRegionName());
assertEquals("ConfusedDomainEntity", entityInfo.getRegionName());
assertEquals(ConfusedDomainEntity.class, entityInfo.getJavaType());
assertEquals(Long.class, entityInfo.getIdType());
assertThat(entityInfo.getId(new ConfusedDomainEntity(123L)).orElse(null)).isEqualTo(123L);
@@ -119,7 +117,7 @@ public class DefaultGemfireEntityInformationTest {
newEntityInformation(newPersistentEntity(ConfusedDomainEntity.class));
assertNotNull(entityInfo);
assertEquals("MyConfusedDomainEntity", entityInfo.getRegionName());
assertEquals("ConfusedDomainEntity", entityInfo.getRegionName());
assertEquals(ConfusedDomainEntity.class, entityInfo.getJavaType());
//assertEquals(String.class, entityInfo.getIdType());
assertTrue(Long.class.equals(entityInfo.getIdType()));

View File

@@ -38,6 +38,7 @@ import org.springframework.beans.factory.config.BeanDefinition;
*
* @author John Blum
* @see org.junit.Test
* @see org.junit.runner.RunWith
* @see org.mockito.Mock
* @see org.mockito.Mockito
* @see org.mockito.junit.MockitoJUnitRunner
@@ -124,8 +125,6 @@ public class SpringUtilsUnitTests {
public void defaultIfNullWithSupplierReturnsValue() {
Supplier<String> mockSupplier = mock(Supplier.class);
when(mockSupplier.get()).thenReturn("supplier");
assertThat(SpringUtils.defaultIfNull("value", mockSupplier)).isEqualTo("value");
verify(mockSupplier, never()).get();