java.lang.IllegalArgumentException: Cannot subclass final class

11,591 views
Skip to first unread message

mike

unread,
Feb 21, 2013, 9:03:13 AM2/21/13
to PowerMock
Hi,

I am using the following in my pom.xml

<mockito-all.version>1.9.0-rc1</mockito-all.version>
<powermock.version>1.4.12</powermock.version>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito-all.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>

This is my testcase ( not all code):

import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(NodeAccess.class)
public class MyTest {

@Test
public void test() throws Exception{


//Mock static
PowerMockito.mockStatic(HifiAccess.class);

.....

}

I execute the test within eclipse ide.
Any ideas why this is not working and I get the error message when I
run the testcase?

java.lang.IllegalArgumentException: Cannot subclass final class
HifiAccess.class

//mike

Johan Haleby

unread,
Feb 21, 2013, 10:38:56 AM2/21/13
to powe...@googlegroups.com
You haven't prepared "HifiAccess" for test?

/Johan


//mike

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



mike

unread,
Feb 21, 2013, 11:59:57 AM2/21/13
to powe...@googlegroups.com
Hi,
 
I am not sure what you mean by that? I have a:
 
MockitTo.when(HifiAccess.method()) ....
 
Later in method. Or do you mean something else?
 
br,
 
//mike

mike

unread,
Feb 21, 2013, 12:19:49 PM2/21/13
to powe...@googlegroups.com
Ah I see what you mean!
 
The annotation:
 
@PrepareForTest(HifiAccess.class)
 
Sorry for faulty example.
 
br,
 
//mike

Johan Haleby

unread,
Feb 21, 2013, 12:20:00 PM2/21/13
to powe...@googlegroups.com
You're using "PowerMockito.mockStatic(HifiAccess.class);" but HifiAccess is not included in the @PrepareForTest annotation.

/Johan

mike

unread,
Feb 21, 2013, 2:52:31 PM2/21/13
to PowerMock
I have tried with:

@PrepareForTest(HifiAccess.class)

but I get the same error....

br,

//mike

On 21 Feb, 18:20, Johan Haleby <johan.hal...@gmail.com> wrote:
> You're using "PowerMockito.mockStatic(HifiAccess.class);" but HifiAccess is
> not included in the @PrepareForTest annotation.
>
> /Johan
>
>
>
> On Thu, Feb 21, 2013 at 5:59 PM, mike <mikaelpetters...@gmail.com> wrote:
> > Hi,
>
> > I am not sure what you mean by that? I have a:
>
> > MockitTo.when(HifiAccess.method()) ....
>
> > Later in method. Or do you mean something else?
>
> > br,
>
> > //mike
> > Den torsdagen den 21:e februari 2013 kl. 16:38:56 UTC+1 skrev Johan Haleby:
>
> >> You haven't prepared "HifiAccess" for test?
>
> >> /Johan
>
> >> On Thu, Feb 21, 2013 at 3:03 PM, mike <mikaelpe...@gmail.com> wrote:
>
> >>> Hi,
>
> >>> I am using the following in my pom.xml
>
> >>> <mockito-all.version>1.9.0-**rc1</mockito-all.version>
> >>> <powermock.version>1.4.12</**powermock.version>
>
> >>> <dependency>
> >>>                         <groupId>junit</groupId>
> >>>                         <artifactId>junit</artifactId>
> >>>                         <version>4.10</version>
> >>>                         <scope>test</scope>
> >>>                 </dependency>
> >>>                 <dependency>
> >>>                         <groupId>org.mockito</groupId>
> >>>                         <artifactId>mockito-all</**artifactId>
> >>>                         <version>${mockito-all.**version}</version>
> >>>                         <scope>test</scope>
> >>>                 </dependency>
> >>>                 <dependency>
> >>>                         <groupId>org.powermock</**groupId>
> >>>                         <artifactId>powermock-api-**mockito</artifactId>
> >>>                         <version>${powermock.version}<**/version>
> >>>                         <scope>test</scope>
> >>>                 </dependency>
> >>>                 <dependency>
> >>>                         <groupId>org.powermock</**groupId>
> >>>                         <artifactId>powermock-module-**
> >>> junit4</artifactId>
> >>>                         <version>${powermock.version}<**/version>
> >>>                         <scope>test</scope>
> >>>                 </dependency>
>
> >>> This is my testcase ( not all code):
>
> >>> import org.junit.runner.RunWith;
> >>> import org.mockito.Mockito;
> >>> import org.powermock.api.mockito.**PowerMockito;
> >>> import org.powermock.core.**classloader.annotations.**PrepareForTest;
> >>> import org.powermock.modules.junit4.**PowerMockRunner;
>
> >>> @RunWith(PowerMockRunner.**class)
> >>> @PrepareForTest(NodeAccess.**class)
> >>> public class MyTest {
>
> >>>         @Test
> >>>         public void test() throws Exception{
>
> >>>                 //Mock static
> >>>                 PowerMockito.mockStatic(**HifiAccess.class);
>
> >>> .....
>
> >>> }
>
> >>> I execute the test within eclipse ide.
> >>> Any ideas why this is not working and I get the error message when I
> >>> run the testcase?
>
> >>> java.lang.**IllegalArgumentException: Cannot subclass final class
> >>> HifiAccess.class
>
> >>> //mike
>
> >>> --
> >>> You received this message because you are subscribed to the Google
> >>> Groups "PowerMock" group.
> >>> To unsubscribe from this group and stop receiving emails from it, send
> >>> an email to powermock+...@**googlegroups.com.
> >>> To post to this group, send email to powe...@googlegroups.com.
>
> >>> Visit this group athttp://groups.google.com/**group/powermock?hl=en<http://groups.google.com/group/powermock?hl=en>
> >>> .
> >>> For more options, visithttps://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
> >>> .

Johan Haleby

unread,
Feb 22, 2013, 1:06:43 AM2/22/13
to powe...@googlegroups.com
Hmm I cannot see anything wrong with the code. This is really basic stuff so it really ought to work...

Regards,
/Johan

mike

unread,
Feb 22, 2013, 1:45:43 AM2/22/13
to PowerMock
I tried the following:

http://code.google.com/p/powermock/wiki/MockitoUsage

And it says I should use:

Mockito.when(Static.firstStaticMethod(param)).thenReturn(value);

and I have tried this and I get the same error. I also tried with:

PowerMockito.when(.....)

And I get the same error.
Confusing.

//mike

Matt Lachman

unread,
Feb 22, 2013, 4:33:05 AM2/22/13
to powe...@googlegroups.com, PowerMock
I think in order for anyone to help out more we need more context. Can you send a full unit test class (from package line to ending brace) with a single @Test method that reproduces the issue?

Matt

On Feb 22, 2013, at 1:45 AM, mike <mikaelpe...@gmail.com> wrote:

> johan.hal..

mike

unread,
Feb 26, 2013, 12:52:45 PM2/26/13
to powe...@googlegroups.com
Hi,

Thanks.
I will try to provide a class for that as soon as I get to the office and traveling now.

br,

//mike

mike

unread,
Mar 5, 2013, 2:04:26 AM3/5/13
to powe...@googlegroups.com
Hi,

I received help from a college. Sine I am using testng for testing I need to do the following to mock static method in a final class.

https://code.google.com/p/powermock/wiki/TestNG_usage

What I am missing is an explanation for using the powermock object factory. Anyone with that knowledge that would like to explain :-)


//mike
Reply all
Reply to author
Forward
0 new messages