简体   繁体   中英

Mockito & PowerMock - class size limitations

To mock data for my unit testing, I am using Mockito. But I see the below exception. Am I missing any setup?

JVM used here is Java HotSpot(TM) 64-Bit Server 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();
}

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. This is a result of applying both the instrumentation implied by Mockito and by PowerMock on an already large class.

The Java class file format sets some limitations as for example the number of fields, methods or constants that are declared by a class. If you cross this limit in a non-generated Java class, this will yield a similar problem.

In order to mock a class, Mockito asks Byte Buddy to add dispatcher methods and synthetic methods for invoking each super method. This effectively doubles the amount of methods by a class and also adds to the constant pool. PowerMock applies something similar.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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