如何对一个使用尚未实现的接口作为方法参数的类执行JUnit测试?

huangapple go评论73阅读模式
英文:

How to perform JUnit testing on a class that uses interfaces not yet implemented as method parameters?

问题

我无法弄清楚如何在一个使用尚未实现为方法参数的接口的类上执行 Junit 测试。我不知道接口的具体实现,因此无法进行实现。

例如,我有这个接口:

public interface Command {
    public void execute();
    // ...
}

和一个将该接口用作方法参数的类:

public class Manager {
    List<Command> listCommands = new List<Command>();

    public void storeCommand(Command command){
        listCommands.add(command);
    }
}

我在网上阅读到的一个解决方案是使用 Mockito 来模拟接口的实现,但我不太确定。

谢谢!

英文:

I can't figure out how to perform Junit testing on a class that uses an interface not yet implemented as method parameters. I don't know the implementation of the interface so i can't implement it.

For example I have this interface

public interface Command{
     public void execute();
     ....
} 

and class that uses the interface as method parameters.

public class Manager{
    List&lt;Command&gt; listCommands = new List&lt;Command&gt;();

    public void storeCommand(Command command){
       listCommands.add(command);
    }
}

I read online one solution could be to use mockito to mock the implementation of the interfaces but i'm not sure.

Thanks!

答案1

得分: 0

是的,您可以使用Mockito模拟您的接口实现,就像这样:

Command mockedCommand = Mockito.mock(Command.class);

然后,您可以开始模拟您在测试中将要使用的接口的每个方法,并指定它们应该返回什么。

英文:

Yes, you can mock an implementation of your interface using mockito like this :

Command mockedCommand = Mockito.mock(Command.class)

Then you can start mocking every method of your interface that will be used in your tests specifying what it shall return.

答案2

得分: 0

首先,首先要熟悉单元测试环境中的模拟概念,因为通过提出这个问题,你展示了你并不熟悉。

然后选择一些要使用的模拟框架Mockito 是一个很好的选择,也可能是最流行的选择之一。

最后,我不会(也不能)教你如何使用这个框架的所有模拟功能,但是这是一个关于如何模拟你的类型(无论是接口还是类)以及如何配置模拟实例使其按照你的预期行为进行的示例:

import static org.mockito.Mockito.*;

class YourTestFixture {

    @Test
    public void yourTest()  {
        //创建模拟
        Command commandMock = mock(Command.class);

        //配置execute()方法的返回值
        when(commandMock.execute()).thenReturn("你的预期值");

        //你的断言..

    }
}

现在,每次你在该特定实例(commandMock)上调用.execute()时,它将始终返回"你的预期值"

请注意,你可以让它抛出异常,或者返回null...或者执行任何你希望它执行的操作,但返回的对象/值必须是方法声明要返回的类型(或子类型)。

另请注意,在你的示例中:

public class Manager{
    List<Command> listCommands = new List<Command>();

    public void storeCommand(Command command) {
        listCommands.add(command);
    }
}

你甚至没有在command参数上进行任何方法调用。因此,你可以按照上述说明简单地对接口进行模拟。

英文:

First of all, get yourself familiar with mocking concept in the Unit Testing context, as by asking this question, you demonstrate, that you are not.

Then choose some mocking Framework to stick with. Mockito is a good, and probably the most popular one.

Finally, I will not (can not) teach you all the mocking features you can leverage with this framework, but this is one example of how you mock your type (be it an interface or class), and how you configure mocked instance to make it behave like you want it to behave:

import static org.mockito.Mockito.*;

class YourTestFixture {

    @Test
    public void yourTest()  {
        //create mock
        Command commandMock = mock(Command.class);

        //configure execute() method&#39;s return value
        when(commandMock.execute()).thenReturn(&quot;your expected value&quot;);

        //your assertions..

    }
}

Now, every time you invoke .execute() on that particular instance (commandMock), it will consistently be returning &quot;your expected value&quot;.

Note, that you can make it throw an exception, or return null.. or just do anything you would like it to do, but the returning object/value must be of a type (or subtype) of whatever method declares to be returning.

Note, that in your example:

public class Manager{
    List&lt;Command&gt; listCommands = new List&lt;Command&gt;();

    public void storeCommand(Command command) {
        listCommands.add(command);
    }
}

you are not even using any method invocation on the command parameter. So, you can simply mock your interface as explained above.

huangapple
  • 本文由 发表于 2020年10月5日 14:23:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64203369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定