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

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

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

    1. <legend id='gKW83'><style id='gKW83'><dir id='gKW83'><q id='gKW83'></q></dir></style></legend>

      Spring jdbcTemplate 单元测试

      时间:2023-09-25
      • <small id='IbOWm'></small><noframes id='IbOWm'>

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

        <i id='IbOWm'><tr id='IbOWm'><dt id='IbOWm'><q id='IbOWm'><span id='IbOWm'><b id='IbOWm'><form id='IbOWm'><ins id='IbOWm'></ins><ul id='IbOWm'></ul><sub id='IbOWm'></sub></form><legend id='IbOWm'></legend><bdo id='IbOWm'><pre id='IbOWm'><center id='IbOWm'></center></pre></bdo></b><th id='IbOWm'></th></span></q></dt></tr></i><div id='IbOWm'><tfoot id='IbOWm'></tfoot><dl id='IbOWm'><fieldset id='IbOWm'></fieldset></dl></div>
          <bdo id='IbOWm'></bdo><ul id='IbOWm'></ul>
            <tbody id='IbOWm'></tbody>

              <tfoot id='IbOWm'></tfoot>
              • 本文介绍了Spring jdbcTemplate 单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我是 Spring 新手,只对 JUnit 和 Mockito 有点经验

                I am new to Spring and only somewhat experienced with JUnit and Mockito

                我有以下需要单元测试的方法

                I have the following method which requires a unit test

                public static String getUserNames(final String userName {
                  List<String> results = new LinkedList<String>();
                   results =  service.getJdbcTemplate().query("SELECT USERNAME FROM USERNAMES WHERE NAME = ?", new RowMapper<String>() {
                      @Override
                      public String mapRow(ResultSet rs, int rowNum) throws SQLException {
                          return new String(rs.getString("USERNAME");
                      }
                   }
                
                   return results.get(0);      
                   },userName)
                

                有人对我如何使用 JUnit 和 Mockito 实现这一点有任何建议吗?

                Does anyone have any suggestions on how I might achieve this using JUnit and Mockito?

                提前非常感谢您!

                推荐答案

                如果你想做一个纯单元测试那就换行

                If you want to do a pure unit test then for the line

                service.getJdbcTemplate().query("....");
                

                你需要mock这个Service,然后service.getJdbcTemplate()方法返回一个mock JdbcTemplate对象,然后mock这个mocked JdbcTemplate的查询方法返回你需要的List.像这样的:

                You will need to mock the Service, then the service.getJdbcTemplate() method to return a mock JdbcTemplate object, then mock the query method of mocked JdbcTemplate to return the List you need. Something like this:

                @Mock
                Service service;
                
                @Mock
                JdbcTemplate jdbcTemplate;
                
                
                @Test
                public void testGetUserNames() {
                
                    List<String> userNames = new ArrayList<String>();
                    userNames.add("bob");
                
                    when(service.getJdbcTemplate()).thenReturn(jdbcTemplate);
                    when(jdbcTemplate.query(anyString(), anyObject()).thenReturn(userNames);
                
                    String retVal = Class.getUserNames("test");
                    assertEquals("bob", retVal);
                }
                

                以上内容不需要任何形式的 Spring 支持.如果您正在执行集成测试,您实际上想测试是否正确地从数据库中提取数据,那么您可能想要使用 Spring Test Runner.

                The above doesn't require any sort of Spring support. If you were doing an Integration Test where you actually wanted to test that data was being pulled from a DB properly, then you would probably want to use the Spring Test Runner.

                这篇关于Spring jdbcTemplate 单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用原始参数模拟方法时 Mockito 中的 NullPointerE 下一篇:如何模拟 JdbcTemplate.queryForObject() 方法

                相关文章

                最新文章

                <tfoot id='5ZC5Y'></tfoot>
                  <bdo id='5ZC5Y'></bdo><ul id='5ZC5Y'></ul>

              • <i id='5ZC5Y'><tr id='5ZC5Y'><dt id='5ZC5Y'><q id='5ZC5Y'><span id='5ZC5Y'><b id='5ZC5Y'><form id='5ZC5Y'><ins id='5ZC5Y'></ins><ul id='5ZC5Y'></ul><sub id='5ZC5Y'></sub></form><legend id='5ZC5Y'></legend><bdo id='5ZC5Y'><pre id='5ZC5Y'><center id='5ZC5Y'></center></pre></bdo></b><th id='5ZC5Y'></th></span></q></dt></tr></i><div id='5ZC5Y'><tfoot id='5ZC5Y'></tfoot><dl id='5ZC5Y'><fieldset id='5ZC5Y'></fieldset></dl></div>
                <legend id='5ZC5Y'><style id='5ZC5Y'><dir id='5ZC5Y'><q id='5ZC5Y'></q></dir></style></legend>
              • <small id='5ZC5Y'></small><noframes id='5ZC5Y'>