简体   繁体   English

测试实现接口的类

[英]Testing a class that implements an interface

I have been set an assignment for my java course which involves creating a basic phone directory. 我已经为我的java课程设置了一个任务,包括创建一个基本的电话目录。 I have come to the part which involves testing all of the methods inside my ArrayPhoneDirectory class, a class which implements an interface named PhoneDirectory. 我来到了涉及测试ArrayPhoneDirectory类中所有方法的部分,这个类实现了一个名为PhoneDirectory的接口。

I am stuck on how to instantiate the ArrayPhoneDirectory class within a separate tester class, as every attempt I have made has been returned with the error that it cannot be instantiated due to it being an abstract class. 我坚持如何在一个单独的测试器类中实例化ArrayPhoneDirectory类,因为我所做的每一次尝试都返回了错误,因为它是一个抽象类而无法实例化。

PhoneDirectory interface code: PhoneDirectory接口代码:

public interface PhoneDirectory {

/**
 * Load file containing directory entries
 *
 * @param sourceName is name of the file containing the directory entries
 */
void loadData(String sourceName);

/**
 * Look up an entry.
 *
 * @param name The name of person to look up
 * @return The telno or null if name is not in the directory
 */
String lookUpEntry(String name);

**CODE SNIPPED**

ArrayPhoneDirectory code: ArrayPhoneDirectory代码:

public abstract class ArrayPhoneDirectory implements PhoneDirectory {

private static final int INIT_CAPACITY = 100;
private int capacity = INIT_CAPACITY;

//holds telno of directory entries
private int size = 0;

//Array to contain directory entries
private DirectoryEntry[] theDirectory = new DirectoryEntry[capacity];

//Holds name of data file to be read
private String sourceName = null;

**CODE SNIPPED**

Any help with instantiating the ArrayPhoneDirectory class would be much appreciated, please comment if I need to add more of my code, i'm still fairly new to this! 任何有关实例化ArrayPhoneDirectory类的帮助都会非常感激,如果我需要添加更多代码请注释,我还是比较新的!

Inside your test create a local class that extends your Abstract class. 在测试中创建一个扩展Abstract类的本地类。 You will need to implement any abstract methods. 您将需要实现任何抽象方法。 If it is not important what logic is inside the abstract methods than you can leave them blank. 如果抽象方法中的逻辑不是很重要,那么你可以将它们留空。

Make ArrayPhoneDirectory not abstract. 使ArrayPhoneDirectory 不是抽象的。 You will need to implement the interface methods. 您需要实现接口方法。

I suggest first you read about abstract classes, to me it looks like ArrayPhoneDirectory should not be abstract: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html 我建议你首先阅读抽象类,对我来说,看起来ArrayPhoneDirectory不应该是抽象的: httpArrayPhoneDirectory

Regarding your real question: I suggest you use a mocking framework in addition to JUnit instead of implementing interfaces / abstract classes. 关于你真正的问题:我建议你使用除JUnit之外的模拟框架,而不是实现接口/抽象类。 I myself use mockito , a hint on how to test abstract class can be found on StackOverflow . 我自己使用mockito ,可以在StackOverflow上找到关于如何测试抽象类的提示。 Your test would look as easy as this: 您的测试看起来就像这样简单:

import static org.mockito.Mockito.*;

PhoneDirectory directory = mock(ArrayPhoneDirectory.class, CALLS_REAL_METHODS);
directory.someImplementedMethod();

With a mocking framework you can define actions to calls of anything involved in your test, which is not actually your class under test. 使用模拟框架,您可以定义操作以调用测试中涉及的任何内容,这实际上不是您正在测试的类。 This way you can really test a small unit and do not end up doing a whole functional test. 这样你就可以真正测试一个小单元而不是最终完成一个完整的功能测试。 Also with a mock you can test classes depending on objects not easily available in your test environment. 使用模拟,您可以根据测试环境中不易获得的对象测试类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM