我该如何测试界面? [英] How can i test an interface?

查看:140
本文介绍了我该如何测试界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为接口做一个JUnit测试用例,因为你知道我不能从接口创建一个对象,我不希望在该测试中显示任何类名,我只想测试接口,只在测试中使用它的方法。

I want to do a JUnit test case for an interface, as you know i can't make an object from the interface and i don't want any classes names to show up in that test, i want only to test the interface and only use it's methods in the test.

所以,我无法实例化像这样的对象:

So, i can't instantiate an object like :

Interface obj = new Class(); 

因为我不会使用任何类方法,我不想实例化interface as:

as i don't wont to use any classes methods, and i don't want to instantiate the interface as :

Interface var = new Interface{//methods};

因为我不想覆盖测试中的方法。

as i don't want to override the methods in the test.

更新:

i have an interface and a class which implements it:
public interface inter {
public void method1();
public void method2();
}


public class BlaBla implements inter{
@override
public void method1(){//stuff}
@override
public void method2(){//stuff}
}

我想要测试BlaBla,但用它的接口(inter)处理我的测试。

i want to test BlaBla but deal in my test with it's interface (inter).

推荐答案

一个接口是一份合同。没有逻辑可以测试。

An interface is a contract. It has no logic to test.

您可以使用像这样的模拟框架 Mockito 创建接口的实例,而无需手动存根方法。但它只会在后台存根。

You can use a mocking framework like Mockito to create an instance of the interface without having to manually stub the methods. But it will just stub them in the background.

你必须问自己你要测试什么?鉴于接口方法有没有实现,没有代码来测试

You have to ask yourself what you want to test? Given that the interface methods have no implementation there is no code to test.

假设我有一个界面就像这样

public interface DoesStuff {
    void doStuff();
}

有什么可以测试的?我唯一要说的是我想要一个的类实现DoesStuff doStuff

What is there to test? The only thing I have said is that I want a class that implements DoesStuff to doStuff.

这篇关于我该如何测试界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆