Link to home
Start Free TrialLog in
Avatar of Srikanth Vishnuvajhala
Srikanth Vishnuvajhala

asked on

Need help to write a JUnit Test Class with Mockito/EasyMock for this Java Class

I need to test the below Java main class with JUnit and Mockito/EasyMock without using PowerMockito so that I get the Code Coverage in Jacoco. I want to have the output as "Mocked Result" without getting the actual output as "Actual Result" when I call the MainClass.process() method in the Test Class.

public class MainClass {
   
    public String process() {
          AnotherClass another = new AnotherClass();
     String result = another.getResult();
          return result;
    }
}
-----------------------------------------------------------------------------
public class AnotherClass {
      public String getResult() {
            return "Actual Result";
      }
}
----------------------------------------------------------------------------
Avatar of girionis
girionis
Flag of Greece image

If you want to mock AnotherClass then you will have to make it an instance variable.

public class MainClass {
         AnotherClass another = new AnotherClass(); 
  
    public String process() {
      String result = another.getResult();
          return result;
    }
}

Open in new window


Then when you test your main class, you will have to inject a mock AnotherClass using @InjectMocks.

public class MainClassTest {
         @Mock
         AnotherClass another;

    @InjectMocks
    MainClass main; 
  
    public String process() {
       when(another.getResult()).thenReturn("Mocked Result");

     main.process();

     verify(another).getResult();
    }
}

Open in new window


Alternatively, if you do not use any dependency injection frameworks, you can inject the instance manually with constructor (as well as setter) injection.

public class MainClass {
         AnotherClass another;

  public MainClass (AnotherClass another) {
    this.another= another;
  }
  
    public String process() {
      String result = another.getResult();
          return result;
    }
}

Open in new window


public class MainClassTest {
         @Mock
         AnotherClass another;

    MainClass main; 
  
    public String process() {
     main = new MainClass(another);

       when(another.getResult()).thenReturn("Mocked Result");

     main.process();

     verify(another).getResult();
    }
}

Open in new window

Avatar of Srikanth Vishnuvajhala
Srikanth Vishnuvajhala

ASKER

Thank you for the solution girionis but can you give me solution without changing the code.
You have to change the code, there is no other solution. You explicitly create a new instance using the new keyword:

public String process() {
          AnotherClass another = new AnotherClass();  // You cannot mock this.
     String result = another.getResult();
          return result;
    }

Open in new window

I cannot change the code but your comments are helpful. Is there any library that I can use other than Power
Mock to solve this.
I do not know of any library that can do this. I am not even sure Powermock can do it.
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial