Skip to content

Commit f480f28

Browse files
committed
java,tests: Replacing stubs with mocks
1 parent 04c1326 commit f480f28

File tree

10 files changed

+56
-129
lines changed

10 files changed

+56
-129
lines changed

Diff for: java/client/test/org/openqa/selenium/remote/AugmenterTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.junit.Test;
3535
import org.junit.runner.RunWith;
3636
import org.junit.runners.JUnit4;
37-
import org.mockito.Mockito;
3837
import org.openqa.selenium.By;
3938
import org.openqa.selenium.Capabilities;
4039
import org.openqa.selenium.WebDriver;

Diff for: java/client/test/org/openqa/selenium/remote/BaseAugmenterTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.junit.Assert.assertSame;
2424
import static org.junit.Assert.assertTrue;
2525
import static org.junit.Assert.fail;
26+
import static org.mockito.Mockito.mock;
2627

2728
import com.google.common.collect.ImmutableMap;
2829
import com.google.common.collect.Lists;
@@ -34,7 +35,6 @@
3435
import org.openqa.selenium.NoSuchElementException;
3536
import org.openqa.selenium.Rotatable;
3637
import org.openqa.selenium.ScreenOrientation;
37-
import org.openqa.selenium.StubDriver;
3838
import org.openqa.selenium.TakesScreenshot;
3939
import org.openqa.selenium.WebDriver;
4040
import org.openqa.selenium.WebElement;
@@ -48,7 +48,7 @@ public abstract class BaseAugmenterTest {
4848

4949
@Test
5050
public void shouldReturnANormalWebDriverUntouched() {
51-
WebDriver driver = new StubDriver();
51+
WebDriver driver = mock(WebDriver.class);
5252

5353
WebDriver returned = getAugmenter().augment(driver);
5454

Diff for: java/client/test/org/openqa/selenium/remote/DesiredCapabilitiesTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.junit.runners.JUnit4;
2424
import org.openqa.selenium.Capabilities;
2525
import org.openqa.selenium.Platform;
26-
import org.openqa.selenium.StubDriver;
2726
import org.openqa.selenium.WebDriver;
2827
import org.openqa.selenium.logging.LoggingPreferences;
2928

@@ -42,8 +41,8 @@ public void testAddingTheSameCapabilityToAMapTwiceShouldResultInOneEntry() {
4241
Map<org.openqa.selenium.Capabilities, Class<? extends WebDriver>> capabilitiesToDriver =
4342
new ConcurrentHashMap<Capabilities, Class<? extends WebDriver>>();
4443

45-
capabilitiesToDriver.put(DesiredCapabilities.firefox(), StubDriver.class);
46-
capabilitiesToDriver.put(DesiredCapabilities.firefox(), StubDriver.class);
44+
capabilitiesToDriver.put(DesiredCapabilities.firefox(), WebDriver.class);
45+
capabilitiesToDriver.put(DesiredCapabilities.firefox(), WebDriver.class);
4746

4847
assertEquals(1, capabilitiesToDriver.size());
4948
}

Diff for: java/client/test/org/openqa/selenium/support/ThreadGuardTest.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,27 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertNotNull;
2121
import static org.junit.Assert.assertNull;
22+
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.withSettings;
2224

2325
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
2428
import org.openqa.selenium.By;
25-
import org.openqa.selenium.StubDriver;
2629
import org.openqa.selenium.WebDriver;
27-
import org.openqa.selenium.WebElement;
2830
import org.openqa.selenium.interactions.HasTouchScreen;
2931

3032
import java.util.concurrent.atomic.AtomicInteger;
3133

3234
/**
3335
* @author Kristian Rosenvold
3436
*/
37+
@RunWith(JUnit4.class)
3538
public class ThreadGuardTest {
3639

3740
@Test
3841
public void testProtect() throws Exception {
39-
WebDriver actual = new PermissiveStubDriver();
42+
WebDriver actual = mock(WebDriver.class);
4043
final WebDriver protect = ThreadGuard.protect(actual);
4144
final AtomicInteger successes = new AtomicInteger();
4245
Thread foo = new Thread(new Runnable() {
@@ -52,23 +55,17 @@ public void run() {
5255

5356
@Test
5457
public void testProtectSuccess() throws Exception {
55-
WebDriver actual = new PermissiveStubDriver();
58+
WebDriver actual = mock(WebDriver.class);
5659
final WebDriver protect = ThreadGuard.protect(actual);
5760
assertNull(protect.findElement(By.id("foo")));
5861
}
5962

6063
@Test
6164
public void testInterfacesProxiedProperly() throws Exception {
62-
WebDriver actual = new PermissiveStubDriver();
65+
WebDriver actual = mock(WebDriver.class,
66+
withSettings().extraInterfaces(HasTouchScreen.class));
6367
final WebDriver webdriver = ThreadGuard.protect(actual);
6468
HasTouchScreen hasTouchScreen = (HasTouchScreen) webdriver;
6569
assertNotNull(hasTouchScreen);
6670
}
67-
68-
class PermissiveStubDriver extends StubDriver {
69-
@Override
70-
public WebElement findElement(By by) {
71-
return null;
72-
}
73-
}
7471
}

Diff for: java/client/test/org/openqa/selenium/support/events/EventFiringWebDriverTest.java

+13-23
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.mockito.Mockito.verify;
2727
import static org.mockito.Mockito.verifyNoMoreInteractions;
2828
import static org.mockito.Mockito.when;
29+
import static org.mockito.Mockito.withSettings;
2930

3031
import org.junit.Test;
3132
import org.junit.runner.RunWith;
@@ -269,7 +270,8 @@ public void onException(Throwable throwable, WebDriver driver) {
269270

270271
@Test
271272
public void shouldUnpackElementArgsWhenCallingScripts() {
272-
final ExececutingDriver mockedDriver = mock(ExececutingDriver.class);
273+
final WebDriver mockedDriver = mock(WebDriver.class,
274+
withSettings().extraInterfaces(JavascriptExecutor.class));
273275
final WebElement stubbedElement = mock(WebElement.class);
274276

275277
when(mockedDriver.findElement(By.id("foo"))).thenReturn(stubbedElement);
@@ -279,12 +281,13 @@ public void shouldUnpackElementArgsWhenCallingScripts() {
279281

280282
WebElement element = testedDriver.findElement(By.id("foo"));
281283
testedDriver.executeScript("foo", element);
282-
verify(mockedDriver).executeScript("foo", element);
284+
verify((JavascriptExecutor) mockedDriver).executeScript("foo", element);
283285
}
284286

285287
@Test
286288
public void testShouldUnpackListOfElementArgsWhenCallingScripts() {
287-
final ExececutingDriver mockedDriver = mock(ExececutingDriver.class);
289+
final WebDriver mockedDriver = mock(WebDriver.class,
290+
withSettings().extraInterfaces(JavascriptExecutor.class));
288291
final WebElement mockElement = mock(WebElement.class);
289292

290293
when(mockedDriver.findElement(By.id("foo"))).thenReturn(mockElement);
@@ -302,7 +305,7 @@ public void testShouldUnpackListOfElementArgsWhenCallingScripts() {
302305
add("after");
303306
}});
304307

305-
verify(mockedDriver).executeScript("foo", new ArrayList<Object>() {{
308+
verify((JavascriptExecutor) mockedDriver).executeScript("foo", new ArrayList<Object>() {{
306309
add("before");
307310
add(mockElement);
308311
add("after");
@@ -311,7 +314,8 @@ public void testShouldUnpackListOfElementArgsWhenCallingScripts() {
311314

312315
@Test
313316
public void testShouldUnpackMapOfElementArgsWhenCallingScripts() {
314-
final ExececutingDriver mockedDriver = mock(ExececutingDriver.class);
317+
final WebDriver mockedDriver = mock(WebDriver.class,
318+
withSettings().extraInterfaces(JavascriptExecutor.class));
315319
final WebElement mockElement = mock(WebElement.class);
316320

317321
when(mockedDriver.findElement(By.id("foo"))).thenReturn(mockElement);
@@ -334,7 +338,7 @@ public void testShouldUnpackMapOfElementArgsWhenCallingScripts() {
334338
}});
335339
}});
336340

337-
verify(mockedDriver).executeScript("foo", new HashMap<String, Object>() {{
341+
verify((JavascriptExecutor) mockedDriver).executeScript("foo", new HashMap<String, Object>() {{
338342
put("foo", "bar");
339343
put("element", mockElement);
340344
put("nested", new ArrayList<Object>() {{
@@ -358,27 +362,15 @@ public void shouldBeAbleToWrapSubclassesOfSomethingImplementingTheWebDriverInter
358362

359363
@Test
360364
public void shouldBeAbleToAccessWrappedInstanceFromEventCalls() {
361-
class MyStub extends StubDriver {
362-
@Override
363-
public void get(String url) {
364-
// Do nothing
365-
}
366-
367-
@SuppressWarnings("unused")
368-
public void fishy() {
369-
}
370-
371-
}
372-
373-
final WebDriver stub = new MyStub();
365+
final WebDriver stub = mock(WebDriver.class);
374366
EventFiringWebDriver driver = new EventFiringWebDriver(stub);
375-
MyStub wrapped = (MyStub) driver.getWrappedDriver();
367+
WebDriver wrapped = driver.getWrappedDriver();
376368
assertEquals(stub, wrapped);
377369

378370
class MyListener extends AbstractWebDriverEventListener {
379371
@Override
380372
public void beforeNavigateTo(String url, WebDriver driver) {
381-
MyStub unwrapped = (MyStub) ((WrapsDriver) driver).getWrappedDriver();
373+
WebDriver unwrapped = ((WrapsDriver) driver).getWrappedDriver();
382374

383375
assertEquals(stub, unwrapped);
384376
}
@@ -424,7 +416,5 @@ public void shouldReturnLocatorFromToStringMethod() {
424416
assertEquals(stubElement.toString(), firingElement.toString());
425417
}
426418

427-
private static interface ExececutingDriver extends WebDriver, JavascriptExecutor {}
428-
429419
private static class ChildDriver extends StubDriver {}
430420
}

Diff for: java/client/test/org/openqa/selenium/support/pagefactory/UsingPageFactoryTest.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@
2121
import static org.hamcrest.Matchers.equalTo;
2222
import static org.junit.Assert.assertEquals;
2323
import static org.junit.Assert.assertThat;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.when;
2426

2527
import org.junit.Test;
26-
import org.openqa.selenium.By;
2728
import org.openqa.selenium.JavascriptExecutor;
28-
import org.openqa.selenium.StubDriver;
2929
import org.openqa.selenium.WebDriver;
3030
import org.openqa.selenium.WebElement;
3131
import org.openqa.selenium.remote.RemoteWebElement;
3232
import org.openqa.selenium.remote.internal.WebElementToJsonConverter;
33+
import org.openqa.selenium.support.ByIdOrName;
3334
import org.openqa.selenium.support.CacheLookup;
3435
import org.openqa.selenium.support.FindBy;
3536
import org.openqa.selenium.support.PageFactory;
@@ -72,12 +73,8 @@ public void testDecoratedElementsShouldBeUnwrapped() {
7273
final RemoteWebElement element = new RemoteWebElement();
7374
element.setId("foo");
7475

75-
WebDriver driver = new StubDriver() {
76-
@Override
77-
public WebElement findElement(By by) {
78-
return element;
79-
}
80-
};
76+
WebDriver driver = mock(WebDriver.class);
77+
when(driver.findElement(new ByIdOrName("element"))).thenReturn(element);
8178

8279
PublicPage page = new PublicPage();
8380
PageFactory.initElements(driver, page);

Diff for: java/server/test/org/openqa/selenium/remote/server/DefaultSessionTest.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717

1818
package org.openqa.selenium.remote.server;
1919

20+
import static org.mockito.Matchers.any;
2021
import static org.mockito.Mockito.mock;
2122
import static org.mockito.Mockito.verify;
23+
import static org.mockito.Mockito.when;
2224

2325
import org.junit.Test;
2426
import org.junit.runner.RunWith;
2527
import org.junit.runners.JUnit4;
28+
import org.openqa.selenium.Capabilities;
29+
import org.openqa.selenium.WebDriver;
2630
import org.openqa.selenium.io.TemporaryFilesystem;
2731
import org.openqa.selenium.remote.DesiredCapabilities;
2832

@@ -31,7 +35,8 @@ public class DefaultSessionTest {
3135

3236
@Test
3337
public void shouldClearTempFsWhenSessionCloses() throws Exception {
34-
final DriverFactory factory = new StubDriverFactory();
38+
final DriverFactory factory = mock(DriverFactory.class);
39+
when(factory.newInstance(any(Capabilities.class))).thenReturn(mock(WebDriver.class));
3540
final TemporaryFilesystem tempFs = mock(TemporaryFilesystem.class);
3641

3742
Session session = DefaultSession.createSession(factory, tempFs, null, DesiredCapabilities.firefox());

Diff for: java/server/test/org/openqa/selenium/remote/server/SessionCleanerTest.java

+10-34
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919

2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertTrue;
22+
import static org.mockito.Matchers.any;
2223
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.verify;
25+
import static org.mockito.Mockito.when;
26+
import static org.mockito.Mockito.withSettings;
2327

2428
import org.junit.Test;
2529
import org.junit.runner.RunWith;
@@ -29,7 +33,6 @@
2933
import org.openqa.selenium.WebDriver;
3034
import org.openqa.selenium.internal.Killable;
3135
import org.openqa.selenium.remote.DesiredCapabilities;
32-
import org.openqa.selenium.remote.RemoteWebDriver;
3336
import org.openqa.selenium.remote.SessionId;
3437
import org.openqa.selenium.remote.server.testing.StaticTestSessions;
3538

@@ -60,7 +63,9 @@ public void testCleanup() throws Exception {
6063
@Test
6164
public void testCleanupWithTimedOutKillableDriver() throws Exception {
6265
Capabilities capabilities = new DesiredCapabilities("foo", "1", Platform.ANY);
63-
DriverSessions testSessions = new StaticTestSessions(capabilities, new KillableDriver());
66+
WebDriver killableDriver = mock(WebDriver.class,
67+
withSettings().extraInterfaces(Killable.class));
68+
DriverSessions testSessions = new StaticTestSessions(capabilities, killableDriver);
6469

6570
final Session session = testSessions.get(testSessions.newSession(capabilities));
6671
final CountDownLatch started = new CountDownLatch(1);
@@ -69,13 +74,12 @@ public void testCleanupWithTimedOutKillableDriver() throws Exception {
6974
new Thread( runnable).start();
7075
started.await();
7176

72-
KillableDriver killableDriver = (KillableDriver) session.getDriver();
7377
assertTrue(session.isInUse());
7478
SessionCleaner sessionCleaner = new SessionCleaner(testSessions, log, 10, 10);
7579
waitForAllSessionsToExpire(11);
7680
sessionCleaner.checkExpiry();
7781
assertEquals(0, testSessions.getSessions().size());
78-
assertTrue(killableDriver.killed);
82+
verify((Killable) killableDriver).kill();
7983
testDone.countDown();
8084
}
8185

@@ -155,36 +159,8 @@ public void testCleanupWithSessionExtension() throws Exception {
155159
}
156160

157161
private DriverSessions getDriverSessions() {
158-
DriverFactory factory = new MyDriverFactory();
162+
DriverFactory factory = mock(DriverFactory.class);
163+
when(factory.newInstance(any(Capabilities.class))).thenReturn(mock(WebDriver.class));
159164
return new DefaultDriverSessions(Platform.LINUX, factory);
160165
}
161-
162-
class MyDriverFactory implements DriverFactory {
163-
@Override
164-
public void registerDriver(Capabilities capabilities, Class<? extends WebDriver> implementation) {
165-
}
166-
167-
@Override
168-
public void registerDriverProvider(Capabilities capabilities, DriverProvider implementation) {
169-
}
170-
171-
@Override
172-
public WebDriver newInstance(Capabilities capabilities) {
173-
return mock(WebDriver.class);
174-
}
175-
176-
@Override
177-
public boolean hasMappingFor(Capabilities capabilities) {
178-
return true;
179-
}
180-
}
181-
182-
static class KillableDriver extends RemoteWebDriver implements Killable {
183-
boolean killed;
184-
185-
public void kill() {
186-
killed = true;
187-
}
188-
}
189-
190166
}

Diff for: java/server/test/org/openqa/selenium/remote/server/StubDriverFactory.java

-42
This file was deleted.

0 commit comments

Comments
 (0)