简体   繁体   English

Mockito & PowerMock - 类大小限制

[英]Mockito & PowerMock - class size limitations

To mock data for my unit testing, I am using Mockito.为了模拟我的单元测试数据,我使用了 Mockito。 But I see the below exception.但我看到以下异常。 Am I missing any setup?我缺少任何设置吗?

JVM used here is Java HotSpot(TM) 64-Bit Server VM.此处使用的 JVM 是 Java HotSpot(TM) 64 位服务器 VM。 1.8 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();
}

This exception is thrown at runtime:在运行时抛出此异常:

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$1.call(TypeCachingBytecodeGenerator.java:37)
    at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.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)

The exception comes from ASM where it complains about an overly large constant pool as Holger pointed out in the comments.异常来自 ASM,它抱怨常量池过大,正如 Holger 在评论中指出的那样。 This is a result of applying both the instrumentation implied by Mockito and by PowerMock on an already large class.这是将 Mockito 和 PowerMock 隐含的检测应用于已经很大的类的结果。

The Java class file format sets some limitations as for example the number of fields, methods or constants that are declared by a class. Java 类文件格式设置了一些限制,例如类声明的字段、方法或常量的数量。 If you cross this limit in a non-generated Java class, this will yield a similar problem.如果在非生成的 Java 类中越过此限制,则会产生类似的问题。

In order to mock a class, Mockito asks Byte Buddy to add dispatcher methods and synthetic methods for invoking each super method.为了模拟一个类,Mockito 要求 Byte Buddy 添加调度程序方法和合成方法来调用每个超级方法。 This effectively doubles the amount of methods by a class and also adds to the constant pool.这有效地使类的方法数量增加了一倍,并且还增加了常量池。 PowerMock applies something similar. PowerMock 应用了类似的东西。

If you were already close to the constant limit, these additions finally push you over the edge of what a class file can represent.如果您已经接近常量限制,这些添加最终将您推到类文件可以表示的边缘。 It typically only happens when you have an object containing nothing but a few thousand setters and getters where the result is that you simply cannot mock this object, especially with both Mockito and PowerMock active.它通常只发生在你有一个只包含几千个 setter 和 getter 的对象时,结果是你根本无法模拟这个对象,尤其是当 Mockito 和 PowerMock 都处于活动状态时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM