Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:02:32+00:00 2026-05-30T15:02:32+00:00

I am using Mockito 1.9 with Grails 1.3.7 and I have a strange bug.

  • 0

I am using Mockito 1.9 with Grails 1.3.7 and I have a strange bug.

The following test case in java works:

import static org.mockito.Mockito.*;

public class MockitoTests extends TestCase {

    @Test
    public void testSomeVoidMethod(){
        TestClass spy = spy(new TestClass());
        doNothing().when(spy).someVoidMethod();
    }

    public static class TestClass {

        public void someVoidMethod(){
        }
    }
}

This test in groovy does not work:

import static org.mockito.Mockito.*

public class MockitoTests extends TestCase {

    public void testSomeVoidMethod() {
        def testClassMock = spy(new TestClass())
        doNothing().when(testClassMock).someVoidMethod()
    }

}

public class TestClass{

    public void someVoidMethod(){
    }
}

This is the error message:

only void methods can doNothing()!
Example of correct use of doNothing():
    doNothing().
    doThrow(new RuntimeException())
    .when(mock).someVoidMethod();
Above means:
someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called
org.mockito.exceptions.base.MockitoException: 
Only void methods can doNothing()!
Example of correct use of doNothing():
    doNothing().
    doThrow(new RuntimeException())
    .when(mock).someVoidMethod();
Above means:
someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createPogoSite(CallSiteArray.java:129)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:146)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)

Does anymone observed the same error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:02:33+00:00Added an answer on May 30, 2026 at 3:02 pm

    The problem is Groovy is intercepting your method call before it reaches someVoidMethod. The method actually being called is getMetaClass which is not a void method.

    You can verify this is happening by replacing:

    doNothing().when(testClassMock).someVoidMethod()
    

    with:

    doReturn(testClassMock.getMetaClass()).when(testClassMock).someVoidMethod()
    

    I’m not sure you will be able to get around this issue using stock Mockito and Groovy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a test case that uses a java.beans.PropertyDescriptor using Mockito, and I want
I'm using Tomcat 6.0.33 with Java 6. I have this servlet ... public class
I'm using Mockito in some tests. I have the following classes: class BaseService {
I'm currently studying the Mockito framework and I've created several test cases using Mockito.
I'm writing a JUnit test and also using Mockito, and I want to call
What is the best way to unit test/integration test the following : @Service(fooService) public
I'm using the Mockito library for Java testing and getting errors in Mockito when
I just started using mock objects (using Java's mockito) in my tests recently. Needless
I am using mockito as mocking framework. I have a scenerio here, my when(abc.method()).thenReturn(value)
I'm trying to test some legacy code, using Mockito. I want to stub a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.