Mockito 和 PowerMock - class 大小限制
Mockito & PowerMock - class size limitations
为了模拟单元测试的数据,我使用了 Mockito。但我看到以下异常。我是否缺少任何设置?
此处使用的 JVM 是 Java HotSpot(TM) 64 位服务器 VM。 1.8
public class TestCreateObj{
public void getMockData() {
TestObj deal = mock(TestObj.class);
when(deal.getDescription()).thenReturn("HURRAH!!!");
System.out.println(deal.getDescription());
}
public static void main(String args[]) {
new TestCreateObj().getMockData();
}
运行时抛出异常:
Caused by: java.lang.RuntimeException: Class file too large!
at net.bytebuddy.jar.asm.ClassWriter.toByteArray(Unknown Source)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default$ForCreation.create(TypeWriter.java:4108)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default.make(TypeWriter.java:1612)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:174)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:155)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.make(DynamicType.java:2560)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase$Delegator.make(DynamicType.java:2662)
at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.mockClass(SubclassBytecodeGenerator.java:94)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.call(TypeCachingBytecodeGenerator.java:37)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.call(TypeCachingBytecodeGenerator.java:34)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:138)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:346)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:161)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:355)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:32)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMockType(SubclassByteBuddyMockMaker.java:71)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:42)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:26)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:65)
at org.mockito.Mockito.mock(Mockito.java:1691)
at org.mockito.Mockito.mock(Mockito.java:1604)
异常来自 ASM,正如 Holger 在评论中指出的那样,它抱怨常量池过大。这是在已经很大的 class.
上同时应用 Mockito 和 PowerMock 暗示的检测的结果
Java class 文件格式设置了一些限制,例如 class 声明的字段、方法或常量的数量。如果您在 non-generated Java class 中超过此限制,则会产生类似的问题。
为了模拟 class,Mockito 要求 Byte Buddy 添加调度程序方法和合成方法以调用每个超级方法。这有效地将方法数量加倍 class 并且还添加到常量池中。 PowerMock 应用了类似的东西。
如果您已经接近常量限制,那么这些添加最终会将您推向 class 文件所能代表的极限。它通常只发生在你有一个只包含几千个 setter 和 getter 的对象时,结果是你根本无法模拟这个对象,尤其是在 Mockito 和 PowerMock 都处于活动状态的情况下。
为了模拟单元测试的数据,我使用了 Mockito。但我看到以下异常。我是否缺少任何设置?
此处使用的 JVM 是 Java HotSpot(TM) 64 位服务器 VM。 1.8
public class TestCreateObj{
public void getMockData() {
TestObj deal = mock(TestObj.class);
when(deal.getDescription()).thenReturn("HURRAH!!!");
System.out.println(deal.getDescription());
}
public static void main(String args[]) {
new TestCreateObj().getMockData();
}
运行时抛出异常:
Caused by: java.lang.RuntimeException: Class file too large!
at net.bytebuddy.jar.asm.ClassWriter.toByteArray(Unknown Source)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default$ForCreation.create(TypeWriter.java:4108)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default.make(TypeWriter.java:1612)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:174)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:155)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.make(DynamicType.java:2560)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase$Delegator.make(DynamicType.java:2662)
at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.mockClass(SubclassBytecodeGenerator.java:94)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.call(TypeCachingBytecodeGenerator.java:37)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.call(TypeCachingBytecodeGenerator.java:34)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:138)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:346)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:161)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:355)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:32)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMockType(SubclassByteBuddyMockMaker.java:71)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:42)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:26)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:65)
at org.mockito.Mockito.mock(Mockito.java:1691)
at org.mockito.Mockito.mock(Mockito.java:1604)
异常来自 ASM,正如 Holger 在评论中指出的那样,它抱怨常量池过大。这是在已经很大的 class.
上同时应用 Mockito 和 PowerMock 暗示的检测的结果Java class 文件格式设置了一些限制,例如 class 声明的字段、方法或常量的数量。如果您在 non-generated Java class 中超过此限制,则会产生类似的问题。
为了模拟 class,Mockito 要求 Byte Buddy 添加调度程序方法和合成方法以调用每个超级方法。这有效地将方法数量加倍 class 并且还添加到常量池中。 PowerMock 应用了类似的东西。
如果您已经接近常量限制,那么这些添加最终会将您推向 class 文件所能代表的极限。它通常只发生在你有一个只包含几千个 setter 和 getter 的对象时,结果是你根本无法模拟这个对象,尤其是在 Mockito 和 PowerMock 都处于活动状态的情况下。