【问题标题】:Getting exception while testing a class using JUnit and Mockito使用 JUnit 和 Mockito 测试类时出现异常
【发布时间】:2016-01-11 23:07:01
【问题描述】:

我正在尝试对编写在 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;
    }

}

【问题讨论】:

    标签: java junit mockito


    【解决方案1】:
      @Override
        public List<State> getAllStates() throws SQLException {
            return new MasterDaoImpl(dataSource).getAllStates();
        }
    

    每次调用该方法时,服务都会创建一个新的 dao。服务未使用您在测试中创建的模拟。

    【讨论】:

      【解决方案2】:

      这很正常。因为模拟的 dao 引用永远不会提供给您的服务。 在您的 MasterDataServiceImpl.getAllStates() 中,您创建了一个新的 dao 实例。解决方案:你可以实现一个 setter,这样你就可以注入你的模拟 dao。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-20
        • 2011-07-25
        • 2020-01-23
        • 2017-06-27
        • 1970-01-01
        • 2017-03-06
        相关资源
        最近更新 更多