IvanCC
IvanCC

Reputation: 1

Mockito object methods of another class

Hi I request your help to know how to emulate the method of the class Validator.validateConnection();. The problem is that the method validateConnection not exist in the class Class_Implementation and I don't want to create that method in the class Class_Implementation. The method validateConnection do a connection to the database to know if the connection is alive. When Mockito runs I get a java.Lang.NullPointerException that is caused by NamingException - need to specify class name in environment.

The real problem is when I call in Mockito test the line:

Boolean resp = mockImpl.checkConnection();

..in the checkConnection() the class Validator.validateConnection(); is trying to connect to database. I just want emulate this line and return true or false, but the problem is that the method validateConnection() is an instance of class Validator.

If need more information for fix this please let me know.

public class Class_Implementation {

    public boolean  checkConnection() {
        boolean isConnectionAlive = false;

        Validator.validateConnection();

        // another things for do

        return false;
    }

}

public class Validator {

    public static Boolean validateConnection() {
        Connection conn = new Connection();

        Boolean connectionAlive = false;
        connectionAlive = conn.isConnectionAlive();

        if (connectionAlive) {
            return true;
        } else {
            return false;
        }
    }

}

public class Connection {

    public boolean isConnectionAlive() {
        // Code for connection to DB
    }

}

// class for do the test
@RunWith(PowerMockRunner.class)
@PrepareForTest({Class_Implementation.class,Validator.class}) 
public class TestConnection {

    @Test
    public void validate_Connection() throws Exception {
        Class_Implementation mockImpl = PowerMock.createPartialMock(Class_Implementation.class);

        PowerMock.mockStatic(Validator.class);

        PowerMockito.when(mockImpl,  Validator.validateConnection() ).thenReturn(true);

        PowerMock.replayAll(mockImpl);

        Boolean resp = mockImpl.checkConnection();

        PowerMock.verifyAll();

        Validate.notNull(resp);
    }

}

Upvotes: 0

Views: 1316

Answers (2)

Stefan Birkner
Stefan Birkner

Reputation: 24510

Use a Validator object instead of its static methods and inject the Validator into Class_Implementation (by constructor). This is called Dependency Injection. In your test you can inject a mock of the Validator.

public class Validator {
  public boolean validateConnection() {
    ...
  }
}

public class Class_Implementation {
  private final Validator validator;

  public Class_Implementation(Validator validator) {
    this.validator = validator;
  }

  public boolean checkConnection() {
    ...
    validator.validateConnection();
    ...
  }
}

public public class Class_ImplementationTest {

  @Test
  public void validate_Connection() throws Exception {
    Validator validator = Mockito.mock(Validator.class);
    Mockito.when(validator.validateConnection()).thenReturn(true);

    Class_Implementation impl = new Class_Implementation(validator);
    boolean response = mockImpl.checkConnection();

    Assert.assertTrue(response);
  }
}

I made some additional changes to your code.

  1. Don't return a Boolean object if there are only two states.
  2. A Unit test tests on class and is named like the class with an additional Test prefix.
  3. You don't need PowerMock if you have nice code.

Upvotes: 1

fge
fge

Reputation: 121702

Just mock your Class_Implementation (you should change the name and stick to Java naming standards by the way) and stub the validateConnection() method:

final Class_Implementation mock = mock(Class_Implementation.class);

when(mock.checkConnection()).thenReturn(true); // or false

But anyway, what you should mock is the interface to start with.

Upvotes: 0

Related Questions