0

I've been working on an application and need to write junit tests for the layer above the Data Access object - I need to mock the data access object and stub values to return for each method. I have tried using mockito but it doesn't seem to work, I think because it has a private constructor.

I have:

Mockito.mock(dataAccessObject.class);
Mockito.when(mockDAO.methodToStub()).thenReturn(returnValue) 

but it doesnt return the specified returnValue when called.

This is the dataAccessObject I'm tring to mock:

public class DataAccessBean extends NewDataAccessBean
    implements ISQLDataAccessBean {

// Instance of ISQLDataAccessBean 
private static ISQLDataAccessBean instance = null;

static ISQLDataAccessBean getInstance() throws TrapDABException {
    if (instance == null) {
        instance = new DataAccessBean ();
    }
    return instance;
}

private DataAccessBean () throws DAOException {
    super("JNDI_LOOKUP","ALIAS");
}

The Methods I am trying to stub values for are all public but when they are called using the mocked object, they return null..

Does anyone know a better test framework to use for this? I have also looked at powermockito plugin but have not been able to get that working either.

  • Unrelated, but your code is a badly written singleton. Your getInstance() should be "synchronized" so you don't accidentally create two instances of a singleton in two different threads. – horatius Jan 08 '16 at 14:04
  • 1
    You can try this link for how to mock singletons. http://stackoverflow.com/questions/2302179/mocking-a-singleton-class – horatius Jan 08 '16 at 14:05

0 Answers0