Home > OS >  Spring finds a test class on a jar and I get a NoUniqueBeanDefinitionException
Spring finds a test class on a jar and I get a NoUniqueBeanDefinitionException

Time:06-24

I have a project that uses Spring. The project consists on two different parts, the generic part and the specific one. The generic part is compiled as a .jar, it defines a set of traits and it's used as a dependency by the specific part, which is the one that implements the methods.

In order to test the generic part, I have created a "fake" implementation of one of the trait (let's say "fakeMethodA"), under the test directory of the generic project and I annotated this fake implementation with the @Component annotation. I'm getting the beans using the application context.

The problem comes when I try to use this generic part on the specific project. Since my actual implementation of this trait (let's say "methodAImplementation") also has a @Component annotation, when I run my tests I get:

org.springframework.beans.factory.NoUniqueBeanDefinitionException
expected single matching bean but found 2:

It finds the fakeMethodA from the generic part and methodAImplementation from the implementation. Is there any way to exclude this "fake" implementation from the execution? Is there a better way to define this?

Any help would be greatly appreciated.

CodePudding user response:

The problem was solved by the use of @Profile annotation on the generic method. I annotated the fake method on the tests with:

@Profile(value = Array("Test"))

And the right implementation with another profile value. After that, when I select the bean from the context, I can select the correct profile.

  • Related