Java Spring jdbcTemplate 单元测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24123683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring jdbcTemplate unit testing
提问by Mat
I am new to Spring and only somewhat experienced with JUnit and Mockito
我是 Spring 的新手,只对 JUnit 和 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)
Does anyone have any suggestions on how I might achieve this using JUnit and Mockito?
有没有人对我如何使用 JUnit 和 Mockito 实现这一目标有任何建议?
Thank you very much in advance!
非常感谢您提前!
采纳答案by wxkevin
If you want to do a pure unit test then for the line
如果您想进行纯单元测试,那么对于该行
service.getJdbcTemplate().query("....");
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:
您需要模拟服务,然后使用 service.getJdbcTemplate() 方法返回模拟 JdbcTemplate 对象,然后模拟模拟 JdbcTemplate 的查询方法以返回您需要的列表。像这样的东西:
@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);
}
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 支持。如果您在进行集成测试时实际上想要测试是否正确地从数据库中提取数据,那么您可能想要使用 Spring Test Runner。
回答by Federico Piazza
You need to use Spring Test to do this. Take a look a the documentation:
您需要使用 Spring Test 来执行此操作。看一下文档:
http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/testing.html
http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/testing.html
You need to create a test using @RunWith and use your spring conf with @ContextConfiguration:
您需要使用@RunWith 创建一个测试,并将您的 spring conf 与 @ContextConfiguration 一起使用:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config.xml")
public class SpringAppTests {
@Autowired
private HelloService helloService;
@Test
public void testSayHello() {
Assert.assertEquals("Hello world!", helloService.sayHello());
}
}
Here you have a little explanation from the documentation:
在这里,您可以从文档中得到一些解释:
@Runwith
@Runwith
@Runwith(SpringJUnit4ClassRunner.class), developers can implement standard JUnit 4.4 unit and integration tests and simultaneously reap the benefits of the TestContext framework such as support for loading application contexts, dependency injection of test instances, transactional test method execution, etc.
@Runwith(SpringJUnit4ClassRunner.class),开发者可以实现标准的 JUnit 4.4 单元和集成测试,同时获得 TestContext 框架的好处,比如支持加载应用程序上下文、测试实例的依赖注入、事务测试方法执行等。
@ContextConfiguration
@ContextConfiguration
@ContextConfiguration Defines class-level metadata that is used to determine how to load and configure an ApplicationContext for integration tests. Specifically, @ContextConfiguration declares either the application context resource locations or the annotated classes that will be used to load the context. Hope to help
@ContextConfiguration 定义类级元数据,用于确定如何为集成测试加载和配置 ApplicationContext。具体来说,@ContextConfiguration 声明应用程序上下文资源位置或将用于加载上下文的带注释的类。希望有所帮助
Hope to help
希望有所帮助