emazzotta:
私は春のアプリケーションにMicronautからリモーティング呼び出しを実行する必要があります。必要に応じてBeanを作成するために、私は工場を作成しました:
@Factory
public class RemotingConfig {
@Bean
@Singleton
public OfferLeadService offerLeadService(@Value("${offer.server.remoting.base.url}")
String offerRemotingBaseUrl) {
HttpInvokerProxyFactoryBean invoker = new HttpInvokerProxyFactoryBean();
invoker.setHttpInvokerRequestExecutor(new SimpleHttpInvokerRequestExecutor());
invoker.setServiceUrl(offerRemotingBaseUrl + OfferLeadService.URI);
invoker.setServiceInterface(OfferLeadService.class);
invoker.afterPropertiesSet();
return (OfferLeadService) invoker.getObject();
}
@Bean
@Singleton
public APIKeyService apiKeyService(@Value("${offer.server.remoting.base.url}")
String offerRemotingBaseUrl) {
HttpInvokerProxyFactoryBean invoker = new HttpInvokerProxyFactoryBean();
invoker.setHttpInvokerRequestExecutor(new SimpleHttpInvokerRequestExecutor());
invoker.setServiceUrl(offerRemotingBaseUrl + APIKeyService.URI);
invoker.setServiceInterface(APIKeyService.class);
invoker.afterPropertiesSet();
return (APIKeyService) invoker.getObject();
}
}
私のスポックの統合テストでは、私はMicronautのドキュメントによると試みたこれらの豆を、モックする必要があります。https://docs.micronaut.io/latest/guide/index.html#replaces
これは、このようなテストの結果:
@MicronautTest
class StackoverflowSpecification extends Specification {
@Inject
AuthorizedClient authorizedClient
@Inject
UnauthorizedClient unauthorizedClient
@Inject
OfferLeadService offerLeadService
@Inject
APIKeyService apiKeyService
@Factory
@Replaces(factory = RemotingConfig.class)
static class RemotingConfigTest extends Specification {
@Singleton
OfferLeadService offerLeadService() {
return Mock(OfferLeadService)
}
@Singleton
APIKeyService apiKeyService() {
return Mock(APIKeyService)
}
}
void "authenticated sessions request returns 200 ok"() {
when:
HttpResponse response = authorizedClient.getSession("AA-BB-CC")
then:
response.status == OK
and: 'setup mock calls'
1 * apiKeyService.find(_, _) >> buildApiKeyVO()
1 * offerLeadService.containsHipHavingPostalCode(_, _) >> true
0 * _
}
void "authenticated sessions request with wrong passphrase returns 403 forbidden"() {
when:
unauthorizedClient.getSessionWithWrongPassphrase("AA-BB-CC")
then:
HttpClientResponseException ex = thrown(HttpClientResponseException)
then:
ex.status == FORBIDDEN
and: 'setup mock calls'
1 * apiKeyService.find(_, _) >> buildApiKeyVO()
1 * offerLeadService.containsHipHavingPostalCode(_, _) >> false
0 * _
}
private static APIKeyVO buildApiKeyVO() {
APIKeyVO key = new APIKeyVO()
key.setId(1L)
key.setValue("123")
key.setEnabled(true)
key.setRoles(List.of("ROLE_STANDARD"))
key.setValidUntil(Instant.now().plus(100, ChronoUnit.DAYS))
key.setDescription("CBC App")
key.setAccountId("CBC")
return key
}
}
このソリューションは、うまく機能していません。彼らは単独で実行する場合は二つの試験に合格し、それらの両方を実行すると、しかし、(順序は第二の試験が上になるようにしたのであれば、それは渡すものであろう、ここでは関係あり)は、第2のテストが失敗します。
両方のテストを実行してデバッグする場合、私は最初のテストで予想通り2つのモックが呼び出された後、モックへのすべての後続の呼び出しがにつながることを確認null
し、false
それぞれ何か他のものを指定するにもかかわらず。
どのように私は2つの豆が経由して指定されたモック行くのですRemotingConfig
統合テストでは?
ジェームズKleeh:
あなたは使用していない@Replaces
、正しく注釈を。factory
部材は、単独で使用されることを意図されるのではなく、さらに置換されているタイプを修飾します。
@Factory
static class RemotingConfigTest extends Specification {
@Singleton
@Replaces(bean = OfferLeadService.class, factory = RemotingConfig.class)
OfferLeadService offerLeadService() {
return Mock(OfferLeadService)
}
@Singleton
@Replaces(bean = APIKeyService.class, factory = RemotingConfig.class)
APIKeyService apiKeyService() {
return Mock(APIKeyService)
}
}
編集:上記はまだしかし、あなたは、テスト実行の間にリセットされ、あなたのモックを期待している、適用されます。すなわち、上記のコードでは発生しません。あなたは使用する必要があります@MockBean
micronaut試験の一部である注釈を。
@MockBean(OfferLeadService.class)
OfferLeadService offerLeadService() {
return Mock(OfferLeadService)
}
@MockBean(APIKeyService.class)
APIKeyService apiKeyService() {
return Mock(APIKeyService)
}