0

I have to test a method inside a class. But the class itself has some inside properties and methods that are inherited and protected.

if fact, what I have is:

public class MyActionTest {
    @Test
    public void goToSearchBaseTest() {
        MyAction myAction = new MyAction();
        myAction.search();
        assert (true);
    }

}

Then

public class MyAction extends BaseAction{

    ...

    public ActionForward search(){

        if(this.getLog().isDebugEnabled()) {
            this.getLog().debug("init --> search()");
        }
    }

}

And finally

public class BaseAction{    

    ...

    protected Log log;

    ...

    public ActionForward execute( ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response ) 
                throws Exception {
        ...

        log = LogFactory.getLog( this.getClass() );

        ...
}

Thus, my little test breaks in the first line: Trying to access the logger, and I cannot access it (Nor put a mocked logger) since it's created way, way before in the parent class, which I cannot modify.

My usual JUnit and Mockito tricks aren't enough, and I know not so much about powermock

Can anybody help me with this one?

  • check this one https://stackoverflow.com/questions/34571/how-do-i-test-a-class-that-has-private-methods-fields-or-inner-classes – Tim Aug 24 '17 at 14:44

1 Answers1

1

Give it a try

@RunWith(PowerMockRunner.class)
@PrepareForTest(LogFactory.class)    
public class MyActionTest {

    @InjectMocks
    MyAction myAction ;
    @Mock
    Log log

    public void setUp() throws Exception {
        PowerMockito.mockStatic(LogFactory.class);
        PowerMockito.when(LogFactory.getLog(any(BaseAction.class))).thenReturn(log);


        @Test
        public void goToSearchBaseTest() {
            myAction.search();
            assert (true);
        }

    }
want2learn
  • 2,471
  • 2
  • 20
  • 37
  • Should work, but of course, the mocked `Log` object will be replaced as soon as `execute` is called. – Florian Schaetz Aug 24 '17 at 18:54
  • You can mock your LogFactory. – want2learn Aug 24 '17 at 18:55
  • Not mine, and you could, but only with static mocking and that's more an act of desparation than a wise choice ;-) And don't get me wrong, I think your solution sounds entirely correct, everything else is just a problem of the bad code. And personally, I don't even get why the logger isn't simply initialized in the beginning instead of somewhere in a method... – Florian Schaetz Aug 24 '17 at 19:40
  • @FlorianSchaetz updated my answer with powermock. give it a try. – want2learn Aug 24 '17 at 21:24
  • Why should I? As I pointed out, I am not author of the question and I know how to use PowerMock. I just don't like it, because the only reason you need PowerMock is bad code that you cannot change. In most other situations, you should refactor the code instead of using PowerMock ;-) But since the OP cannot do that here, PowerMock might be needed, IF he actually wants to mock the Log object instead of just making sure it's not null at the beginning. – Florian Schaetz Aug 25 '17 at 06:03
  • I am sorry I thought you are OP. – want2learn Aug 25 '17 at 13:19