0

I have been set an assignment for my java course which involves creating a basic phone directory. 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.

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.

PhoneDirectory interface code:

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:

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!

Joe Perkins
  • 261
  • 3
  • 9
  • 17

3 Answers3

2

Inside your test create a local class that extends your Abstract class. 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.

wxkevin
  • 1,634
  • 2
  • 25
  • 44
0

Make ArrayPhoneDirectory not abstract. You will need to implement the interface methods.

Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94
  • When i remove abstract i get this error: ArrayPhoneDirectory is not abstract and does not override abstract method format() in PhoneDirectory – Joe Perkins Apr 09 '14 at 18:18
  • That's what I meant when I said you need to implement the interface methods. Take a look at [this](http://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html). – Giovanni Botta Apr 09 '14 at 18:28
0

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

Regarding your real question: I suggest you use a mocking framework in addition to JUnit instead of implementing interfaces / abstract classes. I myself use mockito, a hint on how to test abstract class can be found on 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.

Community
  • 1
  • 1
nbred
  • 130
  • 7