I'm writing some unit tests for a rest controller using HttpClient but I'm gettint the error on the title in the exchange part, we are using DynamoDBEmbeddedClient to have a fake data base instead of using the real db for testing, before adding the DynamoDBEmbeddedClient the test was working, so maybe I'm missing something on the test or the controller to mak it work?
The enpoint is just a patch to save or update an entry, the app it actually works and everything, is just the test that it seems to be broken
application-test.yml:
micronaut:
server:
port: -1
application:
name: app
endpoints:
all:
port: 9999
dynamodb:
e-note-table-name: dummy-table-name
environment: test
Controller:
@Validated
@Controller("/api/v1/")
@ExecuteOn(TaskExecutors.IO)
class ENotesController(private val service: ENoteService) {
@Patch("/")
fun createNoteRecord(@Body eNote: ENote
): Mono<HttpResponse<ENote>> {
return service.save(eNote)
.map { HttpResponse.ok(it) }
}
}
Test:
@MicronautTest(startApplication = false)
@ExtendWith(DynamoDBExtension::class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ENoteControllerTest {
@Inject
@field:Client("/")
lateinit var client: HttpClient
@Test
fun `PATCH eNote - should create a new eNote`() {
val newENote = eNote()
val request = HttpRequest
.PATCH(
"/api/v1/",
newENote
)
val response = client
.toBlocking()
.exchange(request, ENote::class.java)
assertEquals(HttpStatus.OK, response.status)
assertEquals(newENote.noteKey, response.body()!!.noteKey)
assertEquals(newENote.noteEntries, response.body()!!.noteEntries)
}
private fun eNote() = ENote(
studentId = UUID.randomUUID().toString(),
enrollmentId = UUID.randomUUID().toString(),
courseId = UUID.randomUUID().toString(),
activityId = UUID.randomUUID().toString(),
noteEntries = listOf("Test the", "Patch method"),
audit = Audit(
createdBy = UUID.randomUUID().toString(),
updatedBy = UUID.randomUUID().toString(),
createdAt = Instant.now(),
updatedAt = Instant.now()
)
)
}
And this is the ReplacementBeanFactory where we replace the real instance of dynamodb for a fake one for the unit tests
@Factory
class ReplacementBeanFactory() {
@Singleton
@Replaces(DynamoDbAsyncClient::class)
fun dynamoDbAsyncClient(): DynamoDbAsyncClient {
val dynamoDbLocal = DynamoDBEmbedded.create();
return dynamoDbLocal.dynamoDbAsyncClient()
}
@Singleton
@Replaces(DynamoDbEnhancedAsyncClient::class)
fun dynamoDbEnhancedAsyncClient(): DynamoDbEnhancedAsyncClient {
return DynamoDbEnhancedAsyncClient.builder()
.dynamoDbClient(dynamoDbAsyncClient())
.build()
}
}