Why mockito returning me MockitoException while I'm expecting ClassNotFoundException?

16,795 views
Skip to first unread message

LOH KOK HOE

unread,
Jan 7, 2014, 2:04:40 AM1/7/14
to powe...@googlegroups.com
Assume I have a class that throw ClassNotFoundException like this:

public class RealOne {
   public void funcA() {
      try {
         ...
      }
      catch( ClassNotFoundException e ) {}
   }
}

When I unit test this method like this: 

@Test(expected=ClassNotFoundException.class)
public void testingClassNotFoundException() {

   stubber.funcA();
}

I get following error:

java.lang.Exception: Unexpected exception, expected<java.lang.ClassNotFoundException> but was<org.mockito.exceptions.base.MockitoException>
...
...
Caused by: org.mockito.exceptions.base.MockitoException: 
Checked exception is invalid for this method!
Invalid: java.lang.ClassNotFoundException

May I know how could I expect ClassNotFoundException but not MockitoException?

Johan Haleby

unread,
Jan 7, 2014, 2:11:52 AM1/7/14
to powe...@googlegroups.com
Does your code throw a ClassNotFoundException? It doesn't look like it in your example.


--
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.
For more options, visit https://groups.google.com/groups/opt_out.

LOH KOK HOE

unread,
Jan 7, 2014, 2:25:31 AM1/7/14
to powe...@googlegroups.com
I'm sure ClassNotFoundException will be throw because I have this code:

PowerMockito.doThrow(new ClassNotFoundException())
.when(this.stubber, PowerMockito.method(RealOne.class, "funcA"))
.withNoArguments();

LOH KOK HOE

unread,
Jan 7, 2014, 2:42:34 AM1/7/14
to powe...@googlegroups.com
I just found something interesting, if I doThrow(new HibernateException("")), it is OK. If I doThrow(new ClassNotFoundException()), there is an exception occur in the PowerMockito. I think I have accidentally trigger a Mockito bug. Can someone please verify whether I can mock ClassNotFoundException being thrown?

Johan Haleby

unread,
Jan 7, 2014, 3:14:33 AM1/7/14
to powe...@googlegroups.com
ClassNotFoundException is a checked exception so you need to declare it on the method.

/Johan

LOH KOK HOE

unread,
Jan 7, 2014, 4:11:39 AM1/7/14
to powe...@googlegroups.com
You mention declare, do you mean this?

import java.lang.ClassNotFoundException;

I have already done that but still failed.

Johan Haleby

unread,
Jan 7, 2014, 7:12:36 AM1/7/14
to powe...@googlegroups.com
You need to declare it on the method.

/Johan

LOH KOK HOE

unread,
Jan 7, 2014, 7:58:25 PM1/7/14
to powe...@googlegroups.com
May I know how could I declare it on method? Any sample?

Matt Lachman

unread,
Jan 7, 2014, 8:26:36 PM1/7/14
to powe...@googlegroups.com
Java 101:

public void foo() throws Exception

Where Exception is whatever type thrown that does not inherit from RuntimeException.

On Jan 7, 2014, at 7:58 PM, LOH KOK HOE <huah...@gmail.com> wrote:

May I know how could I declare it on method? Any sample?

LOH KOK HOE

unread,
Jan 8, 2014, 7:28:46 AM1/8/14
to powe...@googlegroups.com
ic, that one is actually called declare on method. Got you.


--
You received this message because you are subscribed to a topic in the Google Groups "PowerMock" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/powermock/hCfHV8bbQb0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to powermock+...@googlegroups.com.

Sean Kelly

unread,
Jul 24, 2014, 8:28:47 PM7/24/14
to powe...@googlegroups.com
I thought I was having the same problem, but now I understand thanks to the explanation above. Here's a bit more... if you have:


public class RealOne {
   public void funcA() {
      try {
         someOtherMethod();
      }
      catch( ClassNotFoundException e ) {}
   }

  private void someOtherMethod() throws ClassNotFoundException {
    throw new ClassNotFoundException();
  }
}

When you mock, you can't say that funcA throws ClassNotFoundException() because it doesn't. someOtherMethod() throws that exception, so you have to mock like:

doThrow(new ClassNotFoundException()).when(realOneSpyInstance).someOtherMethod();

funcA doesn't throw the exception, therefore you can't tell mockito to throw that exception from funcA - funcA just *catches* it.

HTH...
- SK

LOH KOK HOE

unread,
Jul 27, 2014, 7:58:58 AM7/27/14
to powe...@googlegroups.com
Nice one ;)
--
You received this message because you are subscribed to a topic in the Google Groups "PowerMock" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/powermock/hCfHV8bbQb0/unsubscribe.
To unsubscribe from this group and all its topics, 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.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages