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

In this short article, I will show you how to solve the "Unexpected error occurred: 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:


data class AppUser(
  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:

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:

class AppUserControllerTestWithoutMocking {

  fun `should return 200 OK on GET users`(spec: RequestSpecification) {
      .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(
	at java.base/java.util.Optional.orElseGet(
	at org.bson.internal.ProvidersCodecRegistry.get(
	at org.bson.internal.ProvidersCodecRegistry.get(
	at com.mongodb.internal.operation.Operations.createFindOperation(

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 in the resources directory and put the following:


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 🙂


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:

Related content

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