我在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
任何人都可以说出错了吗?
答案 0 :(得分:2)
这可能与您使用@Mock注释进行集成测试这一事实有关。 @Mock仅用于单元测试。
在文档中:
http://grails.github.io/grails-doc/latest/guide/testing.html
您将看到单元测试部分中描述了测试混合。