use of org.junit.jupiter.api.Test in project java-design-patterns by iluwatar.
the class UserCreatedEventTest method testGetEventType.
/**
* This unit test should correctly return the {@link AbstractEvent} class type when calling the
* {@link AbstractEvent#getType() getType} method.
*/
@Test
public void testGetEventType() {
User user = new User("iluwatar");
UserCreatedEvent userCreatedEvent = new UserCreatedEvent(user);
assertEquals(UserCreatedEvent.class, userCreatedEvent.getType());
}
use of org.junit.jupiter.api.Test in project java-design-patterns by iluwatar.
the class EventDispatcherTest method testEventDriverPattern.
/**
* This unit test should register events and event handlers correctly with the event dispatcher
* and events should be dispatched accordingly.
*/
@Test
public void testEventDriverPattern() {
EventDispatcher dispatcher = spy(new EventDispatcher());
UserCreatedEventHandler userCreatedEventHandler = spy(new UserCreatedEventHandler());
UserUpdatedEventHandler userUpdatedEventHandler = spy(new UserUpdatedEventHandler());
dispatcher.registerHandler(UserCreatedEvent.class, userCreatedEventHandler);
dispatcher.registerHandler(UserUpdatedEvent.class, userUpdatedEventHandler);
User user = new User("iluwatar");
UserCreatedEvent userCreatedEvent = new UserCreatedEvent(user);
UserUpdatedEvent userUpdatedEvent = new UserUpdatedEvent(user);
// fire a userCreatedEvent and verify that userCreatedEventHandler has been invoked.
dispatcher.dispatch(userCreatedEvent);
verify(userCreatedEventHandler).onEvent(userCreatedEvent);
verify(dispatcher).dispatch(userCreatedEvent);
// fire a userCreatedEvent and verify that userUpdatedEventHandler has been invoked.
dispatcher.dispatch(userUpdatedEvent);
verify(userUpdatedEventHandler).onEvent(userUpdatedEvent);
verify(dispatcher).dispatch(userUpdatedEvent);
}
use of org.junit.jupiter.api.Test in project java-design-patterns by iluwatar.
the class SimpleFileWriterTest method testActualWrite.
/**
* Test if the data written to the file writer actually gets in the file
*/
@Test
public void testActualWrite() throws Exception {
final String testMessage = "Test message";
final File temporaryFile = this.testFolder.newFile();
assertTrue(temporaryFile.exists());
new SimpleFileWriter(temporaryFile.getPath(), writer -> writer.write(testMessage));
assertTrue(Files.lines(temporaryFile.toPath()).allMatch(testMessage::equals));
}
use of org.junit.jupiter.api.Test in project java-design-patterns by iluwatar.
the class SimpleFileWriterTest method testIoException.
/**
* Verify if an {@link IOException} during the write ripples through
*/
@Test
public void testIoException() throws Exception {
assertThrows(IOException.class, () -> {
final File temporaryFile = this.testFolder.newFile();
new SimpleFileWriter(temporaryFile.getPath(), writer -> {
throw new IOException("");
});
});
}
use of org.junit.jupiter.api.Test in project java-design-patterns by iluwatar.
the class SimpleFileWriterTest method testWriterNotNull.
/**
* Verify if the given writer is not 'null'
*/
@Test
public void testWriterNotNull() throws Exception {
final File temporaryFile = this.testFolder.newFile();
new SimpleFileWriter(temporaryFile.getPath(), Assertions::assertNotNull);
}
Aggregations