Skip to content

Commit

Permalink
Add test to make sure that the fallback bean exists in the context
Browse files Browse the repository at this point in the history
  • Loading branch information
guillermocalvo committed Aug 4, 2023
1 parent 22e57dd commit 4cef524
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import io.micronaut.configuration.kafka.annotation.KafkaClient;
import io.micronaut.configuration.kafka.annotation.Topic;
import io.micronaut.context.annotation.Requires;

@Requires(property = "spec.name", value = "MessageClientFallbackSpec")
@KafkaClient
public interface MessageClient {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.inject.Singleton;
// end::imports[]

@Requires(property = "spec.name", value = "MessageClientFallbackSpec")
// tag::clazz[]
@Requires(property = "kafka.enabled", notEquals = StringUtils.TRUE, defaultValue = StringUtils.TRUE) // <1>
@Replaces(MessageClient.class) // <2>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package docs.producer.fallback

import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Property
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@MicronautTest
@Property(name = "spec.name", value = "MessageClientFallbackSpec")
@Property(name = "kafka.enabled", value = "false")
class MessageClientFallbackSpec extends Specification {

@Inject
ApplicationContext context

void "test that context contains the fallback bean"() {
when:
MessageClientFallback bean = context.getBean(MessageClientFallback)

then:
bean != null

when:
bean.send('message')

then:
thrown(UnsupportedOperationException)
}
}

0 comments on commit 4cef524

Please sign in to comment.