org.mockito.exceptions.misusing.MissingMethodInvocationException: testNG + Mockito + Powermock

1,574 views
Skip to first unread message

Danno

unread,
Jan 28, 2010, 1:43:44 PM1/28/10
to PowerMock
Hello, I am trying Mockito + TestNG + Powermock:

and I got the following exception:

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.invoke
(PowerMockTestNGMethodHandler.java:48)
at org.abqjug.testingtools.JukeboxTest_$
$_javassist_1.testGetExternalServiceInformation(JukeboxTest_$
$_javassist_1.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:
644)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:546)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:700)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1002)
at org.testng.internal.TestMethodWorker.invokeTestMethods
(TestMethodWorker.java:137)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:
121)
at org.testng.TestRunner.runWorkers(TestRunner.java:909)
at org.testng.TestRunner.privateRun(TestRunner.java:618)
at org.testng.TestRunner.run(TestRunner.java:499)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:332)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:327)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:299)
at org.testng.SuiteRunner.run(SuiteRunner.java:204)
at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:915)
at org.testng.TestNG.runSuitesLocally(TestNG.java:879)
at org.testng.TestNG.run(TestNG.java:787)
at org.apache.maven.surefire.testng.TestNGExecutor.run
(TestNGExecutor.java:74)
at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute
(TestNGXmlTestSuite.java:92)
at org.apache.maven.surefire.Surefire.run(Surefire.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess
(SurefireBooter.java:350)
at org.apache.maven.surefire.booter.SurefireBooter.main
(SurefireBooter.java:1021)
Caused by:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be a method call on a mock.
For example:
when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because you stub final/private/equals()
or hashCode() method.
Those methods *cannot* be stubbed/verified.

at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:343)
at
org.abqjug.testingtools.JukeboxTest.testGetExternalServiceInformation
(JukeboxTest.java:35)
at org.abqjug.testingtools.JukeboxTest_$
$_javassist_1._d3testGetExternalServiceInformation(JukeboxTest_$
$_javassist_1.java)
... 35 more

I believe I have the test written correctly, and according to the
JayWay Blog Entry. Help is very appreciated.

My dependency tree is:
[INFO] [dependency:tree {execution: default-cli}]
[INFO] org.abqjug.testingtools:testingtools:jar:1.0-SNAPSHOT
[INFO] +- org.powermock.modules:powermock-module-testng:jar:1.3.5:test
[INFO] | +- org.testng:testng:jar:jdk15:5.11:test
[INFO] | \- org.powermock:powermock-core:jar:1.3.5:test
[INFO] | +- org.powermock.reflect:powermock-reflect:jar:1.3.5:test
[INFO] | | \- org.objenesis:objenesis:jar:1.1:test
[INFO] | \- javassist:javassist:jar:3.10.0.GA:test
[INFO] +- org.powermock.api:powermock-api-mockito:jar:1.3.5:test
[INFO] | \- org.powermock.api:powermock-api-support:jar:1.3.5:test
[INFO] +- org.hamcrest:hamcrest-all:jar:1.1:test
[INFO] +- javax.mail:mail:jar:1.4.3:compile
[INFO] | \- javax.activation:activation:jar:1.1:compile
[INFO] +- org.mockito:mockito-all:jar:1.8.2:compile
[INFO] \- com.google.collections:google-collections:jar:1.0:compile


Source code:

testng.xml
==========
<?xml version="1.0" encoding="UTF-8"?>
<suite name="unit" verbose="10" object-
factory="org.powermock.modules.testng.PowerMockObjectFactory">
<test name="unit">
<packages>
<package name="org.abqjug.testingtools.*"/>
<package name="org.powermock.modules.testng.*"/>
</packages>
</test>
</suite>

Jukebox.java
===========
package org.abqjug.testingtools;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Jukebox {
private ArrayList<Album> albums;

public Jukebox() {
this.albums = new ArrayList<Album>();
}

public void addAlbum(Album album) {
this.albums.add(album);
}

public List<Album> getAlbums() {
return albums;
}

public List<Album> getAlbumsByByGroup(Group group) {
List<Album> result = new ArrayList<Album>();
for (Album album : albums) {
if (album.getArtist() instanceof Group) {
Group suspect = (Group) album.getArtist();
if (suspect.getName().equals(group.getName())) {
result.add(album);
}
}
}
return result;
}

public Map<String, String> getGetExternalServiceInformation() {
return ExternalServiceInformation.getInfo();
}
}


JukeboxTest.java
==============
package org.abqjug.testingtools;

import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static org.powermock.api.mockito.PowerMockito.when;

@PrepareForTest(Jukebox.class)
public class JukeboxTest {

@Test()
public void testGetExternalServiceInformation() throws Exception{
PowerMockito.mockStatic(ExternalServiceInformation.class);
Map<String, String> expected = new HashMap<String, String>();
expected.put("Band", "The Beatles");
expected.put("year", "1963");
when(ExternalServiceInformation.getInfo()).thenReturn
(expected);
Jukebox jukebox = new Jukebox();
assertThat(jukebox.getGetExternalServiceInformation(), is
(equalTo(expected)));
verifyStatic();
ExternalServiceInformation.getInfo();
}

@Test()
public void testAddAlbumToJukebox() {
Group coldPlay = new Group("Cold Play");
Album vivaLaVida = new Album("Viva La Vida", coldPlay);
Jukebox jukebox = new Jukebox();
jukebox.addAlbum(vivaLaVida);
assertThat(jukebox.getAlbums().size(), is(1));
List<Album> actualAlbums = jukebox.getAlbumsByByGroup
(coldPlay);
assertThat(actualAlbums, hasItem(vivaLaVida));

}
}

pom.xml
=======
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.abqjug.testingtools</groupId>
<artifactId>testingtools</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>testingtools</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>powermock-repo</id>
<url>http://powermock.googlecode.com/svn/repo/</url>
</repository>
<repository>
<id>google-collections-repo</id>
<url>http://repo2.maven.org/maven2/com/google/collections/
google-collections/1.0/</url>
</repository>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<properties>
<powermock.version>1.3.5</powermock.version>
</properties>
<dependencies>
<dependency>
<groupId>org.powermock.modules</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock.api</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</
suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>

Danno

unread,
Jan 31, 2010, 6:09:07 PM1/31/10
to PowerMock
Nevermind, @PrepareForTest was pointing to the wrong class.

>          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">

Johan Haleby

unread,
Feb 1, 2010, 2:30:35 AM2/1/10
to powe...@googlegroups.com
Hi,

Sorry that I wasn't able to reply earlier, I've been on vacation. The error message is quite confusion since you get an InvocationTargetException that sort of hides the actual exception unless you look further down the stack trace so I can see why it's confusing. I'll try to look into if it's possible to improve this. Thanks for your thorough description of the problem.

/Johan 

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.


Reply all
Reply to author
Forward
0 new messages