0
votes

I am trying to mock a generic method but getting class cast exception, no matter what I try. Class under testing is

public class TClass {
    private byte[] data;
    public <T extends SInterface> T asObject(T item){
        item.SMethod(this.getData());
        return item;
    }

    private byte[] getData() {
        return data;
    }
}

while the testing class is

@RunWith(MockitoJUnitRunner.class)
public class TClassTest {
    @Mock
    SSInterface sClass;
    @Mock
    SObject sObject;

    TClass tClass = new TClass();


    @org.junit.Before
    public void setUp() throws Exception {
    }

    @org.junit.Test
    public void asObject() {
        Mockito.when(tClass.asObject(new SSClass())).thenReturn(sClass);
    }
}

Other helper classes and interface are:

 public interface SInterface {
    public void SMethod(byte[] in);
}



public interface SSInterface extends SInterface {
}


public class SSClass implements SSInterface {
    @Override
    public void SMethod(byte[] in) {
    }
}

public class SObject {
}

The error I am getting is :

Cannot resolve method thenReturn(generic.SSInterface)

if I use Mockito.when(tClass.asObject(new SSClass())).thenReturn(new SSClass());

I get org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because: 1. you stub either of: final/private/equals()/hashCode() methods. Those methods cannot be stubbed/verified. Mocking methods declared on non-public parent classes is not supported. 2. inside when() you don't call method on mock but on some other object.

at generic.TClassTest.asObject(TClassTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:78)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:84)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
2

2 Answers

2
votes

You have incompatible types and it has nothing to do with mockito. asObject() returns the same type as it receives and it has to be of SInterface type.

Depending on what you trying to achive ou can either return the instance of SSClasss:

Mockito.when(tClass.asObject(new SSClass())).thenReturn( new SSClass());

or you can make TClass extend SInterface and then:

Mockito.when(tClass.asObject(tClass)).thenReturn(tClass);

0
votes

I think that you want to test TClass.asObject() but verifying sClass.SMethod() is called.

@org.junit.Test
public void asObject() {
        final SSInterface sut = new SSClass();

        final SSInterface returned = sut.asObject(sClass);

        assertEquals(sClass, returned);
        Mockito.verify(sClass).SMethod(any());    
}