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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:33:49+00:00 2026-05-24T03:33:49+00:00

I am trying to mock a DAO with JMockit: public interface MyDao { Details

  • 0

I am trying to mock a DAO with JMockit:

public interface MyDao {
    Details getDetailsById(int id);
}

With this test class:

public class TestClass {

    @Test
    public void testStuff(final MyDao dao) throws Exception
    {
        new Expectations()
        {
            {
                // when we try to get the message details, return our sample
                // details
                dao.getDetailsById((Integer) any);  ***THROWS AN NPE
                result = sampleDetails;
            }
        };

        ClassUsingDao daoUser = new ClassUsingDao(dao);
        // calls dao.getDetailsById()
        daoUser.doStuff();
}

When the dao object is used in the Expectations block, an NPE is thrown. I’ve tried moving the declaration of dao to a member variable annotated with @Mocked, but the same thing happens. I’ve also tried using a concrete implementation of MyDao, and the same thing happens.

  • 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-24T03:33:50+00:00Added an answer on May 24, 2026 at 3:33 am

    It’s not dao that’s null, but any. The unboxing from Integer (after your cast) to int involves a dereference, which throws a NullPointerException. Try using anyInt instead.

    I don’t think the jMockit documentation talks about what the actual value of Expectations.any is, but note that it can be successfully cast to any other type (you can say (String)any and (Integer)any). The only value in Java for which all casts will always succeed is null. So, Expectations.any must be null. Bit of a surprise, but inevitable really.

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

Sidebar

Related Questions

I'm trying to mock an interface's events like the following: [TestMethod] public void NeedingDataFiresEvents()
I'm trying to mock a mapping interface IMapper : public interface IMapper<TFoo, TBar> {
I am trying to mock a class using Mockito and also using PowerMock. This
I spent an evening trying to mock an object that implements IQueryable: public interface
I'm trying to mock the java.lang.reflect.Method, here my code: @RunWith(PowerMockRunner.class) @PrepareForTest(Method.class) public class AuditDataTest
I'm trying to create a mock HttpContextBase for unit test. var fakePrinciple = new
Trying to mock the following method: bool IsLoginValid(LoginViewModel viewModel, out User user); Tried this
I'm trying to mock the SecurityManager class. When I run the following code, Mockito
I´m encountering this problem trying to mock some objects that receive complex lambda expressions
Im trying to mock a method that is ineherited from a parent class that

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.