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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:52:48+00:00 2026-05-30T22:52:48+00:00

I woud like to write a JUnit test to verify that the code below

  • 0

I woud like to write a JUnit test to verify that the code below uses a BufferedInputStream:

public static final FilterFactory BZIP2_FACTORY = new FilterFactory() {
    public InputStream makeFilter(InputStream in) {        
        // a lot of other code removed for clarity 
        BufferedInputStream buffer = new BufferedInputStream(in);
        return new CBZip2InputStream(buffer);
    }
};

(FilterFactory is an interface.)

My test thus far looks like this:

@Test
public void testBZIP2_FactoryUsesBufferedInputStream() throws Throwable {
    InputStream in = mock(InputStream.class);
    BufferedInputStream buffer = mock(BufferedInputStream.class);
    CBZip2InputStream expected = mock(CBZip2InputStream.class);

    PowerMockito.spy(InputHelper.BZIP2_FACTORY);  // This line fails
    whenNew(BufferedInputStream.class).withArguments(in).thenReturn(buffer);
    whenNew(CBZip2InputStream.class).withArguments(buffer).thenReturn(expected);
    InputStream observed = InputHelper.BZIP2_FACTORY.makeFilter(in);

    assertEquals(expected, observed);
}

The call to PowerMockito.spy raises an exception with this message:

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: class edu.gvsu.cis.kurmasz.io.InputHelper$1
Mockito can only mock visible & non-final classes.

What should I be using instead of PowerMocktio.spy to set up the calls to whenNew?

  • 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-30T22:52:49+00:00Added an answer on May 30, 2026 at 10:52 pm

    The message is pretty obvious: You can’t mock non-visible and final classes. Short answer : Create a named class of your anonymous one, and test this class instead!

    Long answer, let’s dig why !

    An anonymous class is final

    You instantiate an anonymous class of FilterFactory, when the compiler sees an anonymous class, it creates a final and package visible class. So the anonymous class is not mockable through standard mean i.e. through Mockito.

    Mocking anonymous class : possible but BRITTLE if not HACKY

    OK, now suppose you want to be able to mock this anonymous class through Powermock. Current compilers compile anonymous class with following scheme :

    Declaring class + $ + <order of declaration starting with 1>
    

    Mocking anonymous class possible but brittle (And I mean it)
    So supposing the anonymous class is the eleventh to be declared, it will appear as

    InputHelper$11.class
    

    So you could potentially prepare for test the anonymous class:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({InputHelper$11.class})
    public class InputHelperTest {
        @Test
        public void anonymous_class_mocking works() throws Throwable {
            PowerMockito.spy(InputHelper.BZIP2_FACTORY);  // This line fails
        }
    }
    

    This code will compile, BUT will eventually be reported as an error with your IDE. The IDE probably doesn’t know about InputHelper$11.class. IntelliJ who doesn’t use compiled class to check the code report so.

    Also the fact that the anonymous class naming actually depends on the order of the declaration is a problem, when someone adds another anonymous class before, the numbering could change.
    Anonymous classes are made to stay anonymous, what if the compiler guys decide one day to use letters or even random identifiers!

    So mocking anonymous classes through Powermock is possible but brittle, don’t ever do that in a real project!

    EDITED NOTE : The Eclipse compiler has a different numbering scheme, it always uses a 3 digit number :

    Declaring class + $ + <pad with 0> + <order of declaration starting with 1>
    

    Also I don’t think the JLS clearly specify how the compilers should name anonymous classes.

    You don’t reassign the spy to the static field

    PowerMockito.spy(InputHelper.BZIP2_FACTORY);  // This line fails
    whenNew(BufferedInputStream.class).withArguments(in).thenReturn(buffer);
    whenNew(CBZip2InputStream.class).withArguments(buffer).thenReturn(expected);
    InputStream observed = InputHelper.BZIP2_FACTORY.makeFilter(in);
    

    PowerMockito.spy returns the spy, it doesn’t change the value of InputHelper.BZIP2_FACTORY. So you would need to actually set via reflection this field. You can use the Whiteboxutility that Powermock provide.

    Conclusion

    Too much trouble to just test with mocks that the anonymous filter uses a BufferedInputStream.

    Alternative

    I would rather write the following code:

    An input helper that will use the named class, I don’t use the interface name to make clear to the user what is the intent of this filter!

    public class InputHelper {
        public static final BufferedBZIP2FilterFactory BZIP2_FACTORY = new BufferedBZIP2FilterFactory();
    }
    

    And now the filter itself :

    public class BufferedBZIP2FilterFactory {
        public InputStream makeFilter(InputStream in) {
            BufferedInputStream buffer = new BufferedInputStream(in);
            return new CBZip2InputStream(buffer);
        }
    }
    

    Now you can write a test like this :

    @RunWith(PowerMockRunner.class)
    public class BufferedBZIP2FilterFactoryTest {
    
        @Test
        @PrepareForTest({BufferedBZIP2FilterFactory.class})
        public void wraps_InputStream_in_BufferedInputStream() throws Exception {
            whenNew(CBZip2InputStream.class).withArguments(isA(BufferedInputStream.class))
                    .thenReturn(Mockito.mock(CBZip2InputStream.class));
    
            new BufferedBZIP2FilterFactory().makeFilter(anInputStream());
    
            verifyNew(CBZip2InputStream.class).withArguments(isA(BufferedInputStream.class));
        }
    
        private ByteArrayInputStream anInputStream() {
            return new ByteArrayInputStream(new byte[10]);
        }
    }
    

    But could eventually avoid powermock stuff for this test scenario if you force the CBZip2InputStream to only accept BufferedInputStream. Usually using Powermock means something is wrong with the design. In my opinion Powermock is great for legacy softwares, but can blind developers when designing new code; as they are missing the point of OOP’s good part, I would even say they are designing legacy code.

    Hope that helps !

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

Sidebar

Related Questions

I woud like to create a cross-platform drawing program. The one requirement for writing
I was recently advocating to a colleague that we replace some C# code that
I woud like to generate graphs using GraphViz and display them on an ASP.NET
I woud like to save my MS SQL Server 2005 stored procedures to .sql
I woud like to create a filled rounded rectangle at run-time and assign it
I woud like to understand what are all the benefits we have if we
I woud like to I'd like to allow a user to connect to a
I am quite new in Perl and I woud like to know which of
I have a WebSite and I woud like Deny Directory Browser from a web.config
I have x ports in my computer. I woud like to add to menu

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.