我正在尝试对一个写在DAO层顶部的服务层类进行单元测试,但出现了以下异常。
请引导。
异常
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
公共类MasterDaoImpl扩展BaseDao实现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;
}
}
@Override
public List<State> getAllStates() throws SQLException {
return new MasterDaoImpl(dataSource).getAllStates();
}
每次调用该方法时,该服务都会创建一个新的dao。您在测试中创建的mock没有被服务使用。
这很正常。因为被嘲笑的dao引用从未提供给您的服务。在您的MasterDataServiceImpl.getAllStates()
中,您制作了一个dao的新实例。解决方案:您可以实现一个setter,这样您就可以注入您的模拟dao。