File tree Expand file tree Collapse file tree
main/java/org/baeldung/order
test/java/org/baeldung/order Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package org .baeldung .order ;
2+
3+ import org .springframework .core .Ordered ;
4+ import org .springframework .core .annotation .Order ;
5+ import org .springframework .stereotype .Component ;
6+
7+ @ Component
8+ @ Order (Ordered .LOWEST_PRECEDENCE )
9+ public class Average implements Rating {
10+
11+ @ Override
12+ public int getRating () {
13+ return 3 ;
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .order ;
2+
3+ import org .springframework .stereotype .Component ;
4+ import org .springframework .core .annotation .Order ;
5+
6+ @ Component
7+ @ Order (1 )
8+ public class Excellent implements Rating {
9+
10+ @ Override
11+ public int getRating () {
12+ return 1 ;
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .order ;
2+
3+ import org .springframework .core .annotation .Order ;
4+ import org .springframework .stereotype .Component ;
5+
6+ @ Component
7+ @ Order (2 )
8+ public class Good implements Rating {
9+
10+ @ Override
11+ public int getRating () {
12+ return 2 ;
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .order ;
2+
3+ public interface Rating {
4+
5+ int getRating ();
6+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .order ;
2+
3+
4+ import static org .hamcrest .CoreMatchers .equalTo ;
5+ import static org .hamcrest .CoreMatchers .is ;
6+ import static org .junit .Assert .assertThat ;
7+
8+ import java .util .List ;
9+
10+ import org .junit .Test ;
11+ import org .junit .runner .RunWith ;
12+ import org .springframework .beans .factory .annotation .Autowired ;
13+ import org .springframework .context .annotation .ComponentScan ;
14+ import org .springframework .context .annotation .Configuration ;
15+ import org .springframework .test .context .ContextConfiguration ;
16+ import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
17+ import org .springframework .test .context .support .AnnotationConfigContextLoader ;
18+
19+ @ RunWith (SpringJUnit4ClassRunner .class )
20+ @ ContextConfiguration (loader = AnnotationConfigContextLoader .class )
21+ public class RatingRetrieverUnitTest {
22+
23+ @ Configuration
24+ @ ComponentScan (basePackages = {"org.baeldung.order" })
25+ static class ContextConfiguration {}
26+
27+ @ Autowired
28+ private List <Rating > ratings ;
29+
30+ @ Test
31+ public void givenOrderOnComponents_whenInjected_thenAutowireByOrderValue () {
32+ assertThat (ratings .get (0 ).getRating (), is (equalTo (1 )));
33+ assertThat (ratings .get (1 ).getRating (), is (equalTo (2 )));
34+ assertThat (ratings .get (2 ).getRating (), is (equalTo (3 )));
35+ }
36+
37+ }
You can’t perform that action at this time.
0 commit comments