From 248f76132da5b129dadb00bc1d00461f6500318e Mon Sep 17 00:00:00 2001 From: Andreas Gebhardt Date: Thu, 6 Jul 2023 19:51:04 +0200 Subject: [PATCH] use `@TestConstructor` annotation > @TestConstructor is a type-level annotation that is used to configure how the > parameters of a test class constructor are autowired from components in the > test's ApplicationContext. [1]: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/TestConstructor.html --- .../AtLeastOnceInboxXmlMessageProcessingTest.kt | 10 ++++++---- ...grationInboundChannelEventualConsistencyTest.kt | 10 ++++++---- ...dJmsListenerContainerEventualConsistencyTest.kt | 14 ++++++++------ .../snippets/SpringIntegrationJdbcBatchIntTest.kt | 8 +++++--- .../snippets/SpringIntegrationJdbcIntTest.kt | 8 +++++--- .../snippets/SpringIntegrationJdbcXmlIntTest.kt | 8 +++++--- .../SpringIntegrationLeaderElectionTest.kt | 10 ++++++---- .../JdbcInboxXmlMessageRepositoryTest.kt | 8 +++++--- 8 files changed, 46 insertions(+), 30 deletions(-) diff --git a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/AtLeastOnceInboxXmlMessageProcessingTest.kt b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/AtLeastOnceInboxXmlMessageProcessingTest.kt index b89fa43..458120d 100644 --- a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/AtLeastOnceInboxXmlMessageProcessingTest.kt +++ b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/AtLeastOnceInboxXmlMessageProcessingTest.kt @@ -16,7 +16,6 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource import org.postgresql.jdbc.PgSQLXML -import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace.NONE import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest @@ -25,6 +24,8 @@ import org.springframework.jdbc.core.namedparam.MapSqlParameterSource import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate import org.springframework.jdbc.support.GeneratedKeyHolder import org.springframework.jdbc.support.xml.Jdbc4SqlXmlHandler +import org.springframework.test.context.TestConstructor +import org.springframework.test.context.TestConstructor.AutowireMode.ALL import org.springframework.test.context.jdbc.Sql import org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD import org.springframework.transaction.annotation.Propagation.NEVER @@ -51,10 +52,11 @@ import kotlin.time.Duration.Companion.seconds ModifiableFixedUTCClock::class ) @JdbcTest(properties = ["spring.datasource.url=jdbc:tc:postgresql:15.2:///"]) +@TestConstructor(autowireMode = ALL) class AtLeastOnceInboxXmlMessageProcessingTest( - @Autowired private val clock: ModifiableFixedUTCClock, - @Autowired private val jdbcTemplate: NamedParameterJdbcTemplate, - @Autowired private val repository: JdbcInboxXmlMessageRepository + private val clock: ModifiableFixedUTCClock, + private val jdbcTemplate: NamedParameterJdbcTemplate, + private val repository: JdbcInboxXmlMessageRepository ) { @ParameterizedTest @ValueSource(ints = [1, 5, 10]) diff --git a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationInboundChannelEventualConsistencyTest.kt b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationInboundChannelEventualConsistencyTest.kt index f5edc3e..d7c7776 100644 --- a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationInboundChannelEventualConsistencyTest.kt +++ b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationInboundChannelEventualConsistencyTest.kt @@ -6,7 +6,6 @@ import io.github.agebhar1.snippets.repository.JdbcInboxXmlMessageRepository import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows -import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest import org.springframework.boot.test.context.TestConfiguration import org.springframework.context.annotation.Bean @@ -22,6 +21,8 @@ import org.springframework.messaging.MessageHeaders import org.springframework.messaging.handler.annotation.Payload import org.springframework.stereotype.Service import org.springframework.test.annotation.DirtiesContext +import org.springframework.test.context.TestConstructor +import org.springframework.test.context.TestConstructor.AutowireMode.ALL import org.springframework.test.context.jdbc.Sql import org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD import org.springframework.transaction.annotation.Propagation.MANDATORY @@ -63,11 +64,12 @@ class JustAService( @Sql( executionPhase = AFTER_TEST_METHOD, statements = ["DELETE FROM INBOX_XML_MESSAGE WHERE id = '4bafe8fd-2086-4abb-a79f-47bbaa0aa4c9'"]) +@TestConstructor(autowireMode = ALL) @Transactional(propagation = NEVER) class SpringIntegrationInboundChannelEventualConsistencyTest( - @Autowired val txTemplate: TransactionTemplate, - @Autowired val jdbcTemplate: JdbcTemplate, - @Autowired private val messagingTemplate: MessagingTemplate + val txTemplate: TransactionTemplate, + val jdbcTemplate: JdbcTemplate, + private val messagingTemplate: MessagingTemplate ) { @Test fun `should save message from channel to inbox database table and proceed normally`() { diff --git a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationInboundJmsListenerContainerEventualConsistencyTest.kt b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationInboundJmsListenerContainerEventualConsistencyTest.kt index 39a5c29..9bac80b 100644 --- a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationInboundJmsListenerContainerEventualConsistencyTest.kt +++ b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationInboundJmsListenerContainerEventualConsistencyTest.kt @@ -18,7 +18,6 @@ import org.aspectj.lang.annotation.Pointcut import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test -import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.boot.test.context.TestConfiguration import org.springframework.context.annotation.Bean @@ -34,6 +33,8 @@ import org.springframework.stereotype.Component import org.springframework.stereotype.Service import org.springframework.test.annotation.DirtiesContext import org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD +import org.springframework.test.context.TestConstructor +import org.springframework.test.context.TestConstructor.AutowireMode.ALL import org.springframework.test.context.jdbc.Sql import org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD import org.springframework.transaction.annotation.Propagation.NEVER @@ -119,13 +120,14 @@ class CustomJmsConsumer( executionPhase = AFTER_TEST_METHOD, statements = ["DELETE FROM INBOX_XML_MESSAGE WHERE id = '4bafe8fd-2086-4abb-a79f-47bbaa0aa4c9'"]) +@TestConstructor(autowireMode = ALL) @Transactional(propagation = NEVER) class SpringIntegrationInboundJmsEventualConsistencyTest( - @Autowired private val consumer: CustomJmsConsumer, - @Autowired private val repositoryInterceptor: InboxXmlMessageRepositoryInterceptor, - @Autowired private val jdbcTemplate: JdbcTemplate, - @Autowired private val jmsTemplate: JmsTemplate, - @Autowired private val txTemplateInterceptor: TransactionTemplateInterceptor + private val consumer: CustomJmsConsumer, + private val repositoryInterceptor: InboxXmlMessageRepositoryInterceptor, + private val jdbcTemplate: JdbcTemplate, + private val jmsTemplate: JmsTemplate, + private val txTemplateInterceptor: TransactionTemplateInterceptor ) { @BeforeEach fun beforeEach() { diff --git a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationJdbcBatchIntTest.kt b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationJdbcBatchIntTest.kt index 0cd2a1d..b48bbaf 100644 --- a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationJdbcBatchIntTest.kt +++ b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationJdbcBatchIntTest.kt @@ -2,7 +2,6 @@ package io.github.agebhar1.snippets import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest import org.springframework.boot.test.context.TestConfiguration import org.springframework.context.annotation.Bean @@ -13,14 +12,17 @@ import org.springframework.integration.dsl.integrationFlow import org.springframework.integration.jdbc.JdbcMessageHandler import org.springframework.integration.support.MessageBuilder import org.springframework.jdbc.core.JdbcTemplate +import org.springframework.test.context.TestConstructor +import org.springframework.test.context.TestConstructor.AutowireMode.ALL import org.springframework.test.context.jdbc.Sql import org.springframework.test.jdbc.JdbcTestUtils.countRowsInTableWhere import java.util.UUID.randomUUID @JdbcTest +@TestConstructor(autowireMode = ALL) class SpringIntegrationJdbcBatchIntTest( - @Autowired val jdbcTemplate: JdbcTemplate, - @Autowired val messagingTemplate: MessagingTemplate + val jdbcTemplate: JdbcTemplate, + val messagingTemplate: MessagingTemplate ) { @Test @Sql("/sql/spring-integration-jdbc-int-test.sql") diff --git a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationJdbcIntTest.kt b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationJdbcIntTest.kt index ae793dd..7463806 100644 --- a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationJdbcIntTest.kt +++ b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationJdbcIntTest.kt @@ -2,7 +2,6 @@ package io.github.agebhar1.snippets import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest import org.springframework.boot.test.context.TestConfiguration import org.springframework.context.annotation.Bean @@ -13,13 +12,16 @@ import org.springframework.integration.dsl.integrationFlow import org.springframework.integration.jdbc.JdbcMessageHandler import org.springframework.integration.support.MessageBuilder import org.springframework.jdbc.core.JdbcTemplate +import org.springframework.test.context.TestConstructor +import org.springframework.test.context.TestConstructor.AutowireMode.ALL import org.springframework.test.context.jdbc.Sql import org.springframework.test.jdbc.JdbcTestUtils.countRowsInTableWhere @JdbcTest +@TestConstructor(autowireMode = ALL) class SpringIntegrationJdbcIntTest( - @Autowired val jdbcTemplate: JdbcTemplate, - @Autowired val messagingTemplate: MessagingTemplate + val jdbcTemplate: JdbcTemplate, + val messagingTemplate: MessagingTemplate ) { @Test @Sql("/sql/spring-integration-jdbc-int-test.sql") diff --git a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationJdbcXmlIntTest.kt b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationJdbcXmlIntTest.kt index 2df0199..9ffbf7f 100644 --- a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationJdbcXmlIntTest.kt +++ b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationJdbcXmlIntTest.kt @@ -2,7 +2,6 @@ package io.github.agebhar1.snippets import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest import org.springframework.boot.test.context.TestConfiguration import org.springframework.context.annotation.Bean @@ -16,6 +15,8 @@ import org.springframework.integration.support.MessageBuilder import org.springframework.jdbc.core.JdbcTemplate import org.springframework.jdbc.support.xml.Jdbc4SqlXmlHandler import org.springframework.jdbc.support.xml.SqlXmlHandler +import org.springframework.test.context.TestConstructor +import org.springframework.test.context.TestConstructor.AutowireMode.ALL import org.w3c.dom.Document import java.time.Instant import java.time.LocalDateTime @@ -24,9 +25,10 @@ import java.util.UUID @Import(Jdbc4SqlXmlHandler::class) @JdbcTest +@TestConstructor(autowireMode = ALL) class SpringIntegrationJdbcXmlIntTest( - @Autowired val jdbcTemplate: JdbcTemplate, - @Autowired val messagingTemplate: MessagingTemplate + val jdbcTemplate: JdbcTemplate, + val messagingTemplate: MessagingTemplate ) { @Test fun `message with XML payload should be present in database table`() { diff --git a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationLeaderElectionTest.kt b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationLeaderElectionTest.kt index 571ef63..0824cca 100644 --- a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationLeaderElectionTest.kt +++ b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/SpringIntegrationLeaderElectionTest.kt @@ -5,7 +5,6 @@ package io.github.agebhar1.snippets import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Test -import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest import org.springframework.boot.test.context.TestConfiguration import org.springframework.context.annotation.Bean @@ -20,16 +19,19 @@ import org.springframework.integration.leader.event.OnGrantedEvent import org.springframework.integration.leader.event.OnRevokedEvent import org.springframework.integration.support.leader.LockRegistryLeaderInitiator import org.springframework.integration.support.locks.LockRegistry +import org.springframework.test.context.TestConstructor +import org.springframework.test.context.TestConstructor.AutowireMode.ALL import java.util.UUID import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit.SECONDS import javax.sql.DataSource @JdbcTest +@TestConstructor(autowireMode = ALL) class SpringIntegrationLeaderElectionTest( - @Autowired val leaderInitiator: LockRegistryLeaderInitiator, - @Autowired val lockRegistry: LockRegistry, - @Autowired val singleResource: SingleResource + val leaderInitiator: LockRegistryLeaderInitiator, + val lockRegistry: LockRegistry, + val singleResource: SingleResource ) { /* http://presos.dsyer.com/decks/locks-and-leaders.html diff --git a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/repository/JdbcInboxXmlMessageRepositoryTest.kt b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/repository/JdbcInboxXmlMessageRepositoryTest.kt index c1c4658..e9e3eea 100644 --- a/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/repository/JdbcInboxXmlMessageRepositoryTest.kt +++ b/spring-boot/src/test/kotlin/io/github/agebhar1/snippets/repository/JdbcInboxXmlMessageRepositoryTest.kt @@ -4,19 +4,21 @@ import io.github.agebhar1.snippets.domain.InboxXmlMessage import io.github.agebhar1.snippets.toDocument import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest import org.springframework.context.annotation.Import import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate import org.springframework.jdbc.support.xml.Jdbc4SqlXmlHandler +import org.springframework.test.context.TestConstructor +import org.springframework.test.context.TestConstructor.AutowireMode.ALL import java.time.Instant import java.util.UUID @Import(JdbcInboxXmlMessageRepository::class, Jdbc4SqlXmlHandler::class) @JdbcTest +@TestConstructor(autowireMode = ALL) class JdbcInboxXmlMessageRepositoryTest( - @Autowired private val jdbcTemplate: NamedParameterJdbcTemplate, - @Autowired private val repository: JdbcInboxXmlMessageRepository + private val jdbcTemplate: NamedParameterJdbcTemplate, + private val repository: JdbcInboxXmlMessageRepository ) { @Test fun `should save entity`() {