3

I have the following spock integration test for in grails 2.4.3, I have searched on google but didn't find anything understandable

@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)
}
}

I am getting following error when I run grails test-app from command line.

| 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

Can anyone tell what is wrong?

Aamir Ali
  • 145
  • 2
  • 16
  • You're going to want to add the Price/Product classes. For instance, is Price a domain? If so.. why aren't you putting it in the Mock statement? What version of Grails is this (I note you're not using the @Integration annotation, but IntegrationSpec instead)? Why does this need to be an integration test, rather than a unit test? – billjamesdev Apr 26 '16 at 00:32
  • Yes Price is a domain, I have tried adding it in mock as well but it didn't work. I am using grails 2.4.3. I am using IntegrationSpec because I found examples of this only. I have updated question. – Aamir Ali Apr 26 '16 at 06:31

1 Answers1

2

This is probably linked to the fact that you are using the @Mock annotation with an integration test. @Mock is only to be used in unit tests.

In the documentation:

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

You will see that the test mixins are described in the Unit testing section.

Luis Muñiz
  • 4,649
  • 1
  • 27
  • 43
  • I have removed @Mock annotation but now I am getting NullPointerException, how do I mock domain class in test now? actually I am doing Product.createCriteria().list(params) in my service method that is called from the test. – Aamir Ali May 04 '16 at 06:20
  • This actually solved my issue, I removed `@Mock` annotation from the integration tests and it worked !! Thanks @loteq – Aamir Ali May 23 '16 at 07:49