Last active
February 24, 2016 11:48
-
-
Save odrotbohm/5237418 to your computer and use it in GitHub Desktop.
Sample test case showing how to use Spring JavaConfig with qualifiers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2013 the original author or authors. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package org.springsource.restbucks; | |
import static org.hamcrest.CoreMatchers.*; | |
import static org.junit.Assert.*; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Qualifier; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
/** | |
* @author Oliver Gierke | |
*/ | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration | |
public class FooTest { | |
static MyComponent first, second, third; | |
@BeforeClass | |
public static void setUp() { | |
first = new MyComponent(); | |
second = new MyComponent(); | |
third = new MyComponent(); | |
} | |
@Configuration | |
static class Config { | |
@Bean | |
@Qualifier("black") | |
public MyComponent first() { | |
return first; | |
} | |
@Bean | |
@Qualifier("white") | |
public MyComponent second() { | |
return second; | |
} | |
@Bean | |
@MyQualifier | |
public MyComponent third() { | |
return third; | |
} | |
} | |
static class MyComponent { | |
} | |
@Target({ ElementType.FIELD, ElementType.METHOD }) | |
@Retention(RetentionPolicy.RUNTIME) | |
@Qualifier | |
static @interface MyQualifier { | |
} | |
@Autowired | |
@Qualifier("black") | |
MyComponent wiredFirst; | |
@Autowired | |
@Qualifier("white") | |
MyComponent wiredSecond; | |
@Autowired | |
@MyQualifier | |
MyComponent wiredThird; | |
@Test | |
public void assertQualifierWiring() { | |
assertThat(wiredFirst, is(first)); | |
assertThat(wiredSecond, is(second)); | |
assertThat(wiredThird, is(third)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment