Caused by: java.lang.IllegalStateException: The driver is not executable: chromedriver + Selenium

Given below permission to chromedriver and it works.

chmod 777 chromedriver

As previous answer there is no relevance to the .exe part in stack trace

Mockito - calling real method

e.g.

class C {
 void m1() { ...}
 boolean m2() { ... return flag;}
}   

unit test code:

C cMock = Mockito.mock(C.class);
Mockito.doNothing().when(cMock).m1();
Mockito.when(cMock.m2()).thenCallRealMethod();
The strange thing is that m2 is not being called.


Solution


This is also where Mockito.spy can be used. it allows you to do partial mocks on real objects.

C cMock = Mockito.spy(new C());
Mockito.doNothing().when(cMock).m1();

Mockito and constructor + field injection Or Mocking a constructor of @Autowired service(system under test)

As mentioned in a previous post, I’m using Mockito extensively. It’s a superb framework that helps writing unit test a lot.

Recently, I came across a problem when trying to write a test for a legacy Spring class, that used injection in both constructor and field. In Guice, the class would like like this:

package io.mhlg.assisteddemo.service;

import com.google.inject.assistedinject.Assisted;
import io.mhlg.assisteddemo.domain.Money;

import javax.inject.Inject;

class InternetPayment implements Payment {
    @Inject
    private InternetDataSender internetDataSender;
    @Inject
    private InternetDataPreparer internetDataPreparer;

    private final Money money;

    @Inject
    public InternetPayment(@Assisted Money money) {
        this.money = money;
    }

    @Override
    public String sendThePayment() {
        String theMessage = internetDataPreparer.prepareMessage(money);

        return internetDataSender.sendTheDataViaInternet(theMessage);
    }

}
This isn’t an often case, but I can imagine a situation when that would be used.

Trying to test such a class with Mockito can be tricky. It turns out that when using @InjectMocks annotation, Mockito injects mocks only to the constructor, leaving the fields undefined. In order to test it properly, one needs to initialize the class with the constructor parameter and Mockito by hand:

package io.mhlg.assisteddemo.service;

import static org.assertj.core.api.Assertions.assertThat;

import io.mhlg.assisteddemo.domain.Money;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.when;

public class InternetPaymentTest {

    @InjectMocks
    private InternetPayment testObj;

    @Mock
    private InternetDataPreparer internetDataPreparer;

    @Mock
    private InternetDataSender internetDataSender;
    private Money someMoney;

    @Before
    public void setUp() throws Exception {
        someMoney = Money.createMoney(100, "USD"); // you can use mock(Money.class) here instead
        testObj = new InternetPayment(someMoney);
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testSendThePayment() throws Exception {
        // given
        final String somePreparedMessage = "some message";
        when(internetDataPreparer.prepareMessage(someMoney)).thenReturn(somePreparedMessage);

        final String someReturningMessage = "return message";
        when(internetDataSender.sendTheDataViaInternet(somePreparedMessage)).thenReturn(someReturningMessage);

        // when
        final String result = testObj.sendThePayment();

        // then
        assertThat(result).isEqualTo(someReturningMessage);
    }
}

Credits: https://mhaligowski.github.io/blog/2014/05/30/mockito-with-both-constructor-and-field-injection.html

How can I mock methods of @InjectMocks class?

Main Class

@Component
public class MyHandler {

  @AutoWired
  private MyDependency myDependency;

  public int someMethod() {
    ...
    return anotherMethod();
  }

  public int anotherMethod() {...}
}

Test Class

@RunWith(MockitoJUnitRunner.class}
class MyHandlerTest {

  @InjectMocks
  private MyHandler myHandler;

  @Mock
  private MyDependency myDependency;

  @Test
  public void testSomeMethod() {
    when(myHandler.anotherMethod()).thenReturn(1);
    assertEquals(myHandler.someMethod() == 1);
  }
}

Solution:

First of all the reason for mocking MyHandler methods can be the following: we already test anotherMethod() and it has complex logic, so why do we need to test it again (like a part of someMethod()) if we can just verify that it's calling?
We can do it through:

@RunWith(MockitoJUnitRunner.class}
class MyHandlerTest {

  @Spy 
  @InjectMocks 
  private MyHandler myHandler; 

  @Mock 
  private MyDependency myDependency; 

  @Test 
  public void testSomeMethod() { 
    doReturn(1).when(myHandler).anotherMethod(); 
    assertEquals(myHandler.someMethod() == 1); 
    verify(myHandler, times(1)).anotherMethod(); 
  } 


Note: in case of 'spying' object we need to use doReturn instead of thenReturn(little explanation is here)

Quickly Show/Hide Hidden Files on macOS Sierra, OS X El Capitan & Yosemite

The Quickest Way to Show/Hide Hidden Files

Since the release of macOS Sierra, when in Finder, it is now possible to use the shortcut:
 CMD + SHIFT + .
Press once to show hidden files and again to hide them. If you’re using a version earlier than macOS Sierra, see Show/Hide Hidden Files using Terminal Aliases to setup a toggle command via terminal.

Show/Hide Hidden Files the Long Way

The long way to show hidden Mac OS X files is as follows:
  1. Open Terminal found in Finder > Applications > Utilities
  2. In Terminal, paste the following: defaults write com.apple.finder AppleShowAllFiles YES
  3. Press return
  4. Hold the ‘Option/alt’ key, then right click on the Finder icon in the dock and click Relaunch.
Relaunch Finder by right clicking the Finder Icon whilst holding the `option/alt` key
Relaunch Finder by right clicking the Finder Icon whilst holding the ‘Option/alt’ key
This will show all hidden files. To hide them again, follow the same steps but replace the Terminal command with:
defaults write com.apple.finder AppleShowAllFiles NO
It’s not the longest set of instructions or the biggest command to commit to memory but if you’re doing this a lot, it’s worth spending a few minutes now to save yourself a lot more time in the future.

Mockito: Stubbing Methods That Return Type With Bounded Wild-Cards


// Use Mockito.<ResponseView<?>>when(), instead of when()

@Test
public void testMockitoWithGenerics()
{
    DummyClass dummyClass = Mockito.mock(DummyClass.class);
    List<? extends Number> someList = new ArrayList<Integer>();

    Mockito.doReturn(someList).when(dummyClass).dummyMethod();

    Assert.assertEquals(someList, dummyClass.dummyMethod());
}

How to get / return the same passed argument / parameter from mock method

when(relatedProviderAddressAlternateIdentificationRepo.save(anyList())).thenAnswer(new Answer() {
            public Object answer(InvocationOnMock invocation) {
                return invocation.getArguments()[0];
            }
        });


Or

when(relatedProviderAddressAlternateIdentificationRepo.save(any(Class.class))).thenAnswer(new Answer() {
            public Object answer(InvocationOnMock invocation) {
                return invocation.getArguments()[0];
            }
        });


Class.class could be any custom class, eg: User.class