久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      • <bdo id='ZKO18'></bdo><ul id='ZKO18'></ul>

      <tfoot id='ZKO18'></tfoot>

      <legend id='ZKO18'><style id='ZKO18'><dir id='ZKO18'><q id='ZKO18'></q></dir></style></legend>

        <small id='ZKO18'></small><noframes id='ZKO18'>

      1. <i id='ZKO18'><tr id='ZKO18'><dt id='ZKO18'><q id='ZKO18'><span id='ZKO18'><b id='ZKO18'><form id='ZKO18'><ins id='ZKO18'></ins><ul id='ZKO18'></ul><sub id='ZKO18'></sub></form><legend id='ZKO18'></legend><bdo id='ZKO18'><pre id='ZKO18'><center id='ZKO18'></center></pre></bdo></b><th id='ZKO18'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='ZKO18'><tfoot id='ZKO18'></tfoot><dl id='ZKO18'><fieldset id='ZKO18'></fieldset></dl></div>

        如何使用 Mockito 測試 DAO 方法?

        How to test DAO methods using Mockito?(如何使用 Mockito 測試 DAO 方法?)

              <tbody id='pmo6B'></tbody>
            <legend id='pmo6B'><style id='pmo6B'><dir id='pmo6B'><q id='pmo6B'></q></dir></style></legend>
              <bdo id='pmo6B'></bdo><ul id='pmo6B'></ul>
              <tfoot id='pmo6B'></tfoot>
              <i id='pmo6B'><tr id='pmo6B'><dt id='pmo6B'><q id='pmo6B'><span id='pmo6B'><b id='pmo6B'><form id='pmo6B'><ins id='pmo6B'></ins><ul id='pmo6B'></ul><sub id='pmo6B'></sub></form><legend id='pmo6B'></legend><bdo id='pmo6B'><pre id='pmo6B'><center id='pmo6B'></center></pre></bdo></b><th id='pmo6B'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='pmo6B'><tfoot id='pmo6B'></tfoot><dl id='pmo6B'><fieldset id='pmo6B'></fieldset></dl></div>

                <small id='pmo6B'></small><noframes id='pmo6B'>

                • 本文介紹了如何使用 Mockito 測試 DAO 方法?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我已經(jīng)開始發(fā)現(xiàn) Mockito 庫,但有一個問題我沒有找到正確的答案.

                  I've started to discovered Mockito library and there is a question for which I didn't find the proper answer.

                  如果我的 UserDAO 類中有這樣的方法,可以將用戶保存在數(shù)據(jù)庫中:

                  If I have for example such method in my UserDAO class that saves user in database:

                  public class UserDAO{
                  ...
                   public void create(User user) {
                          Connection connection = null;
                          PreparedStatement pstmt = null;
                          ResultSet generatedKeys = null;
                          try {
                  
                              connection = getConnection();
                              pstmt = connection.prepareStatement(INSERT_USER,
                                      PreparedStatement.RETURN_GENERATED_KEYS);
                              int counter = 1;
                              pstmt.setString(counter++, user.getFirstName());
                              pstmt.setString(counter++, user.getLastName());
                              pstmt.setString(counter++, user.getEmail());
                              pstmt.setString(counter++, user.getPassword());
                              pstmt.setString(counter++, user.getRole());
                              pstmt.setString(counter, user.getLang());
                  
                              pstmt.execute();
                              connection.commit();
                              generatedKeys = pstmt.getGeneratedKeys();
                  
                              if (generatedKeys.next()) {
                                  user.setId(generatedKeys.getInt(Fields.GENERATED_KEY));
                              }
                          } catch (SQLException e) {
                              rollback(connection);
                              LOG.error("Can not create a user", e);
                          } finally {
                              close(connection);
                              close(pstmt);
                              close(generatedKeys);
                          }
                      }
                    ....
                  }
                  

                  我應(yīng)該如何測試它?

                  如果我想測試一個 DAO 類,那么我需要創(chuàng)建一個 DataSource 模擬、Connection 模擬、ResultSet 模擬等嗎?所以不測試數(shù)據(jù)庫本身?

                  If I want to test for example a DAO class then I need to create a DataSource mock, Connection mock, ResultSet mock etc ? And so not to test the database itself ?

                  但是如果我還想測試 dao 和 database 的行為呢?

                  But what if I want to also test the behavior of dao and database ?

                  您能否提供一些可能有用的代碼示例、鏈接并展示最佳方法?

                  Would you please produce some code samples, links that could be helpful and show best approaches of doing it ?

                  推薦答案

                  這是使用 Mockito 測試您的 UserDAO 的良好開端.此代碼使用了大量的 Mockito 功能,因此您可以了解如何使用它們.如果您有任何問題,請告訴我.

                  Here is a good start using Mockito to test your UserDAO. This code uses a good amount of the Mockito features, so you can see how to use them. Let me know if you have questions.

                  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;
                          }
                  
                      }
                  }
                  

                  這篇關(guān)于如何使用 Mockito 測試 DAO 方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數(shù)溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關(guān)系嗎?)
                  How to convert Integer to int?(如何將整數(shù)轉(zhuǎn)換為整數(shù)?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內(nèi)創(chuàng)建一個隨機打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲為 int?)
                    <legend id='qwn7V'><style id='qwn7V'><dir id='qwn7V'><q id='qwn7V'></q></dir></style></legend>
                    <i id='qwn7V'><tr id='qwn7V'><dt id='qwn7V'><q id='qwn7V'><span id='qwn7V'><b id='qwn7V'><form id='qwn7V'><ins id='qwn7V'></ins><ul id='qwn7V'></ul><sub id='qwn7V'></sub></form><legend id='qwn7V'></legend><bdo id='qwn7V'><pre id='qwn7V'><center id='qwn7V'></center></pre></bdo></b><th id='qwn7V'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='qwn7V'><tfoot id='qwn7V'></tfoot><dl id='qwn7V'><fieldset id='qwn7V'></fieldset></dl></div>
                    • <bdo id='qwn7V'></bdo><ul id='qwn7V'></ul>

                        <small id='qwn7V'></small><noframes id='qwn7V'>

                          <tbody id='qwn7V'></tbody>
                        <tfoot id='qwn7V'></tfoot>

                          • 主站蜘蛛池模板: 热久久久 | 成人影院在线 | 日韩有码一区 | 成人在线视频一区二区三区 | 一区二区免费在线观看 | 亚洲国产精品一区二区久久 | 欧美日韩中文在线 | 狼人伊人影院 | 成人一级黄色毛片 | 超碰成人在线观看 | 免费在线观看91 | 老妇激情毛片免费 | 国产视频一二三区 | 91精品国产91久久久久游泳池 | 亚洲精品99 | 日韩欧美一区二区三区免费观看 | 二区成人 | 成人国产精品免费观看 | 国产精品一区一区 | 中文字幕一区二区三区精彩视频 | 国产一级在线观看 | 黄免费观看 | 成人激情视频在线 | 日韩视频在线观看中文字幕 | 久久久久久亚洲精品 | 国产婷婷在线视频 | 一级女毛片 | 中文久久 | 男人的天堂在线视频 | 亚洲精品一区在线观看 | 中文字字幕一区二区三区四区五区 | 国产精品一区二区三区久久久 | 久久精品视频一区二区三区 | 久久精品国产v日韩v亚洲 | 国产99免费 | 欧美日韩精品亚洲 | 亚洲成人免费观看 | 欧美在线视频网站 | 米奇7777狠狠狠狠视频 | 毛片一级片| 毛片网站在线观看视频 |