2

Ich habe die folgende spock Integrationstest für in Grails 2.4.3, habe ich auf Google gesucht, aber nichts verständlichjava.lang.IllegalStateException: Konnte nicht finden Application, konfigurieren Grails richtig ersten

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

Ich bekomme folgenden Fehler, wenn ich Grails Test-App von der Befehlszeile aus ausführen.

| 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 

Kann jemand sagen, was falsch ist?

+0

Sie werden die Preis-/Produktklassen hinzufügen wollen. Zum Beispiel, ist Price eine Domain? Wenn ja ... warum schreibst du es nicht in die Mock-Anweisung? Welche Version von Grails ist das? (Ich beachte, dass Sie nicht die @Integration Annotation, sondern stattdessen IntegrationSpec verwenden)? Warum muss dies ein Integrationstest sein und nicht ein Komponententest? – billjamesdev

+0

Ja Preis ist eine Domain, ich habe versucht, es auch in Mock hinzuzufügen, aber es hat nicht funktioniert. Ich benutze Grils 2.4.3. Ich verwende IntegrationSpec, weil ich nur Beispiele dafür gefunden habe. Ich habe die Frage aktualisiert. –

Antwort

1

Dies hängt wahrscheinlich damit zusammen, dass Sie die @Mock Annotation mit einem Integrationstest verwenden. @Mock darf nur in Komponententests verwendet werden.

In der Dokumentation:

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

Sie werden sehen, dass der Test Mixins in dem Unit-Tests Abschnitt beschrieben werden.

+0

Ich habe @Mock Annotation entfernt, aber jetzt bekomme ich NullPointerException, wie mache ich jetzt Domain-Klasse im Test? tatsächlich mache ich Product.createCriteria(). list (params) in meiner Service-Methode, die aus dem Test aufgerufen wird. –

+0

Dies löste mein Problem, ich entfernte '@ Mock' Annotation von den Integrationstests und es funktionierte !! Danke @loteq –

Verwandte Themen