Tuesday, June 13, 2023

How to Fix Spring Boot Restdocs java.lang.NoSuchMethodException: java.util.List.() Error

Resolving "java.lang.NoSuchMethodException" Error in Restdocs

Restdocs is a valuable tool for testing REST APIs, but using it can sometimes be intricate and time-consuming. Recently, I came across the error "java.lang.NoSuchMethodException: java.util.List.<init>()" while utilizing Restdocs. This error surfaced in the controller where I was receiving a list as a parameter.

Seeking the Solution

I sifted through a plethora of resources in an attempt to rectify this error, but a clear solution was difficult to find. However, I managed to find a solution by carefully analyzing the error.

The Solution

The resolution is to modify the list to an array in the controller that receives it as a parameter. In other words, implementing the following changes will mitigate the error.

// Before
public String getSomething(List<String> stringList) {

}

// After
public String getSomething(String[] stringArray) {

}

Understanding the Root Cause

The root cause of this error is that webMvcTest cannot correctly identify the List. While Spring Boot's auto-configuration is highly convenient, it can sometimes lead to confusion in scenarios like this one.

Conclusion

I hope this article will be beneficial for those who are grappling with errors while using Restdocs.


0 개의 댓글:

Post a Comment