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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:13:39+00:00 2026-06-17T19:13:39+00:00

I understand you can’t mock static methods using mockito. But the method I am

  • 0

I understand you can’t mock static methods using mockito. But the method I am trying to mock is not static but it has a call to static methods in it. So can I mock this method?

I get exception when I run the test. Is the call to the static method the reason for this exception?

The Class to be tested:

public class MyAction{
        public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
            MyService service = new MyService();
            **form.setList(service.getRecords(searchRequest));**
        }
    }

Mocked class and method:

public class MyService{
    public List getRecords(SearchRequest sr){
        List returnList = new ArrayList();
        MyDao dao = DAOFactory.getInstance().getDAO();---->Call to static method
        // Some codes
        return returnList;
    }
}

Class with static method:

public class DAOFactory{
        private static DAOFactory instance = new DAOFactory();

         public static DAOFactory getInstance() {------> The static method
            return instance;
        }       
    }

My Test:

@Test
public void testSearch() throws Exception{
    MyService service = mock(MyService.class);
    MyAction action = new MyAction();

    List<MyList> list = new ArrayList<MyList>();
    when(service.getRecords(searchRequest)).thenReturn(list);
    ActionForward forward = action.search(mapping, form, request, response);        
}

This is the stack trace when I run the test. Please keep in mind that I have changed the names of the classes for simplicity.
enter image description here

  • 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-17T19:13:40+00:00Added an answer on June 17, 2026 at 7:13 pm

    The problem is that your search method cannot use the service mock, because it creates its own instance by MyService service = new MyClass();. So you must refactor the MyAction class to allow MyService injection and inject a mock in it. Or use more heavy weapon – PowerMock.

    Easiest and safest Refactoring

    Use in your IDE “extract method” refactoring to extract construction “new MyClass()”. So it will become:

    public class MyAction{
            public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
                MyService service = getMyService();
                **form.setList(service.getRecords(searchRequest));**
            }
            MyService getMyService() {
              return new MyClass();
            }
    
        }
    

    then in your unit test you could inject a mock by creating an inner subclass:

    public class MyActionTest {
       private MyService service = mock(MyService.class);
       private MyAction action = new MyAction(){
           @Override
           MyService getMyService() {return service;}
       };
    
        @Test
        public void testSearch() throws Exception{
    
            List<MyList> list = new ArrayList<MyList>();
            when(service.getRecords(searchRequest)).thenReturn(list);
            ActionForward forward = action.search(mapping, form, request, response);        
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far I can understand HTML.ActionLink and URL.RouteURL but still trying to absorb where
1) I understand I can call this $memcache_obj = memcache_connect('memcache_host', 11211); in the header
So this may be vauge but hold on and see if you can understand
I can understand using properties for messages and international settings but I feel there
.NET has this concept of Application Domains which from what I understand can be
I can understand the difference between a signed char and an unsigned one. But
I understand I can create an enum like this: public enum MyEnum { ONE(1),
I don't understand why can't i access values like this: object = { test:{
I'm trying to understand how can a magnetic link work, as I've read they
I can understand that WCF is in general better than Remoting, but the two

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.