1 package com.atlassian.webdriver.rule;
2
3 import com.atlassian.pageobjects.PageBinder;
4 import com.atlassian.webdriver.it.pageobjects.SimpleTestedProduct;
5 import com.atlassian.webdriver.testing.rule.InjectionRules;
6 import org.junit.BeforeClass;
7 import org.junit.ClassRule;
8 import org.junit.Rule;
9 import org.junit.Test;
10 import org.junit.rules.TestRule;
11 import org.junit.runner.JUnitCore;
12 import org.junit.runner.Result;
13 import org.junit.runner.notification.Failure;
14
15 import javax.inject.Inject;
16
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22
23
24
25
26
27
28
29 public class TestClassAndMemberInjectionRule
30 {
31
32 @ClassRule public static TestRule staticInjectionRule = InjectionRules.forTestClass(SimpleTestedProduct.class);
33
34 @Rule public TestRule memberInjectionRule = InjectionRules.forTestInContext(this);
35
36 @Inject
37 private static SimpleTestedProduct staticSimpleProduct;
38
39 @Inject
40 private static PageBinder staticBinder;
41
42 @Inject
43 private SimpleTestedProduct memberSimpleProduct;
44
45 @Inject
46 private PageBinder memberBinder;
47
48
49 @BeforeClass
50 public static void checkStaticsInjected()
51 {
52 assertNotNull("Static product should get injected", staticSimpleProduct);
53 assertNotNull("Static binder should get injected", staticBinder);
54 }
55
56 @Test
57 public void checkMembersInjected()
58 {
59 assertNotNull("Member product should get injected", memberSimpleProduct);
60 assertNotNull("Member binder should get injected", memberBinder);
61 }
62
63 @Test
64 public void shouldFailIfTryingToRunTestWithMemberRuleExpectingContextThatDoesNotExist()
65 {
66
67 final Result result = JUnitCore.runClasses(WrongTest.class);
68 assertFalse(result.wasSuccessful());
69 assertEquals(1, result.getFailures().size());
70 final Failure theFailure = result.getFailures().get(0);
71 assertTrue(theFailure.getException() instanceof IllegalStateException);
72 }
73
74
75
76
77
78
79 public static class WrongTest
80 {
81 @Rule public TestRule memberInjectionRule = InjectionRules.forTestInContext(this);
82
83 @Test
84 public void iWillNeverBeCalledOhMy()
85 {
86 fail("Nooooo I was not supposed to be caaaaalllled!!!");
87 }
88 }
89
90 }