Fix “Can’t find a codec for CodecCacheKey” in Micronaut

This photo is a featured image for the article about fixing issue with CodecCacheKey in Micronaut and consist of the Micronaut logo in the foreground and a blurred photo in the background,

If you have ever struggled with the “Unexpected error occurred: Can’t find a codec for CodecCacheKey” when testing in Micronaut, then you came to the right place 🙂

In this short article, I will show you how to fix this issue based on the example project.

Trigger The CodecCacheKey in Micronaut Test

Before I show you how to fix this issue, let me quickly introduce you to how I trigger the CodecCacheKey issue when writing tests in Micronaut. (If you are interested in the whole project, check this article about Micronaut with MongoDB).

So firstly, I created a simple AppUser data class:

import io.micronaut.data.annotation.GeneratedValue
import io.micronaut.data.annotation.Id
import io.micronaut.data.annotation.MappedEntity

@MappedEntity
data class AppUser(
  @field:Id
  @field:GeneratedValue
  val id: String? = null,
  val firstName: String,
  val lastName: String,
  val email: String,
  val address: Address
)

Nothing spectacular. Just a simple class with a few annotations necessary to work with MongoDB and generate identifiers automatically.

As the next step, I added the repository, which looked, as follows:

@MongoRepository
interface AppUserRepository : CrudRepository<AppUser, String> {
 
  // few methods 
}

And all of that together with other classes that are not important in this tutorial, created a structure, like this:

Image presents the package structure of the backend Kotlin project created with Micronaut that was used to trigger the "Can't find a codec for CodecCacheKey" issue

So, lastly, I added a simple test with @MicronautTest, JUnit 5, and REST Assured to test whether my REST endpoint returns users from the Mongo database:

@MicronautTest
class AppUserControllerTestWithoutMocking {

  @Test
  fun `should return 200 OK on GET users`(spec: RequestSpecification) {
    spec
      .`when`()
      .get("/users")
      .then()
      .statusCode(200)
      .header("Content-Type", "application/json")
  }
}

As a result, instead of the beautiful, green icon indicating that everything is fine, I got the following:

[io-executor-thread-1] ERROR i.m.http.server.RouteExecutor - Unexpected error occurred: Can't find a codec for CodecCacheKey{clazz=class com.codersee.model.AppUser, types=null}.
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for CodecCacheKey{clazz=class com.codersee.model.AppUser, types=null}.
	at org.bson.internal.ProvidersCodecRegistry.lambda$get$0(ProvidersCodecRegistry.java:87)
	at java.base/java.util.Optional.orElseGet(Optional.java:364)
	at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:80)
	at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:50)
	at com.mongodb.internal.operation.Operations.createFindOperation(Operations.java:188)
...

Moreover, when I ran the application manually and tested using a real MongoDB instance, everything worked fine.

How To Fix CodecCacheKey Issue?

In order to fix the issue, I created a new file- called application-test.properties in the resources directory and put the following:

mongodb.package-names=com.codersee.model

But what exactly does it do?

Well, firstly, the application-test name of the file instructs the Micronaut framework to use this property file only when running tests.

Secondly, with the mongodb.package-names, I set package names to allow for POJOs. And that’s why I put the com.codersee.model only.

And voila, that’s all 🙂

Summary

And basically, that’s all for this short tutorial on how to fix the issue with CodecCacheKey in Micronaut.

Thank you for being here, and if you would like to learn more about the framework, then check out other posts from the Micronaut category.

If you would like to see the codebase for this tutorial, please refer to my GitHub repository.

Share this:

Hi there! 👋

Hi there! 👋

My name is Piotr and I've created Codersee to share my knowledge about Kotlin, Spring Framework, and other related topics through practical, step-by-step guides. Always eager to chat and exchange knowledge.

Related content

Newsletter

Image presents 3 ebooks with Java, Spring and Kotlin interview questions.

Never miss any important updates from the Kotlin world and get 3 ebooks!

You may opt out any time. Terms of Use and Privacy Policy

To make Codersee work, we log user data. By using our site, you agree to our Privacy Policy and Terms of Use.