gpt4 book ai didi

grails - java.lang.IllegalStateException : Could not find ApplicationContext, 首先正确配置 Grails

转载 作者:行者123 更新时间:2023-12-02 09:20:47 28 4
gpt4 key购买 nike

我在 grails 2.4.3 中有以下 spock 集成测试,我在 google 上搜索过,但没有找到任何可以理解的内容

@Mock([Product,Price])
class ProductViewerSpec extends IntegrationSpec {

ProductController productController = new ProductController()

void "Test the complete flow of retrieving and viewing a product"() {

when: "The loadProducts method is executed to get list of products"
String barCode = "1"
String description = "testProduct"
Set<Price> prices = new HashSet<Price>()
Product product = new Product(barCode, description, prices)

Price price1 = new Price(10 as BigDecimal).save(flush: true, failOnError: true)
product.prices.add(price1)
Price price2 = new Price(12 as BigDecimal).save(flush: true, failOnError: true)
product.prices.add(price2)
Price price3 = new Price(14 as BigDecimal).save(flush: true, failOnError: true)
product.prices.add(price3)
Price price4 = new Price(11 as BigDecimal).save(flush: true, failOnError: true)
product.prices.add(price4)
Price price5 = new Price(12 as BigDecimal).save(flush: true, failOnError: true)
product.prices.add(price5)

product.save(flush: true, failOnError: true)

productController.productService.setPrices(product)

List<Product> productList = productController.productService.loadProducts(product.getBarCode(), null)

then: "All prices calculated and returned correctly"
Product.count() == 1
productList.size() == 1
productList.get(0).barCode == barCode
productList.get(0).description == description
productList.get(0).prices.size() == 5
productList.get(0).getAveragePrice() == 11.8 as BigDecimal
productList.get(0).getLowestPrice() == 10 as BigDecimal
productList.get(0).getHighestPrice() == 14 as BigDecimal
productList.get(0).getIdealPrice() == 14.4 as BigDecimal
productList.get(0).prices.id.contains(price1.id)
productList.get(0).prices.id.contains(price2.id)
productList.get(0).prices.id.contains(price3.id)
productList.get(0).prices.id.contains(price4.id)
productList.get(0).prices.id.contains(price5.id)
}
}

当我从命令行运行 grails test-app 时,出现以下错误。

| Compiling 1 source files
| Compiling 1 source files.
| Running 5 integration tests...
| Running 5 integration tests... 1 of 5
| Failure: com.prizypricer.core.ProductViewerSpec
| java.lang.IllegalStateException: Could not find ApplicationContext, configure Grails correctly first
at grails.util.Holders.getApplicationContext(Holders.java:97)
at grails.test.spock.IntegrationSpec.$spock_initializeSharedFields(IntegrationSpec.groovy:41)
| Failure: com.prizypricer.core.ProductLoaderSpec
| java.lang.IllegalStateException: Could not find ApplicationContext, configure Grails correctly first
at grails.util.Holders.getApplicationContext(Holders.java:97)
at grails.test.spock.IntegrationSpec.$spock_initializeSharedFields(IntegrationSpec.groovy:41)
| Completed 1 integration test, 0 failed in 0m 0s

谁能告诉我出了什么问题吗?

最佳答案

这可能与您在集成测试中使用 @Mock 注释有关。 @Mock 仅用于单元测试。

在文档中:

http://grails.github.io/grails-doc/latest/guide/testing.html

您将看到单元测试部分中描述了测试混合。

关于grails - java.lang.IllegalStateException : Could not find ApplicationContext, 首先正确配置 Grails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36851775/

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