Skip to content

Instantly share code, notes, and snippets.

@acdcjunior
Created January 23, 2016 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acdcjunior/21c17b9209c09770d019 to your computer and use it in GitHub Desktop.
Save acdcjunior/21c17b9209c09770d019 to your computer and use it in GitHub Desktop.
Mockito vs. Hand-built test doubles (Mocks) small performance test
import org.junit.Test;
import org.mockito.Mockito;
import org.openqa.selenium.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/*
* Is mockito's performance cost noticeable in the long run? It depends. Say it for yourself.
*/
public class MockitoVsHandBuiltDoublesTest {
private static final int NUMBER_OF_RUNS = 1000;
private static final String SOME_VALUE = "someStuff";
@Test public void withoutMockito01() { without(); }
@Test public void withMockito01() { with(); }
@Test public void withMockito02() { with(); }
@Test public void withoutMockito02() { without(); }
@Test public void withoutMockito03() { without(); }
@Test public void withMockito03() { with(); }
@Test public void withMockito04() { with(); }
@Test public void withoutMockito04() { without(); }
@Test public void withoutMockito05() { without(); }
@Test public void withoutMockito06() { without(); }
@Test public void withMockito05() { with(); }
@Test public void withMockito06() { with(); }
@Test public void withMockito07() { with(); }
@Test public void withoutMockito07() { without(); }
@Test public void withMockito08() { with(); }
@Test public void withoutMockito08() { without(); }
@Test public void withoutMockito09() { without(); }
@Test public void withMockito09() { with(); }
@Test public void withMockito10() { with(); }
@Test public void withoutMockito10() { without(); }
private void without() {
for (int i = 0; i < NUMBER_OF_RUNS; i++) {
runWithout();
}
}
private void with() {
for (int i = 0; i < NUMBER_OF_RUNS; i++) {
runWith();
}
}
private void runWithout() {
WebDriver d = new WebDriverTestDouble(SOME_VALUE);
assertThat(d.getTitle(), is(SOME_VALUE));
WebElement e = new WebElementTestDouble(SOME_VALUE);
assertThat(e.getTagName(), is(SOME_VALUE));
Capabilities c = new CapabilitiesTestDouble(SOME_VALUE);
assertThat(c.getBrowserName(), is(SOME_VALUE));
}
private void runWith() {
WebDriver d = Mockito.mock(WebDriver.class);
Mockito.when(d.getTitle()).thenReturn(SOME_VALUE);
assertThat(d.getTitle(), is(SOME_VALUE));
WebElement e = Mockito.mock(WebElement.class);
Mockito.when(e.getTagName()).thenReturn(SOME_VALUE);
assertThat(e.getTagName(), is(SOME_VALUE));
Capabilities c = Mockito.mock(Capabilities.class);
Mockito.when(c.getBrowserName()).thenReturn(SOME_VALUE);
assertThat(c.getBrowserName(), is(SOME_VALUE));
}
}
class WebDriverTestDouble implements WebDriver {
private final String title;
public WebDriverTestDouble(String title) {
this.title = title;
}
@Override public String getTitle() { return title; }
@Override public void get(String s) { }
@Override public String getCurrentUrl() { return null; }
@Override public List<WebElement> findElements(By by) { return null; }
@Override public WebElement findElement(By by) { return null; }
@Override public String getPageSource() { return null; }
@Override public void close() { }
@Override public void quit() { }
@Override public Set<String> getWindowHandles() { return null; }
@Override public String getWindowHandle() { return null; }
@Override public TargetLocator switchTo() { return null; }
@Override public Navigation navigate() { return null; }
@Override public Options manage() { return null; }
}
class WebElementTestDouble implements WebElement {
private final String tagName;
public WebElementTestDouble(String tagName) {
this.tagName = tagName;
}
@Override public String getTagName() { return this.tagName; }
@Override public void click() { }
@Override public void submit() { }
@Override public void sendKeys(CharSequence... charSequences) { }
@Override public void clear() { }
@Override public String getAttribute(String s) { return null; }
@Override public boolean isSelected() { return false; }
@Override public boolean isEnabled() { return false; }
@Override public String getText() { return null; }
@Override public List<WebElement> findElements(By by) { return null; }
@Override public WebElement findElement(By by) { return null; }
@Override public boolean isDisplayed() { return false; }
@Override public Point getLocation() { return null; }
@Override public Dimension getSize() { return null; }
@Override public Rectangle getRect() { return null; }
@Override public String getCssValue(String s) { return null; }
@Override public <X> X getScreenshotAs(OutputType<X> outputType) throws WebDriverException { return null; }
}
class CapabilitiesTestDouble implements Capabilities {
private final String browserName;
public CapabilitiesTestDouble(String browserName) {
this.browserName = browserName;
}
@Override public String getBrowserName() { return this.browserName; }
@Override public Platform getPlatform() { return null; }
@Override public String getVersion() { return null; }
@Override public boolean isJavascriptEnabled() { return false; }
@Override public Map<String, ?> asMap() { return null; }
@Override public Object getCapability(String s) { return null; }
@Override public boolean is(String s) { return false; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment