2016-07-23 41 views
3

Ich versuche, einige Tests für meine Spring Boot 1.4.0 mit Spock schreiben und meine Anwendung-Test-Eigenschaften-Datei wird nicht abgeholt.Spring Boot 1.4, Spock und application.properties

Ich habe dies in meinem gradle:

dependencies { 

    compile('org.springframework.boot:spring-boot-starter-data-jpa') 
    compile('org.springframework.boot:spring-boot-starter-security') 
    compile('org.springframework.boot:spring-boot-starter-web') 
    compile 'org.codehaus.groovy:groovy-all:2.4.1'  
    testCompile('org.springframework.boot:spring-boot-starter-test') 
    testCompile('org.spockframework:spock-spring:1.0-groovy-2.4') { 
} 

Dann habe ich dies in

/src/test/groovy/resources:

# JWT Key 
[email protected] 

Und schließlich meine Spock-Test:

@SpringBootTest(classes = MyApplication.class, webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT) 
@TestPropertySource("application-test.properties") 
public class TokenUtilityTest extends Specification { 

    @Autowired 
    private TokenUtility tokenUtility 

    def "test a valid token creation"() { 
     def userDetails = new User(username: "test", password: "password", accountNonExpired: true, accountNonLocked: true, 
     ); 

     when: 
     def token = tokenUtility.buildToken(userDetails) 

     then: 
     token != null 
    } 
} 

Welche dieser Klasse testet:

@Component 
public class TokenUtility { 

    private static final Logger LOG = LoggerFactory.getLogger(TokenUtility.class); 

    @Value("${jwt.key}") 
    private String jwtKey; 

    public String buildToken(UserDetails user) { 
     return Jwts.builder() 
         .setSubject(user.getUsername()) 
         .signWith(SignatureAlgorithm.HS512, jwtKey) 
         .compact(); 
    } 

    public boolean validate(String token) { 
     try { 

      Jwts.parser().setSigningKey(jwtKey).parseClaimsJws(token); 
      return true; 

     } catch (SignatureException e) { 
      LOG.error("Invalid JWT found: " + token); 
     } 
     return false; 
    } 
} 

ich instanziiert ursprünglich die TokenUtility auf meinem Test aber die application-test.properties wurde nie geladen (Ich gehe davon aus, da jwtKey null war). Also versuche ich @Autowired meine Klasse im Test, aber das ist jetzt null.

Es sieht so aus, als ob Spring Boot 1.4 viel zum Testen geändert hat, also werde ich das vielleicht nicht richtig verdrahten?

Antwort

6

Es gibt verschiedene Fehler in Ihrem Testcode; Erstens, Ihre Abhängigkeiten sind schlecht - Spock 1.0 unterstützt keine Annotation, so dass kein Kontext initialisiert wird und keine Nachbearbeitung durchgeführt wird, daher die Nullzeiger-Ausnahme: Nichts wird autowired.

Unterstützung für diese Anmerkung wurde in Spock 1.1, hinzugefügt, die noch release Kandidaten, so Sie verwenden müssen werden:

dependencies { 
    compile('org.springframework.boot:spring-boot-starter-data-jpa') 
    compile('org.springframework.boot:spring-boot-starter-security') 
    compile('org.springframework.boot:spring-boot-starter-web') 
    compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.6.0' 

    compile('org.codehaus.groovy:groovy') 

    testCompile('org.springframework.boot:spring-boot-starter-test') 
    testCompile('org.spockframework:spock-core:1.1-groovy-2.4-rc-1') 
    testCompile('org.spockframework:spock-spring:1.1-groovy-2.4-rc-1') 
    testCompile group: 'com.h2database', name: 'h2', version: '1.4.192' 
} 

Dann Ihr Weg zu den application-test.properties ist falsch und sollte /application-test.properties sein, da es sich im Stammverzeichnis des Klassenpfads befindet:

@SpringBootTest(classes = DemoApplication.class, 
       webEnvironment = WebEnvironment.RANDOM_PORT) 
@TestPropertySource("/application-test.properties") 
public class TokenUtilityTest extends Specification { 

    @Autowired 
    TokenUtility tokenUtility 

    def "test a valid token creation"() { 
     def userDetails = new User("test", "password", Collections.emptyList()); 

     when: 
     def token = tokenUtility.buildToken(userDetails) 

     then: 
     token != null 
    } 
}