From 651fe2c0fb6c6f4f8378f17db0600cc87b0657e8 Mon Sep 17 00:00:00 2001 From: Mariusz Kuligowski Date: Sun, 8 Apr 2018 19:24:50 +0200 Subject: [PATCH] Add unit tests for CarEngineFacade --- .../pattern/facade/CarEngineFacadeTest.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 patterns/facade/src/test/java/com/baeldung/pattern/facade/CarEngineFacadeTest.java diff --git a/patterns/facade/src/test/java/com/baeldung/pattern/facade/CarEngineFacadeTest.java b/patterns/facade/src/test/java/com/baeldung/pattern/facade/CarEngineFacadeTest.java new file mode 100644 index 0000000000..dce5ce7df3 --- /dev/null +++ b/patterns/facade/src/test/java/com/baeldung/pattern/facade/CarEngineFacadeTest.java @@ -0,0 +1,85 @@ +package com.baeldung.pattern.facade; + +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.AppenderBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; + +public class CarEngineFacadeTest { + + + private InMemoryCustomTestAppender appender; + + + @Before + public void setUp() { + appender = new InMemoryCustomTestAppender(); + } + + @After + public void tearDown() { + appender.stop(); + } + + + @Test + public void testStartEngine() { + CarEngineFacade carEngineFacade = new CarEngineFacade(); + carEngineFacade.startEngine(); + assertTrue(appender.logContains("Fuel injector ready to inject fuel.")); + assertTrue(appender.logContains("Getting air measurements...")); + assertTrue(appender.logContains("Air provided!")); + assertTrue(appender.logContains("Fuel injector ready to inject fuel.")); + assertTrue(appender.logContains("Fuel Pump is pumping fuel...")); + assertTrue(appender.logContains("Fuel injected.")); + assertTrue(appender.logContains("Starting...")); + assertTrue(appender.logContains("Setting temperature upper limit to 90")); + assertTrue(appender.logContains("Cooling Controller ready!")); + assertTrue(appender.logContains("Setting radiator speed to 10")); + assertTrue(appender.logContains("Catalytic Converter switched on!")); + } + + + @Test + public void testStopEngine() { + CarEngineFacade carEngineFacade = new CarEngineFacade(); + carEngineFacade.stopEngine(); + assertTrue(appender.logContains("Stopping Fuel injector...")); + assertTrue(appender.logContains("Catalytic Converter switched off!")); + assertTrue(appender.logContains("Scheduled cooling with maximum allowed temperature 50")); + assertTrue(appender.logContains("Getting temperature from the sensor...")); + assertTrue(appender.logContains("Radiator switched on!")); + assertTrue(appender.logContains("Stopping Cooling Controller...")); + assertTrue(appender.logContains("Radiator switched off!")); + assertTrue(appender.logContains("Air controller switched off.")); + } + + private class InMemoryCustomTestAppender extends AppenderBase { + + private List logs = new ArrayList<>(); + + public InMemoryCustomTestAppender() { + ((Logger) LoggerFactory.getLogger("root")).addAppender(this); + start(); + } + + @Override + protected void append(ILoggingEvent eventObject) { + logs.add(eventObject); + } + + public boolean logContains(String message) { + return logs.stream().anyMatch(event -> event.getFormattedMessage().equals(message)); + } + } + + +} \ No newline at end of file