Larry Garfield
07/03/2024, 4:38 PMLarry Garfield
07/03/2024, 4:41 PMLarry Garfield
07/03/2024, 4:42 PMLarry Garfield
07/03/2024, 4:47 PM@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ScraperErrorConsumerTest {
@Autowired
lateinit var objectMapper: ObjectMapper
@Autowired
lateinit var subject: ScraperErrorConsumer
@MockBean
lateinit var repository: ScraperRefreshRepository
@Test
fun `error calls are saved`() {
val uuid = UUID.randomUUID()
val scrape = ScraperRefresh(
stateCode = "IL",
entityName = "Me, Inc",
)
scrape.id = uuid
setupRepositoryMock(mutableMapOf(scrape.id to scrape))
repository.save(scrape)
val message = ScraperErrorMessage(
id = uuid,
errorType = "Bad",
title = "Bad",
request = EntityRefreshEvent(uuid),
)
val ack = mockAcknowledgement()
// The actual thing being tested. subject.consume(objectMapper.writeValueAsString(message), ack)
val record = repository.findById(uuid)
// Assertions here
}
private fun mockAcknowledgement() = object : Acknowledgment {
var called = false
override fun acknowledge() {
called = true
}
}
private fun setupRepositoryMock(records: MutableMap<UUID, ScraperRefresh>) {
repository = mockk<ScraperRefreshRepository>()
every { repository.save(any<ScraperRefresh>()) } answers {
val updated = it.invocation.args[0] as ScraperRefresh
records[updated.id] = updated
updated
}
every { repository.findById(any<UUID>()) } answers {
val uuid = it.invocation.args[0] as UUID
Optional.ofNullable(records[uuid])
}
}
}
The problem is, that doesn’t propagate the mock through to the underlying service that is held by subject
. So it saves to I’m not sure where (since there shouldn’t be a DB table?), but then the mocked findById() looks at just the mutable map and doesn’t find the updates.
If I don’t re-set the repository
mock in the first line of setupRepositoryMock(), I get an error
Failed matching mocking signature for
left matchers: [any()]So… I’m confused here. All I can think of is to abandon and just have a pure unit test, but that doesn’t actually buy me anything. I already have tests for the service class so I know it’s covered. It’s specifically the limited logic in the consumer and the wiring in Spring that I want to test, but I cannot for the life of me see how.
Larry Garfield
07/03/2024, 4:47 PMEndre Deak
07/03/2024, 4:52 PMLarry Garfield
07/03/2024, 4:54 PMLarry Garfield
07/03/2024, 4:54 PMEndre Deak
07/03/2024, 5:00 PMLarry Garfield
07/03/2024, 5:00 PMLarry Garfield
07/03/2024, 5:01 PMEndre Deak
07/03/2024, 5:13 PMLarry Garfield
07/03/2024, 5:14 PMEndre Deak
07/03/2024, 5:15 PMLarry Garfield
07/03/2024, 5:24 PMLarry Garfield
07/03/2024, 5:53 PMJacob
07/03/2024, 10:46 PMLarry Garfield
07/08/2024, 1:54 PM