Skip to content

Unable to use Dependency Injection in Service class when having final method #595

@Pavlin619

Description

@Pavlin619

Currently, we are trying to integrate Spring Boot Chaos Monkey library into our project. During the process, we wanted to create an experiment, which includes delayed response from one of our services.

The service under test is an abstract class annotated with @service and @requiredargsconstructor and has a few classes, which are injected into it. To be more precise, we are testing a concrete implementation of the abstract class, but the method that we are testing is public final in the abstract class, therefore i am describing the abstract class.

When we add the dependency to Chaos Monkey library and we use spring.profiles.active: chaos-monkey, each dependency in the abstract class appear to be null. Therefore, it throws NPE, when we invoke the request. This happens only when we activate the chaos-monkey profile and is not the current production behaviour.

After digging down into the possible root cause for 2 days, I found out that the problem is actually caused by the final keyword in the method in the abstract class. When, I remove it works as expected, but it is pretty strange side effect. Moreover, changing the production code for the sake of integrating the library is not the desired approach, so i would be glad if you can assist with solving this issue and finding the root cause for it.

Expected Behavior

When i activate the Spring Boot Chaos Monkey library, I do not expect to touch any of my production code and if i have not configured any assaults I want it to act as if there is not dependency to this library. In other words, I do not want to remove the final keyword from my code :)

Current Behavior

Spring is not able to inject any dependency to the service that has final method and all of them appear to be null.

Possible Solution

Removing final keyword from the method signature solves the issue for some reason.

Steps to Reproduce

In my case, i am producing the issue with having an abstract class with public final method, which uses injected class in its logic. Apparently, all the injected classes in the class appear to be null (Spring is not able to inject them), so this could also be seen from debug console.
The issue is reproduced with the latest version 3.2.2. I also reproduced it with 3.1.0, 3.0.2, 3.1.2.
Our Spring Boot application is using Spring Boot 3.4.5.

Context (Environment)

This issue becomes a deal breaker for integrating the library, because we are afraid of these side effects to our production code and we do not want test library integration to affect the behaviour of our newly developed features.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions