0

I am trying to unit test a service layer class that is written on top of DAO layer but getting following exception.

Please guide.

Exception

Running com.ministore.service.MasterDataServiceTest
actStates = null
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.941 sec <<< FAILURE!
testGetAllStates(com.ministore.service.MasterDataServiceTest)  Time elapsed: 0.8 sec  <<< FAILURE!
junit.framework.AssertionFailedError
    at junit.framework.Assert.fail(Assert.java:48)
    at junit.framework.Assert.assertTrue(Assert.java:20)
    at junit.framework.Assert.assertNotNull(Assert.java:218)
    at junit.framework.Assert.assertNotNull(Assert.java:211)
    at com.ministore.service.MasterDataServiceTest.testGetAllStates(MasterDataServiceTest.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Failed tests:   testGetAllStates(com.ministore.service.MasterDataServiceTest)

MasterDataServiceTest.java

public class MasterDataServiceTest {

    @Mock
    DataSource dataSource;
    @Mock
    MasterDao dao;
    @Mock
    Connection cn;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testGetAllStates() throws SQLException {
        MasterDataService dataService = new MasterDataServiceImpl(dataSource);

        when(dao.getAllStates()).thenReturn(new ArrayList<State>());
        when(dataSource.getConnection()).thenReturn(cn);

        List<State> actStates = dataService.getAllStates();
        System.out.println("actStates = " + actStates);
        assertNotNull(actStates);
    }

}

MasterDataServiceImpl.java

public class MasterDataServiceImpl implements MasterDataService {

    private final DataSource dataSource;

    public MasterDataServiceImpl(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public List<State> getAllStates() throws SQLException {
        return new MasterDaoImpl(dataSource).getAllStates();
    }

}

MasterDaoImpl.java

public class MasterDaoImpl extends BaseDao implements MasterDao {

private static final Logger LOG = Logger.getLogger(MasterDaoImpl.class);

public MasterDaoImpl(DataSource dataSource) {
    super(dataSource);
}

@Override
public List<State> getAllStates() throws SQLException {
    String sql = "select * from states a order by a.name";
    return QueryExecutor.executeStatesQuery(dataSource.getConnection(), sql);
}

}

BaseDao.java

public abstract class BaseDao {

    protected DataSource dataSource;

    public BaseDao(DataSource dataSource) {
        this.dataSource = dataSource;
    }

}

2 Answers 2

1
  @Override
    public List<State> getAllStates() throws SQLException {
        return new MasterDaoImpl(dataSource).getAllStates();
    }

The service is creating a new dao each time the method is called. The mock you are creating in the test is not being used by the service.

Sign up to request clarification or add additional context in comments.

Comments

0

It's normal. because the mocked dao reference is never given to your service. In your MasterDataServiceImpl.getAllStates() you make a new instance of your dao. Solution : you can implement a setter, so you can inject your mocked dao.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.