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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:57:49+00:00 2026-06-18T11:57:49+00:00

Lets say I have the following code: Mocked object class public class SomeClass {

  • 0

Lets say I have the following code:
Mocked object class

public class SomeClass {
    private Foo someField;
    public SomeClass {
        someField = new Foo();
    }
    public Foo getSomeField { return someField; }
    public void getSomething() {}
    public boolean doSomething(Object object) {}
}

Next I have test suite

public class TestSuite {
    private ClassToTest classToTest;
    private SomeClass mock;

    @Before
    public void setUp() {
        classToTest = new ClassToTest();
        mock = EasyMock.createMock(SomeClass.class);
    }

    @Test
    public void testMethod() throws Exception { 
        mock.getSomething();
        EasyMock.replay(mock);

        classToTest.methodToTest(mock); //Where methodToTest accepts SomeClass and int

        EasyMock.verify(mock);
    }
}

And method which is being tested

public void methodToTest(SomeClass a) {
    //Logic here
    Foo b = a.getSomeField();
    b.do(); // <--- null pointer exception here because someField isn't initialized
    a.getSomething(); // <--- thing I want to test if it is being called, but can't due to exception prior to this line
    //Logic after
}

I am stuck.. So yea basically SomeClass isn’t initialized like I wanted to. Is there any workaround? Or maybe any other frameworks which can do something like that?

  • 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-18T11:57:51+00:00Added an answer on June 18, 2026 at 11:57 am

    Your methodToTest calls a.getSomeField(), but the setup part of your test doesn’t expect that call. You want something like:

    Foo foo = new Foo();
    EasyMock.expect(mock.getSomeField()).andReturn(foo);
    

    Or to stub the call:

    Foo foo = new Foo();
    EasyMock.expect(mock.getSomeField()).andStubReturn(foo);
    

    (before your call to mock.getSomething()).

    See this question for the differences between andReturn and andStubReturn.

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

Sidebar

Related Questions

Lets say that I have the following code: public class Shelter<A extends Animal, B
Lets say I have the following code: public class Base { // Some stuff
Lets say I have the following code: public class Collection implements CollectionInterface{ ElementInterface[] elementArray
Lets say I have the following code in C#: public class AppleTree { public
Lets say we have the following code class Image { public: Image(const std::string &
Lets say i have the following code private Func<T> _method; public void SetExecutableMethod<T>(Func<T> methodParam)
Lets say we have following sample code in C#: class BaseClass { public virtual
Lets say I have the following code: import collections d = collections.OrderedDict() d['foo'] =
Let's say I have the following code: class MyClass(object): def __init__(self, param): self.param =
Lets say I have the following code. double *return_array(void) { double foo[2]; foo[0] =

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.