I was adding some JUnit tests in my NixMash Spring app when I encountered an InstanceAlreadyExistsException exception. Here’s more googly.
Caused by: org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [org.apache.tomcat.dbcp.dbcp2.BasicDataSource@26667415] with key 'dataSource'; nested exception is javax.management.InstanceAlreadyExistsException: org.apache.tomcat.dbcp.dbcp2:name=dataSource,type=BasicDataSource
Keywords: UnableToRegisterMBeanException, InstanceAlreadyExistsException
Here are the test classes. The exceptions started when I added the Security Tests class, and only occurred when I ran ALL tests. Running either the ContactControllerTest or SecurityTests was successful. With that as a backdrop I’ll bet savvy JUnit’ers already know the problem.
And that problem was loading the Spring Context with each test class. The solution was restructuring the test classes by extending an AbstractContext base class which loaded a Spring Context for all tests to share, and all the Beans therein.
The test source described in this post can be found in the v0.1.6 branch of NixMash Spring on GitHub.