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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:49:30+00:00 2026-05-22T16:49:30+00:00

I have an error condition that I want to test. The behavior I want

  • 0

I have an error condition that I want to test. The behavior I want to verify is that an error message gets written to the log. Since Mockito can’t stub static methods, this is rather difficult, because I want my class-under-test to either write directly to System.err.println() or my static Log.error() method. I don’t want to have to inject a mocked “logger object” into every single object that might write error messages!

So I guess what I’m asking is, what do you think is the best way to structure my Log class and/or the class-under-test so that I can stub out the logging methods or replace them with a mocked logger?

The best answer might not even make use of Mockito, it could be anything. I’d rather not import yet another library like PowerMock, but if you have a good answer that requires something like that I wouldn’t mind seeing it.

  • 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-22T16:49:31+00:00Added an answer on May 22, 2026 at 4:49 pm

    If you want to keep your logging logic in static methods, you can still initialize real logging implementation when the class is loaded based on some system property:

    class Log {
    
      private static MyLogger logger;
    
      static {
        String className = System.getProperty("my.static.logger.class.name");
        // Instantiate your logger here...
        // By default use some DefaultLogger implementation...
      }
    
      public static void error(String message, Throwable t) {
        logger.error(message, t);
      }
    
    }
    

    Then when you run your test you can specify a mock class name using -D property. But you will need to implement your mock logging class without mockito magic.

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

Sidebar

Related Questions

I have an $.ajax call that includes both a success and error condition: $('input[name=StateName]').live('change',
I have a bunch of ajax calls that contain success and error conditions like
Hello i have error when i am forwarding page without parameter. that is happen
I experience strange problem. We have error handling in global.asax that would redirect user
I have a simple condition and want to implement it with ?: keyword but
I have a cell in Excel that I want to format differently based on
Here is what I want to do. I have a project that must be
I have the following shell script that echos output to the log and errors
I have a /lib/tasks/send_mailer_job.rb file. On production I want that file to run every
I have a component that uses log4net. I want to create unit tests, 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.