Skip to content

Commit

Permalink
use @TestConstructor annotation
Browse files Browse the repository at this point in the history
> @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
  • Loading branch information
agebhar1 committed Jul 6, 2023
1 parent 298d505 commit 248f761
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`() {
Expand Down

0 comments on commit 248f761

Please sign in to comment.