0

i am facing a problem regarding class mocking in java.

I will explain the problem using dummy classes( to avoid project related security concerns) We have a class Employee

public class Employee {
public int netSalary() {
    int sal = totalSal() - 100;
    return sal;
}

public int totalSal() {
    // code to return value which is making db calls or remote calls
}

}

Now my problem is that how to test netSalary method without totalSal method being called i have tried expect().andReturn() as well as suppress(method());

But both are not working

Anil Sharma
  • 558
  • 6
  • 18

2 Answers2

1

If this was done via MVC then your employee class should have a DAO that gives access to the DB. Inject a mocked version of the DAO that is called in totalSalary.

Per Comment:

This is based on the code you have above:

public class MyTest{

    private class TestableEmployee extends Employee{

        public int totalSal(){
           return 55;
        }
    }

    @Test
    public void testIt(){
       Employee employee = new TestableEmployee();

       int netValue = employee.netSalary();

       assertEquals(netValue, 55-100);
    } 
}
John B
  • 32,493
  • 6
  • 77
  • 98
  • @John....this is not the thing i asked for. i mean we don't want to call the method although i appreciate your suggestion but bit apprehensive about this thing. – Anil Sharma Sep 14 '12 at 13:10
  • Another option is to create a subclass of `Employee` (say `TestableEmployee`) that overrides `totalSalary()` – John B Sep 14 '12 at 13:13
  • can you bit elaborate this overriding thing with reference or example.Any pointers. but i dont want to change my original code. – Anil Sharma Sep 14 '12 at 13:59
0

You can use Mockito. Mock the method:

when(employee.totalSal()).thenReturn(1000);
Kevindra
  • 1,682
  • 3
  • 24
  • 45
  • you forgot to say that `employee` should be a mock or stub – michael Sep 14 '12 at 10:05
  • @michael employee can not be mock bcoz we have to test the netSalary() method so we can not mock employee – Anil Sharma Sep 14 '12 at 10:19
  • Then in that case, these both the methods should be tested together in a unit test, because they both are tightly coupled together. You can accomplish it using in-memory databases like HSQL. – Kevindra Sep 14 '12 at 10:24
  • Mockito allows to create mocks that invokes real methods on call. For example if you create mock like `Employee employee = mock( Employee.class, withSettings().defaultAnswer( RETURNS_DEFAULTS ) );` and then you stub one method `totalSal()` like `when( employee.totalSal() ).thenReturn( 1000 );`, real call to `employee.netSalary()` will results with 900 – michael Sep 14 '12 at 12:18
  • @michael actually i have that example where we can do this with the help of mockito using spy and then using when().thenreturn() thing but the problem is that we want to do it with easymock. – Anil Sharma Sep 14 '12 at 13:07
  • @AnilSharma easymock also provides partial mocking: `Employee employee = createMockBuilder( Employee.class ).addMockedMethod( "totalSal" ).createMock();` – michael Sep 14 '12 at 14:05