0

I have following class:

public class MyClazz
{

    private static enum MyEnum
    {
        INSTANCE;

        private MyClazzB getMyClazzB()
        {
            ....
            return
        }

        final MyClazzB b = getMyClazzB();
    }

    public void methodWhichIWantTest(arguments)
    {
        //...
        //here is logic which I want to test
        //...
        MyEnum.INSTANCE.b.doSomething();//I want to mock this line. 
    }
}

I am not author of MyClazz and I do not want to change anything. I only want to test methodWhichIWantTest method. The problem is that method getMyClazzB throws exception. I do not how to mock it. How to mock it? I think that I should mock MyEnum class but it is private.

===EDIT===

I think that I should clarify my question because I got some comments. I am not author of MyClazz but this class is part of my project and I can edit this class but I would like to avoid editing as much as possible. I fixed some bug in private method of MyClazz. I would like to write some test which tests my fix. Testing private method is not good practice so I can write integration test or write unit test for some public method which calls this private method. Firstly I wanted to write integration test but after research I found that integration test is to complicated and takes me too much time. So I decided to write unit test for some public method which calls this private method. methodWhichIWantTest is this public method. Method getMyClazzB throws exception because it crates javax.naming.InitialContext and lookup some beans (It works properly on wildfly but it does not work inside simple unit test). I think that I have following options:

  1. do not test it
  2. write integration test (it is time consuming)
  3. try to mock MyEnum and write unit test
  4. try to mock InitialContext and write unit test
  5. edit source code, mock and write unit test.
  6. test private method directly using java reflection

My question is related to point 3. I do not know how to realize point 3 I was curious how to do it so I asked my question. I am afraid that realizing point 3 is not possible within a reasonable time. Thank you for help.

how-to-mock-an-enum-singleton-class-using-mockito-powermock does not solve my issue because MyEnum enum is a private. E.g. I cannot use @PrepareForTest({MyEnum.class}) annotation because MyEnum is not accessible.

Mariusz
  • 1,907
  • 3
  • 24
  • 39
  • 4
    Just wondering, but if you're not the author of `MyClazz`, why are you testing the internals of `methodWhichIWantTest`? I assume you're using this class within your own application code, so why not just mock the call `MyClazz.methodWhichIWantTest` and skip the internals? – Dan W Jan 28 '20 at 16:33
  • 1
    If `getMyClazzB` throws an exception it means you're testing poorly or you have a mistake in your code, don't try to skip the exception, handle it. – AntoineB Jan 28 '20 at 16:46
  • 1
    And this is why singletons are BAD. This is just a lazymans implementation of a singleton. If you mock this at a deep level, you'll have the mock in there for all eternity (until the JVM restart) and for all subsequent tests. If you really want to test this, you'll have to change the code and separate the singleton from your code under test. – Nicktar Jan 28 '20 at 16:48
  • Does this answer your question? [How to mock an enum singleton class using Mockito/Powermock?](https://stackoverflow.com/questions/15939023/how-to-mock-an-enum-singleton-class-using-mockito-powermock) – Kaan Jan 28 '20 at 16:56
  • Well, `doSomething` and `methodWhichIWantTest` are both methods of MyClazzB, then you don't need to mock the private enum – Renato Jan 28 '20 at 19:58

0 Answers0