diff --git a/src/main/java/com/github/jsonzou/jmockdata/MockConfig.java b/src/main/java/com/github/jsonzou/jmockdata/MockConfig.java index 7f66c426a..719cbb82d 100644 --- a/src/main/java/com/github/jsonzou/jmockdata/MockConfig.java +++ b/src/main/java/com/github/jsonzou/jmockdata/MockConfig.java @@ -114,6 +114,14 @@ public Object getcacheBean(String beanClassName) { return beanCache.get(beanClassName); } + public void cacheTypeVariable(String name, Type type) { + typeVariableCache.put(name, type); + } + + public void removeTypeVariable(String name) { + typeVariableCache.remove(name); + } + public void cacheEnum(String name, Enum[] enums) { enumCache.put(name, enums); } diff --git a/src/main/java/com/github/jsonzou/jmockdata/mocker/BeanMocker.java b/src/main/java/com/github/jsonzou/jmockdata/mocker/BeanMocker.java index e2c9a3613..aef73e7e0 100644 --- a/src/main/java/com/github/jsonzou/jmockdata/mocker/BeanMocker.java +++ b/src/main/java/com/github/jsonzou/jmockdata/mocker/BeanMocker.java @@ -8,7 +8,10 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -17,9 +20,15 @@ public class BeanMocker implements Mocker { private Map, List> classFieldCache = new ConcurrentHashMap<>((int)((float)32 / 0.75F + 1.0F)); private final Class clazz; + private final Type[] genericTypes; BeanMocker(Class clazz) { + this(clazz, null); + } + + BeanMocker(Class clazz, Type[] genericTypes) { this.clazz = clazz; + this.genericTypes = genericTypes; } @Override @@ -41,7 +50,41 @@ public Object mock(DataConfig mockConfig) { if(mockConfig.globalConfig().isConfigExcludeMock(clazz)){ return result; } - setFieldValueByFieldAccessible(mockConfig, result); + // Initialize type variable cache if generic types are provided + Map savedTypeVariables = null; + if (genericTypes != null && genericTypes.length > 0) { + TypeVariable[] typeVariables = clazz.getTypeParameters(); + if (typeVariables != null && typeVariables.length > 0) { + // Save current type variable mappings + savedTypeVariables = new HashMap<>(); + for (int index = 0; index < Math.min(typeVariables.length, genericTypes.length); index++) { + String varName = typeVariables[index].getName(); + Type oldValue = mockConfig.globalConfig().getVariableType(varName); + if (oldValue != null) { + savedTypeVariables.put(varName, oldValue); + } + mockConfig.globalConfig().cacheTypeVariable(varName, genericTypes[index]); + } + } + } + try { + setFieldValueByFieldAccessible(mockConfig, result); + } finally { + // Restore previous type variable mappings + if (savedTypeVariables != null) { + TypeVariable[] typeVariables = clazz.getTypeParameters(); + if (typeVariables != null && typeVariables.length > 0) { + for (int index = 0; index < Math.min(typeVariables.length, genericTypes.length); index++) { + String varName = typeVariables[index].getName(); + if (savedTypeVariables.containsKey(varName)) { + mockConfig.globalConfig().cacheTypeVariable(varName, savedTypeVariables.get(varName)); + } else { + mockConfig.globalConfig().removeTypeVariable(varName); + } + } + } + } + } return result; } catch (Exception e) { throw new MockException(e); diff --git a/src/main/java/com/github/jsonzou/jmockdata/mocker/ClassMocker.java b/src/main/java/com/github/jsonzou/jmockdata/mocker/ClassMocker.java index 2ec273b2b..871adf15f 100644 --- a/src/main/java/com/github/jsonzou/jmockdata/mocker/ClassMocker.java +++ b/src/main/java/com/github/jsonzou/jmockdata/mocker/ClassMocker.java @@ -32,7 +32,7 @@ public Object mock(DataConfig mockConfig) { } else { mocker = mockConfig.globalConfig().getMocker(clazz); if (mocker == null) { - mocker = new BeanMocker(clazz); + mocker = new BeanMocker(clazz, genericTypes); } } return mocker.mock(mockConfig); diff --git a/src/test/java/com/github/jsonzou/jmockdata/JMockDataTest.java b/src/test/java/com/github/jsonzou/jmockdata/JMockDataTest.java index 3741dda25..5b05b692e 100644 --- a/src/test/java/com/github/jsonzou/jmockdata/JMockDataTest.java +++ b/src/test/java/com/github/jsonzou/jmockdata/JMockDataTest.java @@ -121,6 +121,28 @@ public void testGenericData() { assertNotNull(genericData); } + /** + * Test for issue: Mocking fails when regular objects contain collections with generic fields + * (普通对象包含含有泛型字段的集合时mock失败) + * Test mocking a bean with a field containing a parameterized generic type + */ + @Test + public void testGenericFieldInCollection() { + GeneralBean entity = JMockData.mock(GeneralBean.class); + assertNotNull(entity); + assertNotNull(entity.getRows()); + assertFalse(entity.getRows().isEmpty()); + + // Verify that the generic field in GenericFieldEntity is properly mocked + GenericFieldEntity firstItem = entity.getRows().get(0); + assertNotNull(firstItem); + assertNotNull(firstItem.getKey()); + assertTrue(firstItem.getKey() instanceof String); + + System.out.println("Generic field test passed! Key type: " + firstItem.getKey().getClass().getName()); + System.out.println("Key value: " + firstItem.getKey()); + } + @Test public void testMockConfig() { MockConfig mockConfig = new MockConfig() diff --git a/src/test/java/com/github/jsonzou/jmockdata/bean/GeneralBean.java b/src/test/java/com/github/jsonzou/jmockdata/bean/GeneralBean.java new file mode 100644 index 000000000..223dcdda2 --- /dev/null +++ b/src/test/java/com/github/jsonzou/jmockdata/bean/GeneralBean.java @@ -0,0 +1,19 @@ +package com.github.jsonzou.jmockdata.bean; + +import java.util.List; + +/** + * Bean containing a list of generic entities + * Used to test the fix for issue: Mocking fails when regular objects contain collections with generic fields + */ +public class GeneralBean { + private List> rows; + + public List> getRows() { + return rows; + } + + public void setRows(List> rows) { + this.rows = rows; + } +} diff --git a/src/test/java/com/github/jsonzou/jmockdata/bean/GenericFieldEntity.java b/src/test/java/com/github/jsonzou/jmockdata/bean/GenericFieldEntity.java new file mode 100644 index 000000000..de1411aa8 --- /dev/null +++ b/src/test/java/com/github/jsonzou/jmockdata/bean/GenericFieldEntity.java @@ -0,0 +1,16 @@ +package com.github.jsonzou.jmockdata.bean; + +/** + * Generic entity with a parameterized type field + */ +public class GenericFieldEntity { + private T key; + + public T getKey() { + return key; + } + + public void setKey(T key) { + this.key = key; + } +}