栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Mockito测试DAO方法?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Mockito测试DAO方法?

这是使用Mockito测试UserDAO的一个很好的开始。该代码使用了大量的Mockito功能,因此您可以了解如何使用它们。如果您有任何问题,请告诉我。

import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.sql.DataSource;import org.junit.After;import org.junit.AfterClass;import org.junit.Before;import org.junit.BeforeClass;import org.junit.Test;import static org.junit.Assert.*;import org.junit.runner.RunWith;import static org.mockito.Matchers.anyInt;import static org.mockito.Matchers.anyString;import org.mockito.Mock;import static org.mockito.Mockito.doNothing;import static org.mockito.Mockito.times;import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;import org.mockito.runners.MockitoJUnitRunner;@RunWith(MockitoJUnitRunner.class)public class TestUserDAO {    @Mock    DataSource mockDataSource;    @Mock    Connection mockConn;    @Mock    PreparedStatement mockPreparedStmnt;    @Mock    ResultSet mockResultSet;    int userId = 100;    public TestUserDAO() {    }    @BeforeClass    public static void setUpClass() throws Exception {    }    @AfterClass    public static void tearDownClass() {    }    @Before    public void setUp() throws SQLException {        when(mockDataSource.getConnection()).thenReturn(mockConn);        when(mockDataSource.getConnection(anyString(), anyString())).thenReturn(mockConn);        donothing().when(mockConn).commit();        when(mockConn.prepareStatement(anyString(), anyInt())).thenReturn(mockPreparedStmnt);        donothing().when(mockPreparedStmnt).setString(anyInt(), anyString());        when(mockPreparedStmnt.execute()).thenReturn(Boolean.TRUE);        when(mockPreparedStmnt.getGeneratedKeys()).thenReturn(mockResultSet);        when(mockResultSet.next()).thenReturn(Boolean.TRUE, Boolean.FALSE);        when(mockResultSet.getInt(Fields.GENERATED_KEYS)).thenReturn(userId);    }    @After    public void tearDown() {    }    @Test    public void testCreateWithNoExceptions() throws SQLException {        UserDAO instance = new UserDAO(mockDataSource);        instance.create(new User());        //verify and assert        verify(mockConn, times(1)).prepareStatement(anyString(), anyInt());        verify(mockPreparedStmnt, times(6)).setString(anyInt(), anyString());        verify(mockPreparedStmnt, times(1)).execute();        verify(mockConn, times(1)).commit();        verify(mockResultSet, times(2)).next();        verify(mockResultSet, times(1)).getInt(Fields.GENERATED_KEYS);    }    @Test(expected = SQLException.class)    public void testCreateWithPreparedStmntException() throws SQLException {         //mock         when(mockConn.prepareStatement(anyString(), anyInt())).thenThrow(new SQLException());        try { UserDAO instance = new UserDAO(mockDataSource); instance.create(new User());        } catch (SQLException se) { //verify and assert verify(mockConn, times(1)).prepareStatement(anyString(), anyInt()); verify(mockPreparedStmnt, times(0)).setString(anyInt(), anyString()); verify(mockPreparedStmnt, times(0)).execute(); verify(mockConn, times(0)).commit(); verify(mockResultSet, times(0)).next(); verify(mockResultSet, times(0)).getInt(Fields.GENERATED_KEYS); throw se;        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516540.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号