How to use JUnitRuleMockery class of org.jmock.integration.junit4 package

Best Jmock-library code snippet using org.jmock.integration.junit4.JUnitRuleMockery

Run Jmock-library automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 * Copyright 2015 The Error Prone Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.errorprone.bugpatterns;
18
19import com.google.errorprone.CompilationTestHelper;
20import org.junit.Before;
21import org.junit.Test;
22import org.junit.runner.RunWith;
23import org.junit.runners.JUnit4;
24
25@RunWith(JUnit4.class)
26public class JMockTestWithoutRunWithOrRuleAnnotationTest {
27
28  private CompilationTestHelper compilationTestHelper;
29
30  @Before
31  public void setup() {
32    compilationTestHelper =
33        CompilationTestHelper.newInstance(
34            JMockTestWithoutRunWithOrRuleAnnotation.class, getClass());
35  }
36
37  @Test
38  public void testShouldFlagNoRuleAndNoRunWith() {
39    compilationTestHelper
40        .addSourceLines(
41            "Test.java",
42            "import org.jmock.Mockery;",
43            "public class Test {",
44            "  // BUG: Diagnostic contains:",
45            "  private final Mockery mockery = new Mockery();",
46            "}")
47        .doTest();
48  }
49
50  @Test
51  public void testShouldFlagWrongRunWith() {
52    compilationTestHelper
53        .addSourceLines(
54            "Test.java",
55            "import org.jmock.Mockery;",
56            "import org.junit.runner.RunWith;",
57            "import org.junit.runners.Parameterized;",
58            "@RunWith(Parameterized.class)",
59            "public class Test {",
60            "  // BUG: Diagnostic contains:",
61            "  private final Mockery mockery = new Mockery();",
62            "}")
63        .doTest();
64  }
65
66  @Test
67  public void testShouldFlagJUnitRuleMockery() {
68    compilationTestHelper
69        .addSourceLines(
70            "Test.java",
71            "import org.jmock.integration.junit4.JUnitRuleMockery;",
72            "public class Test {",
73            "  // BUG: Diagnostic contains:",
74            "  public final JUnitRuleMockery mockery = new JUnitRuleMockery();",
75            "}")
76        .doTest();
77  }
78
79  @Test
80  public void testShouldNotFlagWithRuleAnnotation() {
81    compilationTestHelper
82        .addSourceLines(
83            "Test.java",
84            "import org.jmock.Mockery;",
85            "import org.jmock.integration.junit4.JUnitRuleMockery;",
86            "import org.junit.Rule;",
87            "public class Test {",
88            "  @Rule",
89            "  private final Mockery mockery = new Mockery();",
90            "  @Rule",
91            "  public final JUnitRuleMockery mockery2 = new JUnitRuleMockery();",
92            "}")
93        .doTest();
94  }
95
96  @Test
97  public void testShouldNotFlagWithRuleAnnotationAndWrongRunWith() {
98    compilationTestHelper
99        .addSourceLines(
100            "Test.java",
101            "import org.jmock.Mockery;",
102            "import org.jmock.integration.junit4.JUnitRuleMockery;",
103            "import org.junit.Rule;",
104            "import org.junit.runner.RunWith;",
105            "import org.junit.runners.Parameterized;",
106            "@RunWith(Parameterized.class)",
107            "public class Test {",
108            "  @Rule",
109            "  private final Mockery mockery = new Mockery();",
110            "  @Rule",
111            "  public final JUnitRuleMockery mockery2 = new JUnitRuleMockery();",
112            "}")
113        .doTest();
114  }
115
116  @Test
117  public void testShouldNotFlagRunWithJMock() {
118    compilationTestHelper
119        .addSourceLines(
120            "Test.java",
121            "import org.jmock.Mockery;",
122            "import org.jmock.integration.junit4.JMock;",
123            "import org.jmock.integration.junit4.JUnitRuleMockery;",
124            "import org.junit.runner.RunWith;",
125            "@RunWith(JMock.class)",
126            "public class Test {",
127            "  private final Mockery mockery = new org.jmock.Mockery();",
128            "  public final JUnitRuleMockery mockery2 = new JUnitRuleMockery();",
129            "}")
130        .doTest();
131  }
132
133  @Test
134  public void testShouldNotFlagWithRuleAnnotationAndRunWithJMock() {
135    compilationTestHelper
136        .addSourceLines(
137            "Test.java",
138            "import org.jmock.Mockery;",
139            "import org.jmock.integration.junit4.JMock;",
140            "import org.jmock.integration.junit4.JUnitRuleMockery;",
141            "import org.junit.Rule;",
142            "import org.junit.runner.RunWith;",
143            "@RunWith(JMock.class)",
144            "public class Test {",
145            "  @Rule",
146            "  private final Mockery mockery = new Mockery();",
147            "  @Rule",
148            "  public final JUnitRuleMockery mockery2 = new JUnitRuleMockery();",
149            "}")
150        .doTest();
151  }
152
153  @Test
154  public void testShouldNotFlagSingleFieldWithRuleAnnotation() {
155    compilationTestHelper
156        .addSourceLines(
157            "Test.java",
158            "import org.junit.Rule;",
159            "import org.jmock.Mockery;",
160            "public class Test {",
161            "  @Rule",
162            "  final Mockery mockery = new Mockery();",
163            "}")
164        .doTest();
165  }
166}
167
Full Screen
copy
1package lesson151027;
2
3import static org.junit.Assert.*;
4
5import org.jmock.Expectations;
6import org.jmock.integration.junit4.JMock;
7import org.jmock.integration.junit4.JUnitRuleMockery;
8import org.junit.Rule;
9import org.junit.Test;
10import org.junit.runner.RunWith;
11
12
13//@RunWith(JMock.class)// = @Rule below
14public class ATest {
15
16	@Rule public JUnitRuleMockery context = new JUnitRuleMockery();
17	
18	@Test
19	public void test() {
20		final Crashable c = context.mock(Crashable.class);
21		
22		A a = new A(c);
23		
24		context.checking(new Expectations() {{
25//		    oneOf(c).crash();
26		    this.atLeast(2).of(c).crash();
27		}});
28		a.destroy();
29}
30}
31
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on JUnitRuleMockery

    No relevant questions found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)