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 8546801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:07:08+00:00 2026-06-11T13:07:08+00:00

I have a very simple method that I am trying to unit test: public

  • 0

I have a very simple method that I am trying to unit test:

public class MyAntTask extends org.apache.tools.ant.Task {
    public void execute() {
        fire();
    }

    public void fire() {
        // Do stuff
    }
}

I just want to write a unit test that confirms that calling execute() always invokes fire(), so I wrote this:

@Test
public void executeCallsFire() {
    //GIVEN
    MyAntTask myTask = Mockito.mock(MyAntTask.class);

    // Configure the mock to throw an exception if the fire() method
    // is called.
    Mockito.doThrow(new RuntimeException("fired")).when(myTask).fire();

    // WHEN
    try {
        // Execute the execute() method.
        myTask.execute();

        // We should never get here; HOWEVER this is the fail() that's
        // being executed by JUnit and causing the test to fail.
        Assert.fail();
    }
    catch(Exception exc) {
        // THEN
        // The fire() method should have been called.
        if(!exc.getMessage().equals("fired"))
            Assert.fail();
    }
}

I guess (and I’m by no means an expert) Mockito normally can’t mock methods that return void, but this is a workaround. You basically say “wrap my object with a Mock that will always return a specific RuntimeException whenever a particular method is about to get executed”. So, instead of fire() actually executing, Mockito just sees that its about to execute and throws an exception instead. Execution verified? Check.

Instead of passing, it fails at the first Assert.fail() just below the call to myTask.execute().

For the life of me, I can’t figure out why. Here’s the first 10-or-so lines of the enormous stack trace JUnit is giving me for the fail:

java.lang.AssertionError
    at org.junit.Assert.fail(Assert.java:92)
    at org.junit.Assert.fail(Assert.java:100)
    at net.myproj.ant.tasks.MyAntTaskUnitTest.executeCallsFire(MyAntTaskUnitTest.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)

Any thoughts here, ye Mockito Gurus of StackOverflow? Thanks in advance!

  • 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-06-11T13:07:10+00:00Added an answer on June 11, 2026 at 1:07 pm

    Because myTask is a mock, the real object isn’t called at all. To call a real object, use a spy.

    You can test that a method is called using verify so there’s no need for the exceptions.

    public void executeCallsFire() {
        MyAntTask myTask = Mockito.spy(new MyAntTask());
    
        myTask.execute();
    
        Mockito.verify(myTask).fire();
    }
    

    Wanting to mock the object that you’re testing doesn’t seem right though. It’s usually better to design the test so that you’re verifying calls to a separate object instead.

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

Sidebar

Related Questions

So I have a very simple class that has a method called getThumbUrl() but
I have a very simple method I'm trying to test. _interactionService puts up dialog
I have a very simple WCF service running that has a single method that
I have a very simple method that allows me to specify the name of
I have a very simple C# class: namespace mybox { public class userAccount :
I have a custom listview and made overlapping very simple: in onDraw method, I
I have a very simple Mapping I'm trying to do. I'm following the NHibernate
I'm struggling with something that I am sure has a very simple method, but
I am just trying to compile and run a very simple test program, but
I have a very simple question, in fact I'm a bit frustrated that I

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.