Willam2004 / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

Powermock cannot handle collapsed catch blocks (introduced in Java 7) #485

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Surround a method, which throws multiple checked exceptions, with a try 
catch block
2. Collapse all exceptions in 1 line of code. For example, when using 
reflection to call a method, it will throw several checked exceptions, like 
this: 

try {
            Class<? extends ControlledVocabulary> entityClass = findControlledVocabularyFor(request.getType()).getEntityClass();
            builder = (ControlledVocabulary.Builder) entityClass.getDeclaredMethod("builder").invoke(null);
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            LOGGER.error(TAG, "Could not instantiate ControlledVocabulary of type " + request.getType(), e);
            throw new RuntimeException("Could not instantiate ControlledVocabulary of type " + request.getType(), e);
        }

(ignore the business rules behind this, let's just say that I'm getting some 
class, based on a string in the request, and I am calling a (static) method to 
get a builder object from that class)

3. Write a unit test for this class using PowerMock. For testing reasons, I had 
to mock a constructor used by that class. So, annotated on the type are these 
annotations:

@RunWith(PowerMockRunner.class)
@PrepareForTest(ManageControlledVocabularyService.class)

... where, evidently, ManageControlledVocabularyService is the class which I'm 
testing.

############################
What is the expected output? What do you see instead?

I would expect PowerMock not to complain :-) 

Instead, I get a java.lang.VerifyError: 

-----------------------------------------------------
<<<START FULL STACK TRACE>>>

java.lang.VerifyError: Stack map does not match the one at exception handler 314
Exception Details:
  Location:
    be/inbo/lifewatch/seabirds/manage/controlled_vocabulary/service/ManageControlledVocabularyService.createControlledVocabulary(Lbe/inbo/lifewatch/seabirds/manage/controlled_vocabulary/controller/request/ManageControlledVocabularyRequest;)Lbe/inbo/lifewatch/seabirds/manage/controlled_vocabulary/model/ControlledVocabulary; @314: astore_3
  Reason:
    Type 'java/lang/IllegalAccessException' (current frame, stack[0]) is not assignable to 'java/lang/NoSuchMethodException' (stack map, stack[0])
  Current Frame:
    bci: @87
    flags: { }
    locals: { 'be/inbo/lifewatch/seabirds/manage/controlled_vocabulary/service/ManageControlledVocabularyService', 'be/inbo/lifewatch/seabirds/manage/controlled_vocabulary/controller/request/ManageControlledVocabularyRequest' }
    stack: { 'java/lang/IllegalAccessException' }
  Stackmap Frame:
    bci: @314
    flags: { }
    locals: { 'be/inbo/lifewatch/seabirds/manage/controlled_vocabulary/service/ManageControlledVocabularyService', 'be/inbo/lifewatch/seabirds/manage/controlled_vocabulary/controller/request/ManageControlledVocabularyRequest' }
    stack: { 'java/lang/NoSuchMethodException' }
  Bytecode:
    0000000: 2a13 0208 04bd 003f 5903 2b53 1302 09b8
    0000010: 01d4 1302 0bb8 01dc 4d2c 013a 0401 3a05
    0000020: 1904 1303 60b8 035a 1303 6113 0362 b803
    0000030: 64b8 0356 3a06 1906 b203 30a6 000b b201
    0000040: e03a 05a7 000a 1906 c002 2d3a 0519 05a5
    0000050: 0008 2cc0 020c b02a 2bb9 003a 0100 b700
    0000060: 04b6 003b 4e2d 123c 03bd 003d 3a06 3a05
    0000070: 3a04 013a 0701 3a08 1904 01a5 000a 1904
    0000080: 3a08 a700 0b13 0366 b803 683a 0819 0813
    0000090: 0369 05bd 003f 5903 1905 5359 0419 0653
    00000a0: 1303 6ab8 036b 1303 6db8 0347 3a09 1909
    00000b0: b203 30a6 0011 1904 1905 1906 b603 6f3a
    00000c0: 07a7 000a 1909 c003 703a 0719 0701 03bd
    00000d0: 003f 3a06 3a05 3a04 013a 0701 3a08 1904
    00000e0: 01a5 000a 1904 3a08 a700 0b13 0371 b803
    00000f0: 683a 0819 0813 0372 05bd 003f 5903 1905
    0000100: 5359 0419 0653 1303 73b8 036b 1303 74b8
    0000110: 0347 3a09 1909 b203 30a6 0011 1904 1905
    0000120: 1906 b603 763a 07a7 000a 1909 c002 2d3a
    0000130: 0719 07c0 0041 4da7 0389 4e01 3a04 013a
    0000140: 0519 0413 0377 b803 6813 0378 1303 79b8
    0000150: 037b b803 7c3a 0619 06b2 0330 a600 0bb2
    0000160: 0045 3a05 a700 0a19 06c0 037d 3a05 1905
    0000170: 1246 0000 0000 013a 0401 3a05 1303 7fb8
    0000180: 037b 03bd 003f 1303 81b8 036b b803 833a
    0000190: 0619 06b2 0330 a500 3319 06c1 0266 9900
    00001a0: 21b8 0385 1303 86b8 0387 123f 01b6 0389
    00001b0: b603 8a01 b603 8cc0 0047 3a05 a700 0a19
    00001c0: 06c0 0047 3a05 a700 0cbb 0047 59b7 038e
    00001d0: 3a05 1905 1249 3a05 3a04 013a 0601 3a07
    00001e0: 1904 01a5 000a 1904 3a07 a700 0b13 0390
    00001f0: b803 923a 0719 0713 0393 04bd 003f 5903
    0000200: 1905 5313 0394 b803 9513 0396 b803 983a
    0000210: 0819 08b2 0330 a600 0f19 0419 05b6 039a
    0000220: 3a06 a700 0a19 08c0 039b 3a06 1906 2bb9
    0000230: 003a 0100 3a05 3a04 013a 0601 3a07 1904
    0000240: 01a5 000a 1904 3a07 a700 0b13 039c b803
    0000250: 923a 0719 0713 039d 04bd 003f 5903 1905
    0000260: 5313 039e b803 9513 039f b803 a03a 0819
    0000270: 08b2 0330 a600 0f19 0419 05b6 03a1 3a06
    0000280: a700 0a19 08c0 039b 3a06 1906 3a04 013a
    0000290: 0501 3a06 1904 01a5 000a 1904 3a06 a700
    00002a0: 0b13 03a2 b803 923a 0619 0613 03a3 03bd
    00002b0: 003f 1303 a4b8 0395 1303 a6b8 03a7 3a07
    00002c0: 1907 b203 30a6 000d 1904 b603 a93a 05a7
    00002d0: 000a 1907 c003 aa3a 0519 052d b900 4c04
    00002e0: 0000 0000 0000 0000 0001 3a04 013a 0513
    00002f0: 03ab b803 ad03 bd00 3f13 03ae b803 95b8
    0000300: 03af 3a06 1906 b203 30a5 0033 1906 c102
    0000310: 6699 0021 b803 b013 03b1 b803 b312 3f01
    0000320: b603 b4b6 03b5 01b6 038c c000 473a 05a7
    0000330: 000a 1906 c000 473a 05a7 000c bb00 4759
    0000340: b703 b73a 0519 0512 493a 053a 0401 3a06
    0000350: 013a 0719 0401 a500 0a19 043a 07a7 000b
    0000360: 1303 b8b8 03ba 3a07 1907 1303 bb04 bd00
    0000370: 3f59 0319 0553 1303 bcb8 0395 1303 bdb8
    0000380: 03bf 3a08 1908 b203 30a6 000f 1904 1905
    0000390: b603 c03a 06a7 000a 1908 c003 9b3a 0619
    00003a0: 062b b900 3a01 003a 053a 0401 3a06 013a
    00003b0: 0719 0401 a500 0a19 043a 07a7 000b 1303
    00003c0: c1b8 03ba 3a07 1907 1303 c204 bd00 3f59
    00003d0: 0319 0553 1303 c3b8 0395 1303 c4b8 03bf
    00003e0: 3a08 1908 b203 30a6 000f 1904 1905 b603
    00003f0: c03a 06a7 000a 1908 c003 9b3a 0619 063a
    0000400: 0401 3a05 013a 0619 0401 a500 0a19 043a
    0000410: 06a7 000b 1303 c5b8 03ba 3a06 1906 1303
    0000420: c603 bd00 3f13 03c7 b803 9513 03c8 b803
    0000430: bf3a 0719 07b2 0330 a600 0d19 04b6 03c9
    0000440: 3a05 a700 0a19 07c0 03aa 3a05 1905 2d3a
    0000450: 063a 0501 3a04 013a 0713 03cb b803 cd05
    0000460: bd00 3f59 0319 0553 5904 1906 5313 03cf
    0000470: b803 95b8 03d1 3a08 1908 b203 30a5 0033
    0000480: 1908 c102 6699 0021 b803 d213 03d3 b803
    0000490: cd12 3f01 b603 d5b6 03d6 01b6 038c c000
    00004a0: 4d3a 07a7 000a 1908 c000 4d3a 07a7 0010
    00004b0: bb00 4d59 1905 1906 b703 d83a 0719 07bf
    00004c0: 2bb9 001b 0100 c000 1c4e 2c2d b600 11b6
    00004d0: 004f 2db6 0050 b600 512a 3a04 013a 0519
    00004e0: 0413 03d9 b803 db13 03dc 1303 ddb8 03df
    00004f0: b803 e13a 0619 06b2 0330 a600 0d19 04b4
    0000500: 0012 3a05 a700 0a19 06c0 02a2 3a05 1905
    0000510: 2db6 0013 013a 0401 3a05 1904 1303 e2b8
    0000520: 03e4 1303 e513 03e6 b803 e8b8 03e1 3a06
    0000530: 1906 b203 30a6 000b b200 143a 05a7 000a
    0000540: 1906 c002 ac3a 0519 05b6 0015 b600 522a
    0000550: 3a04 013a 0519 0413 03e9 b803 eb13 03ec
    0000560: 1303 edb8 03ef b803 e13a 0619 06b2 0330
    0000570: a600 0d19 04b4 0012 3a05 a700 0a19 06c0
    0000580: 02a2 3a05 1905 2db6 0016 013a 0401 3a05
    0000590: 1904 1303 f0b8 03f2 1303 f313 03f4 b803
    00005a0: f6b8 03e1 3a06 1906 b203 30a6 000b b200
    00005b0: 143a 05a7 000a 1906 c002 ac3a 0519 05b6
    00005c0: 0015 b600 53b6 0054 b0                 
  Exception Handler Table:
    bci [87, 311] => handler: 314
    bci [87, 311] => handler: 314
    bci [87, 311] => handler: 314
  Stackmap Table:
    full_frame(@70,{Object[#208],Object[#209],Object[#63],Top,Top,Top,Object[#63]},{Object[#63]})
    full_frame(@77,{Object[#208],Object[#209],Object[#63],Top,Top,Object[#63]},{Object[#63]})
    full_frame(@87,{Object[#208],Object[#209]},{})
    full_frame(@133,{Object[#208],Object[#209],Top,Top,Object[#61],Object[#14],Object[#1016]},{})
    append_frame(@141,Top,Object[#61])
    full_frame(@196,{Object[#208],Object[#209],Top,Top,Top,Top,Top,Top,Top,Object[#63]},{})
    full_frame(@203,{Object[#208],Object[#209],Top,Top,Top,Top,Top,Object[#294]},{})
    full_frame(@235,{Object[#208],Object[#209],Top,Top,Object[#294],Object[#63],Object[#1018]},{})
    append_frame(@243,Top,Object[#63])
    full_frame(@298,{Object[#208],Object[#209],Top,Top,Top,Top,Top,Top,Top,Object[#63]},{})
    full_frame(@305,{Object[#208],Object[#209],Top,Top,Top,Top,Top,Object[#63]},{})
    full_frame(@314,{Object[#208],Object[#209]},{Object[#66]})
    full_frame(@359,{Object[#208],Object[#209],Top,Object[#66],Top,Top,Object[#63]},{})
    full_frame(@366,{Object[#208],Object[#209],Top,Object[#66],Top,Object[#306]},{})
    full_frame(@447,{Object[#208],Object[#209],Top,Object[#66],Top,Top,Object[#63]},{Object[#306],Object[#14]})
    full_frame(@454,{Object[#208],Object[#209],Top,Object[#66],Top,Object[#71]},{Object[#306],Object[#14]})
    full_frame(@457,{Object[#208],Object[#209],Top,Object[#66]},{Object[#306],Object[#14]})
    full_frame(@466,{Object[#208],Object[#209],Top,Object[#66],Top,Object[#71]},{Object[#306],Object[#14]})
    full_frame(@493,{Object[#208],Object[#209],Top,Object[#66],Object[#71],Object[#14]},{Object[#306],Object[#14]})
    full_frame(@501,{Object[#208],Object[#209],Top,Object[#66],Object[#71],Object[#14],Top,Object[#63]},{Object[#306],Object[#14]})
    full_frame(@549,{Object[#208],Object[#209],Top,Object[#66],Top,Top,Top,Top,Object[#63]},{Object[#306],Object[#14]})
    full_frame(@556,{Object[#208],Object[#209],Top,Object[#66],Top,Top,Object[#71]},{Object[#306],Object[#14]})
    full_frame(@587,{Object[#208],Object[#209],Top,Object[#66],Object[#71],Object[#14]},{Object[#306],Object[#14]})
    full_frame(@595,{Object[#208],Object[#209],Top,Object[#66],Object[#71],Object[#14],Top,Object[#63]},{Object[#306],Object[#14]})
    full_frame(@643,{Object[#208],Object[#209],Top,Object[#66],Top,Top,Top,Top,Object[#63]},{Object[#306],Object[#14]})
    full_frame(@650,{Object[#208],Object[#209],Top,Object[#66],Top,Top,Object[#71]},{Object[#306],Object[#14]})
    full_frame(@673,{Object[#208],Object[#209],Top,Object[#66],Object[#71]},{Object[#306],Object[#14]})
    full_frame(@681,{Object[#208],Object[#209],Top,Object[#66],Object[#71],Top,Object[#63]},{Object[#306],Object[#14]})
    full_frame(@722,{Object[#208],Object[#209],Top,Object[#66],Top,Top,Top,Object[#63]},{Object[#306],Object[#14]})
    full_frame(@729,{Object[#208],Object[#209],Top,Object[#66],Top,Object[#14]},{Object[#306],Object[#14]})
    full_frame(@818,{Object[#208],Object[#209],Top,Object[#66],Top,Top,Object[#63]},{})
    full_frame(@825,{Object[#208],Object[#209],Top,Object[#66],Top,Object[#71]},{})
    chop_frame(@828,2)
    append_frame(@837,Top,Object[#71])
    full_frame(@864,{Object[#208],Object[#209],Top,Object[#66],Object[#71],Object[#14]},{})
    append_frame(@872,Top,Object[#63])
    full_frame(@920,{Object[#208],Object[#209],Top,Object[#66],Top,Top,Top,Top,Object[#63]},{})
    full_frame(@927,{Object[#208],Object[#209],Top,Object[#66],Top,Top,Object[#71]},{})
    full_frame(@958,{Object[#208],Object[#209],Top,Object[#66],Object[#71],Object[#14]},{})
    append_frame(@966,Top,Object[#63])
    full_frame(@1014,{Object[#208],Object[#209],Top,Object[#66],Top,Top,Top,Top,Object[#63]},{})
    full_frame(@1021,{Object[#208],Object[#209],Top,Object[#66],Top,Top,Object[#71]},{})
    full_frame(@1044,{Object[#208],Object[#209],Top,Object[#66],Object[#71]},{})
    append_frame(@1052,Top,Object[#63])
    full_frame(@1093,{Object[#208],Object[#209],Top,Object[#66],Top,Top,Top,Object[#63]},{})
    full_frame(@1100,{Object[#208],Object[#209],Top,Object[#66],Top,Object[#14]},{})
    full_frame(@1190,{Object[#208],Object[#209],Top,Top,Top,Top,Top,Top,Object[#63]},{})
    full_frame(@1197,{Object[#208],Object[#209],Top,Top,Top,Top,Top,Object[#77]},{})
    full_frame(@1200,{Object[#208],Object[#209],Top,Top,Top,Object[#14],Object[#66]},{})
    full_frame(@1213,{Object[#208],Object[#209],Top,Top,Top,Top,Top,Object[#77]},{})
    full_frame(@1216,{Object[#208],Object[#209],Object[#200]},{})
    full_frame(@1287,{Object[#208],Object[#209],Top,Object[#28],Top,Top,Object[#63]},{Object[#200]})
    full_frame(@1294,{Object[#208],Object[#209],Top,Object[#28],Top,Object[#242]},{Object[#200]})
    full_frame(@1344,{Object[#208],Object[#209],Top,Object[#28],Top,Top,Object[#63]},{Object[#200],Object[#242],Object[#14]})
    full_frame(@1351,{Object[#208],Object[#209],Top,Object[#28],Top,Object[#240]},{Object[#200],Object[#242],Object[#14]})
    full_frame(@1405,{Object[#208],Object[#209],Top,Object[#28],Top,Top,Object[#63]},{Object[#200]})
    full_frame(@1412,{Object[#208],Object[#209],Top,Object[#28],Top,Object[#242]},{Object[#200]})
    full_frame(@1462,{Object[#208],Object[#209],Top,Top,Top,Top,Object[#63]},{Object[#200],Object[#242],Object[#14]})
    full_frame(@1469,{Object[#208],Object[#209],Top,Top,Top,Object[#240]},{Object[#200],Object[#242],Object[#14]})

    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at javassist.runtime.Desc.getClassObject(Desc.java:44)
    at javassist.runtime.Desc.getClassType(Desc.java:153)
    at javassist.runtime.Desc.getType(Desc.java:123)
    at javassist.runtime.Desc.getType(Desc.java:79)
    at be.inbo.lifewatch.seabirds.manage.controlled_vocabulary.service.ManageControlledVocabularyServiceTest.<init>(ManageControlledVocabularyServiceTest.java:66)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.createTestInstance(PowerMockJUnit44RunnerDelegateImpl.java:186)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.createTest(PowerMockJUnit44RunnerDelegateImpl.java:171)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:193)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:104)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 255

<<<END FULL STACK TRACE>>>
-----------------------------------------------------

What version of the product are you using? On what operating system?

PowerMock version: 1.5.4
Mockito version: 1.9.5

Please provide any additional information below.

There are 2 workarounds for this problem: 
(1) Uncollapse all the exceptions. That means: revert to the Java 6 situation. 
Lesser of many evils, but it does not use the Java 7 capabilities, which leads 
to more code. And in my case: reproduction of code. 
(2) Don't catch multiple exceptions, just catch (Exception e): Worst of many 
evils, in my book. Not only will I catch other exceptions (if any), which do 
not belong to the code I'm executing; you'll also loose the concept of catching 
a single type of exception. But you could also state that if you collapse all 
the exceptions, you're not interested in a single type of exception, which 
would not be a wrong assessment, I guess. 

Original issue reported on code.google.com by ger...@gmail.com on 6 Mar 2014 at 8:12

GoogleCodeExporter commented 8 years ago
is this issue fixed in latest version of PwerMock?

Original comment by bupadh...@noknok.com on 9 Feb 2015 at 7:22

GoogleCodeExporter commented 8 years ago
Haven't retried, but I don't think so, given the status of the ticket ...

Original comment by geroen.j...@gtempaccount.com on 10 Feb 2015 at 4:48

GoogleCodeExporter commented 8 years ago
I saw this issue disappear after updating to javassist-3.19.0 (along with 
powermock-easymock-1.6.0).

Original comment by pattymna...@gmail.com on 10 Feb 2015 at 4:52

GoogleCodeExporter commented 8 years ago
@pattymna : many thanks for your reply. Updating to javassist-3.19.0 really 
worked

Original comment by bupadh...@noknok.com on 11 Feb 2015 at 1:06

GoogleCodeExporter commented 8 years ago
Glad to hear it :)

Original comment by pattymna...@gmail.com on 11 Feb 2015 at 1:11

GoogleCodeExporter commented 8 years ago
Still didn't work for me even with javassist-3.19.0 and powermock 1.6.2.

Original comment by alexey.v...@gmail.com on 21 Jul 2015 at 12:37