Skip to content

Commit

Permalink
Fix wrapping of maps with null values. Fixes #3380
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Aug 21, 2017
1 parent 10229a9 commit 91eb004
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,10 @@ private Object wrapResult(Object result) {
return ((List) result).stream().map(this::wrapResult).collect(Collectors.toList());
}
if (result instanceof Map) {
return ((Map<String, Object>) result).entrySet().stream().collect(Collectors.toMap(
e -> e.getKey(), e -> wrapResult(e.getValue())
));
return ((Map<String, Object>) result).entrySet().stream().collect(
HashMap::new,
(m, e) -> m.put(e.getKey(), e.getValue()),

This comment has been minimized.

Copy link
@khatchad

khatchad Aug 20, 2019

Shouldn't this be (m, e) -> m.put(e.getKey(), wrapResult(e.getValue())),?

Map::putAll);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -52,7 +53,9 @@
import org.openqa.selenium.internal.WrapsElement;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Michael Tamm
Expand Down Expand Up @@ -318,6 +321,22 @@ public void shouldWrapMultipleElementsFoundWhenCallingScripts() {
assertSame(stubbedElement2, ((WrapsElement) resList.get(1)).getWrappedElement());
}

@Test
public void shouldWrapMapsWithNullValues() {
Map<String, Object> map = new HashMap<>();
map.put("a", null);
final WebDriver mockedDriver = mock(WebDriver.class,
withSettings().extraInterfaces(JavascriptExecutor.class));
when(((JavascriptExecutor) mockedDriver).executeScript("foo")).thenReturn(map);

EventFiringWebDriver testedDriver = new EventFiringWebDriver(mockedDriver);

Object res = testedDriver.executeScript("foo");
verify((JavascriptExecutor) mockedDriver).executeScript("foo");
assertThat(res, instanceOf(Map.class));
assertThat(((Map<String, Object>) res).get("a"), is(nullValue()));
}

@Test
public void testShouldUnpackMapOfElementArgsWhenCallingScripts() {
final WebDriver mockedDriver = mock(WebDriver.class,
Expand Down

0 comments on commit 91eb004

Please sign in to comment.