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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:20:10+00:00 2026-06-04T20:20:10+00:00

I am writing an unit testing using JUNIT + Mockito to test a method

  • 0

I am writing an unit testing using JUNIT + Mockito to test a method like :

public someObject methodUnderTest(){
  SomeObject obj = SomeAbstractClass.someMethod();

  if(obj!=null){
    obj.someOtherMethod();
  }

  return someThing;
}

And I would like to mock the call on abstract Class "SomeAbstractClass" mentioned in above code fragment so i can verify call on “obj” like :

verify(SomeAbstractClass).someMethod();
verify(obj).someOtherMethod();

I have tried using mockito features like :
Mockito.CALLS_REAL_METHODS
Mockito.RETURNS_MOCKS

but they don’t work due to dependencies not available to the SomeAbstractClass.

Note:

1) SomeObject is an Interface.

2) I need a technique to test above code fragment. I am constrained to use the above code fragment and cannot change the code fragment.

  • 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-04T20:20:12+00:00Added an answer on June 4, 2026 at 8:20 pm

    Assumption: if you write unit test, I guess you still can modify tested method a bit.

    Solution:

    1. extract static method call to overridable method:
    public someObject methodUnderTest() {
        SomeObject obj = getSomeObject();
    
        if(obj!=null){
          obj.someOtherMethod();
        }
    
        return someThing;
    }
    
    protected SomeObject getSomeObject() {
        return SomeAbstractClass.someMethod();
    }
    
    1. then you can use Mockito Spy to partially mock the object you actually test:
    private ClassUnderTest classUnderTest;
    
    @Before
    public void setUp() {
        classUnderTest= new ClassUnderTest();
        classUnderTest = Mockito.spy(classUnderTest);
    }
    
    @Test
    public void test() {
        SomeObject someObject = Mockito.mock(SomeObject.class);
        when(classUnderTest.getSomeObject()).thenReturn(someObject);
        classUnderTest.methodUnderTest();
        verify(someObject).someOtherMethod();
    }
    
    @Test
    public void testNull() {
        when(classUnderTest.getSomeObject()).thenReturn(null);
        classUnderTest.methodUnderTest();
        verify(something);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing some unit test and I have a unit testing base class
I am very new to unit testing and writing/using exceptions. I am currently making
So I'm having trouble with unit testing CakePHP models. Simple stuff like writing tests
I'm writing unit tests for Spring Framework 2.5 using JUnit. More specifically, I'm using
Any Java unit testing framework that supports writing unit testing code like this: Collection<AType>
Just embarking on using a test framework for writing unit tests and also the
I'm writing integration tests using JUnit to automate the testing of a console based
I'm using Moq & NUnit as a unit test framework. I've written a method
More precisely does writing unit tests consider as BDD practice? Does unite testing consider
I have used a bit of MS VS2008 integrated Unit Testing framework for writing

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.