我有一个用groovy编写的服务类,如下所示:
import groovy.sql.Sql import javax.sql.DataSource import java.sql.SQLException class DatabaseService { DataSource dataSource; void registerUser(User user) { try { def sql = new Sql(dataSource); sql.call("{call regUser(?)}", [user.name]) } catch (SQLException e) { log.error("unable to register the user", e) throw e; } } }我想在这里做的是模拟sql.call方法,抛出一个SQLException。 此时编写集成测试是不可能的。
这是我到目前为止:
void "test registerDeveloper exception handling"() { String expectedErrorMsg = "exception from test" DataSource mockedSource = Mock(DataSource) User user = new User(name: "joe") Sql mockedSql = Mock(Sql) DatabaseService databaseService = new DatabaseService(dataSource:mockedSource) given: mockedSql.call(_,_) << {throw new SQLException()} when: databaseService.registerUser(user) then: def exception = thrown(SQLException) exception.message == expectedErrorMsg }使用上面的代码,我收到此错误:
groovy.lang.MissingMethodException:没有方法签名:java.lang.Integer.leftShift()适用于参数类型:(UcopRip.DatabaseServiceSpec $ __ spock_feature_0_0_closure1)值:[UcopRip.DatabaseServiceSpec$__spock_feature_0_0_closure1@77049094]可能的解决方案:leftShift(java .lang.Number),rightShift(java.lang.Number)
如果我将<<更改为>>,我会收到此错误:类型为'java.sql.SQLException'的预期异常,但得到'java.lang.NullPointerException',poiting到DatabaseService类中的sql.call行。
有谁知道我做错了什么? 我当然不熟悉使用SPOCK进行测试,但似乎无法在网上找到人们嘲笑SQL方法的地方。
I have a service class written in groovy, as follows:
import groovy.sql.Sql import javax.sql.DataSource import java.sql.SQLException class DatabaseService { DataSource dataSource; void registerUser(User user) { try { def sql = new Sql(dataSource); sql.call("{call regUser(?)}", [user.name]) } catch (SQLException e) { log.error("unable to register the user", e) throw e; } } }What I want to do here, is Mock the sql.call method, to throw an SQLException. Writing an integration test to do this, at this time, is not possible.
Here is what I have so far:
void "test registerDeveloper exception handling"() { String expectedErrorMsg = "exception from test" DataSource mockedSource = Mock(DataSource) User user = new User(name: "joe") Sql mockedSql = Mock(Sql) DatabaseService databaseService = new DatabaseService(dataSource:mockedSource) given: mockedSql.call(_,_) << {throw new SQLException()} when: databaseService.registerUser(user) then: def exception = thrown(SQLException) exception.message == expectedErrorMsg }With the above code, I get this error:
groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.leftShift() is applicable for argument types: (UcopRip.DatabaseServiceSpec$__spock_feature_0_0_closure1) values: [UcopRip.DatabaseServiceSpec$__spock_feature_0_0_closure1@77049094] Possible solutions: leftShift(java.lang.Number), rightShift(java.lang.Number)
If I change << to >>, I get this error: Expected exception of type 'java.sql.SQLException', but got 'java.lang.NullPointerException', poiting to sql.call line in the DatabaseService class.
Does anyone have an idea what I am doing wrong? I am certainly new to testing with SPOCK, but cannot seem to find anywhere online where people are mocking SQL methods.
最满意答案
交互应该在when子句中,并且使用>>此外,'Sql'是一个类,并且在您的服务中创建:您不能轻易地模拟它。 使用像hsqldb这样的内存数据库来测试这种事情更简单,但你可以模拟连接和预备声明(这很难看)
void "test registerDeveloper exception handling"() { given: String expectedErrorMsg = "exception from test" DataSource mockedSource = Mock(DataSource) def user = new User(name: "joe") def databaseService = new DatabaseService(dataSource:mockedSource) when: databaseService.registerUser(user) then: 1* mockedSource.getConnection() >> {throw new SQLException(expectedErrorMsg)} and: def exception = thrown(SQLException) exception.message == expectedErrorMsg }Interaction should be in a when clause, and use >> Moreover 'Sql' is a class, and is created in your service : you can't mock it really easily. it's more simple to test this kind of things with an in-memory db like hsqldb, but you can mock the connection and preparedstatement (it's ugly)
void "test registerDeveloper exception handling"() { given: String expectedErrorMsg = "exception from test" DataSource mockedSource = Mock(DataSource) def user = new User(name: "joe") def databaseService = new DatabaseService(dataSource:mockedSource) when: databaseService.registerUser(user) then: 1* mockedSource.getConnection() >> {throw new SQLException(expectedErrorMsg)} and: def exception = thrown(SQLException) exception.message == expectedErrorMsg }更多推荐
发布评论