Skip to content

Instantly share code, notes, and snippets.

@rivancic
Last active April 20, 2017 14:59
Show Gist options
  • Save rivancic/f0a4b7ed8083c8cd2822b4f6dd93502f to your computer and use it in GitHub Desktop.
Save rivancic/f0a4b7ed8083c8cd2822b4f6dd93502f to your computer and use it in GitHub Desktop.
Spring

Spring

Configuration

Unit testing

@ContextConfiguration

Integration testing

Basic example on DZone

Watch out that the dependency tree is clean. If ComponentScan can pick up two matching beans you'll get NoUniqueBeanDefinitionException.

Unsatisfied dependency expressed through field 'xxx'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'xxx' available: expected single matching bean but found 2

It can happen in case of running integration tests from same package as unit tests. If for Unit test we use specific @ContextConfiguration. Then beans will be taken from specific class. Then if in integration tests we want to use default context it will pick beans from main as well from test directory.

Solution found on SO

I annotated service in application with @Qualifier("qualifier_name")

And then in the application controller I set that it is using only dependency with specific "qualifier_name".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment