如何在Groovy / Grails Spoc中模拟SQL调用(How to mock an SQL call in Groovy/Grails Spoc)

编程入门 行业动态 更新时间:2024-10-28 15:27:33
如何在Groovy / Grails Spoc中模拟SQL调用(How to mock an SQL call in Groovy/Grails Spoc)

我有一个用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 }

更多推荐

本文发布于:2023-08-03 00:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382533.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Grails   Groovy   Spoc   mock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!