I am creating mock to check logger functionality. For that i have interface and create mock for that. But its not working. Any help appreciated.
This is my interface
public interface ILoggerMock {
public void write(String text) throws Throwable;
}
And this is my Test class
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
public class LoggerMock {
@Test
public void out() throws Throwable {
ILoggerMock iLoggerMock = Mockito.mock(ILoggerMock.class);
Mockito.doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocationOnMock) throws Throwable, IOException {
try {
System.out.println("CalculatorMock.out().new Answer() {...}.answer()");
Files.write(Paths.get("D:/log/logger.txt"), "Hello!".getBytes(), StandardOpenOption.CREATE);
} catch (IOException e) {
// exception handling
}
return null;
}
}).when(iLoggerMock).write("write");
List<String> list = Files.readAllLines(Paths.get("D:/log/logger.txt"));
for (String line : list) {
System.out.println(line);
}
//System.out.print("hello");
assertEquals("Hello!", list.get(0));
}
}
Problem is in mock i am not able to create file. Please help me for creating file.