私はSpringBoot1.4、Spockおよびspring-boot-starter-securityを使用しています。ここに私の関連のGradleファイルは次のとおりです。TestRestTemplateとApacheのためにSpringBoot統合が開始されないSSL
springBootVersion = '1.4.0.BUILD-SNAPSHOT'
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-devtools')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.jasypt:jasypt-spring31:1.9.2')
compile('org.apache.httpcomponents:httpclient:4.5.2')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.spockframework:spock-core:1.1-groovy-2.4-rc-2')
testCompile('org.spockframework:spock-spring:1.1-groovy-2.4-rc-2')
私も私のapplication-test.propertiesファイルにこれを追加しました:私はjasyptを使用して(JWTを作成し、認証されたエンドポイントを作成することができました
server.ssl.enabled=false
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
-spring31:1.9.2)、それをAuthorizationヘッダーに返します。これはブラウザやPostmanを使ってアクセスするときに効果的です。しかし、私はSpring Boot Integration Testでこの作業を行うことができませんでした。
私は自分のパスワードが暗号化されているので、DB Configurationクラスを持っている:
@Configuration
public class DBConfig {
@Autowired
private Environment env;
@Value("${spring.datasource.driverClassName}")
private String databaseDriverClassName;
@Value("${spring.datasource.url}")
private String datasourceUrl;
@Value("${spring.datasource.username}")
private String databaseUsername;
@Value("${jasypt.key}")
private String jasyptKey;
@Bean
public DataSource datasource() throws IOException {
return DataSourceBuilder
.create()
.username(databaseUsername)
.password(getSecurePassword())
.url(datasourceUrl)
.driverClassName(databaseDriverClassName)
.build();
}
private String getSecurePassword() throws IOException {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword(jasyptKey);
Properties props = new EncryptableProperties(encryptor);
props.load(this.getClass().getClassLoader().getResourceAsStream("application-" + env.getActiveProfiles()[0] + ".properties"));
return props.getProperty("spring.datasource.password");
}
}
私の基本テストクラスが埋め込まれたTomcatインスタンスを起動し、すべてのエンドポイントが利用できるようにし、JWTを取得する方法があります:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource("classpath:application-test.properties")
class BaseSpecification extends Specification {
@Autowired
def TestRestTemplate restTemplate
String loginAndGetJWT() {
def model = new ModelMap();
model.addAttribute("username", "ausername")
model.addAttribute("password", "apassword");
ResponseEntity<JwtAuthenticationResponse> response = restTemplate.getForEntity("/auth", String.class, model);
def jwt = response.getBody().token
return jwt;
}
}
私は私のサンプルテストを実行しようとすると:
class ApplicationSpecWithoutAnnotation extends BaseSpecification {
@Autowired
WebApplicationContext context
def "should boot up without errors"() {
expect: "web application context exists"
context != null
}
}
を私はトンを取得彼は次のよう
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testRestTemplate': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.apache.http.conn.ssl.SSLConnectionSocketFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
thisによると:@SpringBootTestが使用されるたびにTestRestTemplateはBeanとして利用できるようになりました。エラーの変化を私は( ':HTTPClientは4.5.2 org.apache.httpcomponents'):コンパイルを削除した場合
は
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testRestTemplate': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClients
だから春のウェブ&のapacheへの依存性があるように見えますそれは正しいとは思わない。
明示的な依存関係として 'httpclient'は必要ないと思います。あなたのエラーはクラスを初期化する際に問題を示しています(この[リンク](https://stackoverflow.com/questions/1457863/what-causes 'NoClassDefFoundError'の説明についてはnoclassdeffounderrorとclassnの違いがあります)。どのJDKバージョンを使用していますか?また、 'server.ssl.enabled = false'を' true'に設定するか、設定キーを省略することもできます。 –
私は実際にserver.ssl.enabled = falseを持ち、JDK1.8を使用しています。私がbuild.gradleから "compile( 'org.apache.httpcomponents:httpclient:4.5.2')という行を削除すると、私はまだエラーが出ます:原因:java.lang.NoClassDefFoundError:org/apache/http/impl /クライアント/ HttpClients。私がこれをベースにしている春の例がすべてmavenにあるので、これがgradleの問題かどうか疑問に思っていますか? – sonoerin
私はこれがGradleの問題ではないと思います。 Githubで問題を示す小さなサンプルプロジェクトを公開できますか? –