From d1e6b95ff8bf70d61f780a1dde57856b834f82a2 Mon Sep 17 00:00:00 2001 From: Elvis de Freitas Date: Tue, 6 Aug 2024 22:38:46 -0300 Subject: [PATCH] testing --- .../dnsproxyserver/server/Starter.java | 10 +++- .../dnsproxyserver/server/StarterTest.java | 47 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/mageddo/dnsproxyserver/server/StarterTest.java diff --git a/src/main/java/com/mageddo/dnsproxyserver/server/Starter.java b/src/main/java/com/mageddo/dnsproxyserver/server/Starter.java index fd7ace968..356a4569e 100644 --- a/src/main/java/com/mageddo/dnsproxyserver/server/Starter.java +++ b/src/main/java/com/mageddo/dnsproxyserver/server/Starter.java @@ -30,11 +30,19 @@ public void start() { this.startupEvents.forEach(StartupEvent::onStart); } if (shouldStartDnsServer()) { - this.dnsServerStarter.start(); + this.startDnsServer(); } + this.startWebServer(); + } + + void startWebServer() { this.webServer.start(Configs.getInstance().getWebServerPort()); } + void startDnsServer() { + this.dnsServerStarter.start(); + } + private static boolean shouldStartDnsServer() { return !isTest() || isMustStartFlagActive(); } diff --git a/src/test/java/com/mageddo/dnsproxyserver/server/StarterTest.java b/src/test/java/com/mageddo/dnsproxyserver/server/StarterTest.java new file mode 100644 index 000000000..c5b0b41d4 --- /dev/null +++ b/src/test/java/com/mageddo/dnsproxyserver/server/StarterTest.java @@ -0,0 +1,47 @@ +package com.mageddo.dnsproxyserver.server; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Set; + +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +@ExtendWith(MockitoExtension.class) +class StarterTest { + + final Starter starter = spy(new Starter(null, null, Set.of())); + + @Test + void wontStartInTestMode() { + + doNothing().when(this.starter).startWebServer(); + doNothing().when(this.starter).startDnsServer(); + + this.starter.start(); + + verify(this.starter, never()).startDnsServer(); + verify(this.starter).startWebServer(); + } + + + @Test + void mustStartWhenInTestModeAndFlagForced() { + + Starter.setMustStartFlagActive(true); + + doNothing().when(this.starter).startWebServer(); + doNothing().when(this.starter).startDnsServer(); + + this.starter.start(); + + verify(this.starter).startDnsServer(); + verify(this.starter).startWebServer(); + + Starter.setMustStartFlagActive(false); + } +}