javatestingintegration-testingjmx

Testing: Unable to register MBean instance already exists


We have a few integration tests but recently we have faced a problem when a @ManagedResource is in the way:

Unable to register MBean [com.api.configuration.ApiConfiguration@63fde7ca] with key 'apiConfiguration'; nested exception is javax.management.InstanceAlreadyExistsException: api:name=ApiConfiguration

And the offending bean looks like this:

@Component
@ManagedResource(objectName = "api:name=ApiConfiguration")
public class ApiConfiguration {

    @ManagedOperation   
    public void reloadConfiguration() {
        // do something
    }

}

We have tried adding a @DirtiesContext on every integration test without success. The error only appears when running all tests from Maven or IntelliJ. If we run only one of the failing tests it works.


Solution

  • If you are using java based configuration you only need to add this

    @EnableMBeanExport(registration=RegistrationPolicy.REPLACE_EXISTING)
    

    OR

    @EnableMBeanExport(registration=RegistrationPolicy.IGNORE_EXISTING)
    

    So it will replace or use already existing bean (according to your configuration) and it will not give any kind of error