chiffchaff / powermock

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

Static method mocking with Mockito 1.9.5 and Powermock fails with Java 8 Consumer Interface #490

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Create class that implements Consumer interface (ie IntConsumer):
import java.io.PrintStream;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;

public class FizzBuzz implements IntConsumer {

    public static boolean isFizz(int i) {
        return i % 5 == 0;
    }

    public static void run(PrintStream outputStream, int max) {
        IntStream.range(0, max).forEach(new FizzBuzz());
    }

    @Override
    public void accept(int value) {

    }
}

Create test case that does:
@RunWith(PowerMockRunner.class)
@PrepareForTest(FizzBuzz.class)
public class FizzBuzzTest extends TestCase {

    public static final int ANY_NUMBER = 5;

    public void testFizzOnThreeShouldReturnFalse() {
        Assert.assertThat(FizzBuzz.isFizz(3), is(false));
    }

    public void testFizzOnFiveShouldReturnTrue() {
        Assert.assertThat(FizzBuzz.isFizz(5), is(true));
    }

    @Test
    public void acceptOutputsFizzForFizz() {
        PrintStream stream = mock(PrintStream.class);

        PowerMockito.mockStatic(FizzBuzz.class);
        Mockito.when(FizzBuzz.isFizz(ANY_NUMBER)).thenReturn(true);

        new FizzBuzz().accept(ANY_NUMBER);

        Mockito.verify(stream, times(10)).println("fizz");
    }
}

What is the expected output? 
Unsatisfied invocation, as there is no implementation in that method that is 
being tested yet...

This works when you remove the 'implements IntConsumer' from the 
implementation. It also seems to fail on the @PrepareForTest(FizzBuzz.class) 
part.

What do you see instead?
/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/bin/java -ea 
-Didea.launcher.port=7543 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 
13.app/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA 
13.app/lib/idea_rt.jar:/Applications/IntelliJ IDEA 
13.app/plugins/junit/lib/junit-rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0
.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0
.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Con
tents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Cont
ents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Conten
ts/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/H
ome/lib/tools.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/j
re/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
/jre/lib/htmlconverter.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Conten
ts/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Conten
ts/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/
Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Hom
e/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Hom
e/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/
jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Cont
ents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Cont
ents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/C
ontents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Conte
nts/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk
/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.
jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8
.0.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachine
s/jdk1.8.0.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMa
chines/jdk1.8.0.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtua
lMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Ja
va/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Lib
rary/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/
Users/shendriks/projects/katas/fizzbuzz/java/target/test-classes:/Users/shendrik
s/projects/katas/fizzbuzz/java/target/classes:/Users/shendriks/.m2/repository/ju
nit/junit/4.8.2/junit-4.8.2.jar:/Users/shendriks/.m2/repository/org/hamcrest/ham
crest-all/1.3/hamcrest-all-1.3.jar:/Users/shendriks/.m2/repository/org/mockito/m
ockito-all/1.9.5/mockito-all-1.9.5.jar:/Users/shendriks/.m2/repository/org/power
mock/powermock-module-junit4/1.5.4/powermock-module-junit4-1.5.4.jar:/Users/shen
driks/.m2/repository/org/powermock/powermock-module-junit4-common/1.5.4/powermoc
k-module-junit4-common-1.5.4.jar:/Users/shendriks/.m2/repository/org/powermock/p
owermock-core/1.5.4/powermock-core-1.5.4.jar:/Users/shendriks/.m2/repository/org
/powermock/powermock-reflect/1.5.4/powermock-reflect-1.5.4.jar:/Users/shendriks/
.m2/repository/org/objenesis/objenesis/2.1/objenesis-2.1.jar:/Users/shendriks/.m
2/repository/org/javassist/javassist/3.18.1-GA/javassist-3.18.1-GA.jar:/Users/sh
endriks/.m2/repository/org/powermock/powermock-api-mockito/1.5.4/powermock-api-m
ockito-1.5.4.jar:/Users/shendriks/.m2/repository/org/powermock/powermock-api-sup
port/1.5.4/powermock-api-support-1.5.4.jar" 
com.intellij.rt.execution.application.AppMain 
com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 
com.fundynamic.katas.FizzBuzzTest
Internal Error occured.
java.lang.IllegalStateException: Failed to transform class with name 
com.fundynamic.katas.FizzBuzz. Reason: 
javassist.bytecode.InterfaceMethodrefInfo cannot be cast to 
javassist.bytecode.MethodrefInfo
    at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:214)
    at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:144)
    at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:340)
    at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
    at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
    at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
    at sun.reflect.annotation.AnnotationParser.parseSig(AnnotationParser.java:439)
    at sun.reflect.annotation.AnnotationParser.parseClassValue(AnnotationParser.java:420)
    at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:724)
    at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:531)
    at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:355)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
    at java.lang.Class.createAnnotationData(Class.java:3410)
    at java.lang.Class.annotationData(Class.java:3399)
    at java.lang.Class.getAnnotations(Class.java:3335)

What version of the product are you using? On what operating system?
Mockito 1.9.5, Powermock 1.5.4

Mac OS X 10.9 (Mavericks) with Java 8.

Please provide any additional information below.
POM.xml dependencies:
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <powermock.version>1.5.4</powermock.version>
  </properties>

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-all</artifactId>
      <version>1.3</version>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.9.5</version>
    </dependency>
    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-junit4</artifactId>
      <version>${powermock.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-api-mockito</artifactId>
      <version>${powermock.version}</version>
      <scope>test</scope>
    </dependency>
</dependencies>

Original issue reported on code.google.com by stefanhe...@gmail.com on 24 Mar 2014 at 9:20

GoogleCodeExporter commented 9 years ago
I don't think Javassist has support for Java 8 yet. Please report to Javassist 
as well.

Original comment by johan.ha...@gmail.com on 25 Mar 2014 at 11:15

GoogleCodeExporter commented 9 years ago
Done at Javassist - https://issues.jboss.org/browse/JASSIST-220

Original comment by stefanhe...@gmail.com on 26 Mar 2014 at 8:12