====== Testing ====== ===== Mockito ===== ==== Questions answered ==== === How to implement strict mock? === [[http://benjiweber.co.uk/blog/2013/12/06/strictmocks-in-mockito/|Strict mocks in Mockito]] could be implemented using special class that is switched to "verification" state, but it's not clear how combine it with ordered mocks. Additionally subscribe to [[github>mockito/mockito/labels/strictness|Support of strict mocks]] and check for more appropriate approach. import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import org.mockito.exceptions.verification.NoInteractionsWanted; import org.mockito.internal.util.MockUtil; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; public static class StrictMockAnswer implements Answer { private boolean strict = false; @Override public Object answer(InvocationOnMock invocation) throws Throwable { if (strict) { throw new NoInteractionsWanted(invocation.getMethod().getName()); } return null; } public void setStrict() { strict = true; } } public static void verifyNoUnstubbedInteractions(Object... mocks) { for (Object mock : mocks) { ((StrictMockAnswer) MockUtil.getMockHandler(mock).getMockSettings().getDefaultAnswer()).setStrict(); } } public void testGetImageAnnotationsNoData() throws IOException { final String userId = "user123"; Service1 service1 = mock(Service1.class, new StrictMockAnswer()); Service2 service2 = mock(Service2.class, new StrictMockAnswer()); MyController testee = new MyController(service1, service2); when(service1.createUser(userId, "Full Name")).thenReturn(new User(userId, ...)); doNothing().when(service2.activateUser(eq(userId), isA(PrivilegedUserGroup.class))); // void method call verifyNoUnstubbedInteractions(service1, service2); assertTrue(testee.createPrivilegedUser(userId)); } ===== Selenium ===== ==== Questions answered ==== === [[stackoverflow>14461827|Very slow character input on IE10 ×64 driver]] === As solution, use ×32 driver version (×32 version of IE is always present). === How to enable logging from JavaScript to Unit test console? === From JavaScript part one need to use ''window.dump()'' function. The description of ''[[https://developer.mozilla.org/en-US/docs/Web/API/window.dump|window.dump(message)]]'' says, that on Linux the parent console is used for the output, and on Windows ''-console'' is only necessary when process is not started with redirected output, so we don't need this option. Another obstacle is ''window.dump()'' takes only one argument and does not add newline at the end of message. The solution is to use helper wrapper like the following: function dumpToConsole() { if ($.isFunction(dump)) { logOutput = ""; // Stringify all passed arguments: for (var i = 0; i < arguments.length; i++) { var obj = arguments[i]; if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean') { logOutput += obj; } else { logOutput += JSON.stringify(obj); } } logOutput += "\n"; dump(logOutput); } } Firefox also needs to have the following profile option set to ''true'' (by means of Java/Selenium or hardcoding the existing profile) so that ''window.dump()'' has any effect: FirefoxProfile firefoxProfile = getDefaultProfile(); firefoxProfile.setPreference("browser.dom.window.dump.enabled", true); And finally the output of Firefox driver needs to be redirected to file or, in our case, to ''stdout'': mvn -Dwebdriver.firefox.logfile=/dev/stdout ... Firefox driver will take care of redirecting the Firefox output to the same file. The special value ''/dev/stdout'' is the same for both Linux and Windows. All above instructions work equally on Windows and Linux. References: * [[http://forums.mozillazine.org/viewtopic.php?f=38&t=2091941|Logging console error messages to a file]]