Typ-smocka med Mockito

Jag stötte på en riktig fuling idag när jag skulle skriva ett test likt det nedan. Hittade “lösningen” på mailinglistan efter att ha läst ett par buggar/feature requests.

Som jag förstår det går det inte att lösa pga Javas Type Inference i metodanrop. I exemplet nedan kan raden med when aldrig fungera utan man får gå på doReturn istället.

package demo;

import static java.util.Arrays.asList;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Collection;

import org.junit.Test;

public class GenericCollectionStubTest {

    @Test
    public void onGoingStubFails() {
        Car bmw = mock(Car.class);
        Reseller reseller = mock(Reseller.class);
        Collection cars = asList(bmw);

        //when(reseller.getModels()).thenReturn(cars);

        doReturn(cars).when(reseller).getModels();
    }
}

Raden med when misslyckas med följande meddelande:

The method thenReturn(Collection<capture#1-of ? extends Vehicle>) in the type OngoingStubbing<Collection<capture#1-of ? extends Vehicle>> is not applicable for the arguments (Collection<capture#2-of ? extends Vehicle>)

PayPal Spam

Just got a really well formatted, articulated phishing/scam mail. It was so good that I actually looked up whois information for one of the domains (they even bothered to use https). Anyhow, comparing the whois-data for paypal.com with paypalobjects.com shows this:

fredrik@a-machine:~$ diff ppo pp
8c8,14
<    Domain Name: PAYPALOBJECTS.COM
---
>    Server Name: PAYPAL.COM.ZZZZZ.GET.LAID.AT.WWW.SWINGINGCOMMUNITY.COM
>    IP Address: 69.41.185.207
>    Registrar: TUCOWS.COM CO.
>    Whois Server: whois.tucows.com
>    Referral URL: http://domainhelp.opensrs.net
>
>    Domain Name: PAYPAL.COM
[...]

So, I won’t open the attached form that requires a “fairly modern browser to work”. :-)