如何使用Mockito模拟通用方法?

时间:2018-11-06 19:45:15

标签: java junit mockito

无论我尝试什么,我都在尝试模拟通用方法,但是却得到类强制转换异常。正在测试的课程是

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;
    }
}

测试班

@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);
    }
}

其他帮助类界面是:

 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 {
}

我得到的错误是:

  

无法解析方法,然后返回(generic.SSInterface)

如果我使用Mockito.when(tClass.asObject(new SSClass())).thenReturn(new SSClass());

我明白了 org.mockito.exceptions.misusing.MissingMethodInvocationException: when()需要一个参数,该参数必须是“模拟的方法调用”。 例如:     when(mock.getArticles())。thenReturn(articles);

此外,由于以下原因,可能会出现此错误: 1.存根:final / private / equals()/ hashCode()方法之一。    那些方法 无法存根/验证。    不支持在非公共父类上声明的模拟方法。 2.在when()内部,您不会在模拟对象上调用方法,而是在其他某个对象上调用。

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 个答案:

答案 0 :(得分:2)

您有不兼容的类型,它与Mockito无关。 asObject()返回与接收的相同类型,并且必须为SInterface类型。

根据您尝试达到的目标,可以返回SSClasss的实例:

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

或者您可以将TClass扩展SInterface,然后:

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

答案 1 :(得分:0)

我认为您想测试TClass.asObject(),但调用sClass.SMethod()进行验证。

@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());    
}