2016-06-18 9 views
1

Ich habe ein Projekt mit 2 Modulen, A und B (Business-Logik und Rest-Dienste) und B ist abhängig von A.Wie installiere ich einen Integrationstest mit Spring und Spock?

Starten der SpringBoot-Anwendung funktioniert, aber ich kann den Integrationstest nicht richtig einrichten.

Ich habe eine abstrakte Klasse, die Spock Spezifikation

@ContextConfiguration(loader = SpringApplicationContextLoader.class, 
        classes = [WebAppConfig.class, AppConfig.class]) 
@WebIntegrationTest 
@Stepwise 
abstract class RestIntegrationBaseSpec extends Specification { 

    RestTemplate restTemplate = new TestRestTemplate() 

    String getBasePath() { "" } 

    URI serviceURI(String path = "") { 
     new URI("http://localhost:8080/${basePath}${path}") 
    } 
} 

und erweitere ich es für meine Prüfung erstreckt, versucht, einen Dienst zu injizieren, die A

class TransactionsControllerSpec extends RestIntegrationBaseSpec{ 

@Override 
String getBasePath() { 
    //return "api/" 
    return "" 
} 

@Autowired 
ImportService importService 

def setupSpec() { 
    initDB() 
} 

private void initDB() { 
    importService.importData(); 
} 

def "Test"() { 
    given: 
     ... 
    when: 
     ... 
    then: 
     ... 
} 

in Modul implementiert ist, aber ich Nullpointer Bei importService.importData() ist importService gleich null.

In meiner gradle Datei in B-Projekt habe ich diese Abhängigkeiten:

compile('org.springframework.boot:spring-boot-starter-aop') 
compile('org.springframework.boot:spring-boot-devtools') 
compile('org.springframework.boot:spring-boot-starter-remote-shell') 
compile('org.springframework.boot:spring-boot-starter-security') 
compile('org.springframework.boot:spring-boot-starter-web') 
compile('org.projectlombok:lombok:1.16.6') 
compile project(':business') 

// http://mvnrepository.com/artifact/commons-fileupload/commons-fileupload 
compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.2' 

// http://mvnrepository.com/artifact/commons-io/commons-io 
compile group: 'commons-io', name: 'commons-io', version: '2.5' 

testCompile('org.springframework.boot:spring-boot-starter-test') 

testCompile('org.spockframework:spock-core:1.0-groovy-2.4') 
testCompile('org.spockframework:spock-spring:1.0-groovy-2.4') 

Was bin ich?

+0

Meintest du @Autowired? –

+0

@MattBusche ja, Tippfehler, danke – gdantimi

Antwort

0

Nicht sicher, ob das das Problem löst, aber versuchen, @WebAppConfiguration in dem Test hinzuzufügen. Dadurch wird sichergestellt, dass der WebApplicationContext für den Test geladen wird.