gpt4 book ai didi

Grails 集成测试不能在空服务对象上调用方法?

转载 作者:行者123 更新时间:2023-12-02 15:32:35 25 4
gpt4 key购买 nike

简单的服务类 AnalyzerService 调用存储在数据库中的过程。
尝试运行集成测试以确保服务调用存储的过程并在分析器类对其进行操作后返回正确的数据。但是,得到“无法在空对象上调用方法 calculateEstimateNumberOfPositions()”的可怕异常。为什么服务对象为空?我错过了什么?

谢谢你!

package foobar.analyze
import static org.junit.Assert.*
import org.junit.*
import foobar.analyze.AnalyzerService
//@TestFor(AnalyzerService)
class AnalyzerServiceTests {
def AnalyzerService service
def dataSource

@Before
void setUp() { }

@After
void tearDown() { }

@Test
void testcalculateEstimateNumberOfPositions() {
String positionName = "crew"
String city = "Great Neck"
String state = "NY"
int numberOfPositionsSought = 100
int expectedNumberOfPositionsEstimate = 100
def numberOfPositionsEstimate = service.calculateEstimateNumberOfPositions(positionName, city, state, numberOfPositionsSought)
fail (numberOfPositionsEstimate != expectedNumberOfPositionsEstimate)
}
}

最佳答案

习俗。遵守约定。任何不符合惯例的命名法都会在依赖注入(inject)期间产生问题。

约定是使用服务类名作为analyzerService而不是 service在集成测试中。

集成测试应该看起来像

class AnalyzerServiceTests extends GroovyTestCase {
//Service class injected only if you
//use the naming convention as below for AnalyzerService
def analyzerService
def dataSource
......
......
}

可以使用 service在使用测试 mixin 时的单元测试用例中
@TestFor(AnalyzerService)
通过在单元测试用例中使用上述内容,您可以使用默认的 service测试用例中的变量。这在集成测试的情况下是不一样的。

关于Grails 集成测试不能在空服务对象上调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17433828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com