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

  • Home
  • SEARCH
  • 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 6049971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:38:11+00:00 2026-05-23T07:38:11+00:00

I’m struggling with EasyMock. I’ve written two small classes to illustrate my problem: public

  • 0

I’m struggling with EasyMock. I’ve written two small classes to illustrate my problem:

public abstract class A {
    private AtomicReference<Integer> id = new AtomicReference<Integer>(null);
    public final int getId() {
        return id.get();
    }
    public final boolean setId(int id) {
        return this.id.compareAndSet(null, id);
    }
}

public class B extends A {
}

Then I proceed to write a test method as follows:

public class EasyMockTester extends EasyMockSupport {
    @Test
    public void test() {
        B b = EasyMock.createStrictMock(B.class);
        EasyMock.expect(b.getId()).andReturn(100);
        replayAll();
        int id = b.getId();
        System.out.println("The ID is: " + id);
        verifyAll();
    }
}

The problem is that I want EasyMock to simply mock an instance of class B (my actual class isn’t empty, but instead adds more methods to the methods inherited from the abstract class).
Instead, EasyMock somehow actually goes into the code of class A and starts complaining about a NullPointerException.
How do I make EasyMock mock a class that extends an abstract class?

When I run this test I get the following Failure trace:

java.lang.NullPointerException at
com.my.project.package.tests.A.getId(A.java:9)
at
com.my.project.package.tests.EasyMockTester.test(EasyMockTester.java:11)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at
org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at
org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at
org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Oh yeah, I’m using Eclipse 3.6.2, JUnit 4.8.2 and EasyMock 3.0.

EDIT: Seems PowerMock can handle mocking final methods inherited from abstract classes! http://code.google.com/p/powermock/wiki/MockFinal

  • 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-23T07:38:11+00:00Added an answer on May 23, 2026 at 7:38 am

    I think it’s not related to abstract classes and so on. It’s caused by the fact that EasyMock can’t mock final methods. From the EasyMock documentation:

    Final methods cannot be mocked. If called, their normal code will be executed

    So, you need to make your method non-final, or use some other approach to testing that doesn’t require mocking it.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.