0

I'm programming application and i need to Mock File to test it. My code below:

@Test
public void testPostMail() throws Exception
{
    Emailer instance = new Emailer();
    instance.setRecipientsFromFile(new File("list.txt"));
}

The problem is, I don't want to be dependent on file on my hdd (of course I can make a file with a proper content and after a test delete it, but I want to do it with EasyMock).

I tried to import org.easymock.classextension.EasyMock and use it, but:

1) I still get error "File is not an interface"

2) classextension.EasyMock is deprecated, so I should use just EasyMock

My not working EasyMock code:

@Test
public void testSetReceipientsFromFile() throws Exception
{
File file = EasyMock.createMock(File.class);
FileReader in = EasyMock.createMock(FileReader.class);
BufferedReader br = EasyMock.createMock(BufferedReader.class);

EasyMock.expect(new FileReader(file)).andReturn(in);
EasyMock.expect(new BufferedReader(in)).andReturn(br);

EasyMock.expect(br.readLine()).andReturn("test@mail.com");
EasyMock.expect(br.readLine()).andReturn("test2@mail2.com");

EasyMock.replay(file, in, br);

EasyMock.verify(file, in, br);

// ...
}

EDIT: I'm wondering to change from EasyMock to Mockito, because I heard more favourable opinions.

user1227115
  • 843
  • 1
  • 8
  • 11

5 Answers5

1

Both EasyMock and Mockito don't support mocking construction of objects. Therefore you need a library that supports it such as PowerMock or PowerMockito.

However, I think there is a design flaw here. Is it a responsibility of Emailer to read a file? I don't think so.

I'd extract code for reading file from Emailer into new class responsible for reading list of recipients from file. It would make Emailer more testable. And if you want to test that new class, it would be a natural choice to create a real file (using TemporaryFolder rule), since reading files is the main responsibility of that class.

axtavt
  • 239,438
  • 41
  • 511
  • 482
1

In Mockito you do it like this:

    BufferedReader br = Mockito.mock(BufferedReader.class);

    Mockito.when(br.readLine())
            .thenReturn("test@mail.com")
            .thenReturn("test2@mail2.com");

    // Alternative
    // Mockito.when(br.readLine()).thenReturn("aaa", "bbb");

    Assert.assertEquals("test@mail.com", br.readLine());
    Assert.assertEquals("test2@mail.com", br.readLine());
Vadim Ponomarev
  • 1,346
  • 9
  • 15
0

Which version of EasyMock are you using. In the latest version 3.1, yes the extension EasyMock is deprecated. You can use the normal easy mock instead, that's what they are suggesting in their API documentation http://www.easymock.org/api/easymockclassextension/3.1/index.html.

raddykrish
  • 1,866
  • 13
  • 15
0

"Listen to your tests". You should create a wrapper around File and use it in Emailer.

yegor256
  • 102,010
  • 123
  • 446
  • 597
0

As of EasyMock 3.0 (the current version is 3.2), you can mock concrete classes so you would not get this error now.

Zoltán
  • 21,321
  • 14
  • 93
  • 134