Permalink
Browse files

Activate JUnit 5 with Surefire/Failsafe.

Fix remaining leftovers from JUnit 4 to 5 migration.
  • Loading branch information...
1 parent 9982af6 commit fdcd20f07f42f1d47a3c7294c865048b83ca74eb @mp911de committed Dec 30, 2016
View
@@ -169,6 +169,18 @@
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
+ <dependencies>
+ <dependency>
+ <groupId>org.junit.platform</groupId>
+ <artifactId>junit-platform-surefire-provider</artifactId>
+ <version>1.0.0-M3</version>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-engine</artifactId>
+ <version>5.0.0-M3</version>
+ </dependency>
+ </dependencies>
</plugin>
<plugin>
@@ -593,6 +605,18 @@
</configuration>
</execution>
</executions>
+ <dependencies>
+ <dependency>
+ <groupId>org.junit.platform</groupId>
+ <artifactId>junit-platform-surefire-provider</artifactId>
+ <version>1.0.0-M3</version>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-engine</artifactId>
+ <version>5.0.0-M3</version>
+ </dependency>
+ </dependencies>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
@@ -1,6 +1,7 @@
package biz.paluch.logging.gelf.intern;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.reset;
@@ -42,8 +43,6 @@
@Mock
private GelfMessageAssembler assembler;
- private GelfSenderFactory sut = new GelfSenderFactory();
-
@BeforeEach
public void before() throws Exception {
GelfSenderFactory.addGelfSenderProvider(senderProvider);
@@ -64,15 +63,15 @@ public void testCreateSender() throws Exception {
mockSupports();
when(senderProvider.create(any(GelfSenderConfiguration.class))).thenReturn(sender);
- GelfSender result = sut.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
+ GelfSender result = GelfSenderFactory.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
assertThat(result).isSameAs(sender);
}
@Test
public void testCreateSenderFailUdp() throws Exception {
- GelfSender result = sut.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
+ GelfSender result = GelfSenderFactory.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
assertThat(result).isNull();
}
@@ -81,7 +80,7 @@ public void testCreateSenderFailTcp() throws Exception {
reset(assembler);
when(assembler.getHost()).thenReturn("tcp:" + THE_HOST);
- GelfSender result = sut.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
+ GelfSender result = GelfSenderFactory.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
assertThat(result).isNull();
}
@@ -91,7 +90,7 @@ public void testCreateSenderFailUnknownHostException() throws Exception {
mockSupports();
when(senderProvider.create(any(GelfSenderConfiguration.class))).thenThrow(new UnknownHostException());
- GelfSender result = sut.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
+ GelfSender result = GelfSenderFactory.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
assertThat(result).isNull();
verify(errorReporter).reportError(anyString(), any(UnknownHostException.class));
@@ -104,7 +103,7 @@ public void testCreateSenderFailSocketException() throws Exception {
mockSupports();
when(senderProvider.create(any(GelfSenderConfiguration.class))).thenThrow(new SocketException());
- GelfSender result = sut.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
+ GelfSender result = GelfSenderFactory.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
assertThat(result).isNull();
verify(errorReporter).reportError(anyString(), any(SocketException.class));
@@ -117,7 +116,7 @@ public void testCreateSenderFailIOException() throws Exception {
mockSupports();
when(senderProvider.create(any(GelfSenderConfiguration.class))).thenThrow(new IOException());
- GelfSender result = sut.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
+ GelfSender result = GelfSenderFactory.createSender(assembler, errorReporter, Collections.EMPTY_MAP);
assertThat(result).isNull();
verify(errorReporter).reportError(anyString(), any(IOException.class));
@@ -130,8 +129,12 @@ public void testCreateSenderFailNPE() throws Exception {
mockSupports();
when(senderProvider.create(any(GelfSenderConfiguration.class))).thenThrow(new NullPointerException());
- sut.createSender(assembler, errorReporter, new HashMap<String, Object>());
-
+ try {
+ GelfSenderFactory.createSender(assembler, errorReporter, new HashMap<String, Object>());
+ fail("Missing NullPointerException");
+ } catch (Exception e) {
+ assertThat(e).isInstanceOf(NullPointerException.class);
+ }
}
private void mockSupports() {
@@ -14,18 +14,21 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import biz.paluch.logging.gelf.intern.ErrorReporter;
import biz.paluch.logging.gelf.intern.GelfMessage;
import biz.paluch.logging.gelf.netty.NettyLocalHTTPServer;
+import external.MockitoExtension;
import io.netty.handler.codec.http.HttpResponseStatus;
/**
* @author Aleksandar Stojadinovic
*/
@SuppressWarnings("unchecked")
+@ExtendWith(MockitoExtension.class)
public class GelfHTTPSenderIntegrationTests {
private static final GelfMessage GELF_MESSAGE = new GelfMessage("shortMessage", "fullMessage", 12121L, "WARNING");
@@ -1,6 +1,7 @@
package biz.paluch.logging.gelf.intern.sender;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
@@ -10,6 +11,7 @@
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
+import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.util.Random;
@@ -79,8 +81,12 @@ public void connectionTimeoutShouldApply() throws Exception {
@Test
public void unknownHostShouldThrowException() throws Exception {
- new GelfTCPSender("unknown.host.unknown", 65534, 100, 100, errorReporter);
-
+ try {
+ new GelfTCPSender("unknown.host.unknown", 65534, 100, 100, errorReporter);
+ fail("Missing UnknownHostException");
+ } catch (UnknownHostException e) {
+ assertThat(e).isInstanceOf(UnknownHostException.class);
+ }
}
@Test
@@ -1,8 +1,8 @@
package biz.paluch.logging.gelf.jul;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assume.assumeTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.util.List;
import java.util.Map;
@@ -4,12 +4,12 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.AppenderLoggingException;
-import org.apache.logging.log4j.junit.InitialLoggerContext;
+import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.test.appender.ListAppender;
-import org.junit.ClassRule;
-import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
@@ -21,12 +21,17 @@
public static final String LOG_MESSAGE = "foo bar test log message";
public static final String EXPECTED_LOG_MESSAGE = LOG_MESSAGE;
- @ClassRule
- public static final InitialLoggerContext loggerContext = new InitialLoggerContext("log4j2/log4j2-propagate-exceptions.xml");
+ private static LoggerContext loggerContext;
- @BeforeEach
- public void before() throws Exception {
- ThreadContext.clearAll();
+ @BeforeAll
+ public static void beforeAll() throws Exception {
+ loggerContext = Configurator.initialize("GelfLogAppenderPropagateExceptionsTests",
+ GelfLogAppenderPropagateExceptionsTests.class.getClassLoader(), "log4j2/log4j2-propagate-exceptions.xml");
+ }
+
+ @AfterAll
+ public static void afterAll() throws Exception {
+ Configurator.shutdown(loggerContext);
}
@Test
@@ -48,7 +53,7 @@ public void shouldUseFailoverAppender() throws Exception {
Logger logger = loggerContext.getLogger("biz.failover");
logger.info(LOG_MESSAGE);
- ListAppender failoverList = loggerContext.getListAppender("failoverList");
+ ListAppender failoverList = getListAppender("failoverList");
assertThat(failoverList.getEvents()).hasSize(1);
}
@@ -58,8 +63,12 @@ public void shouldIgnoreException() throws Exception {
Logger logger = loggerContext.getLogger("biz.ignore");
logger.info(LOG_MESSAGE);
- ListAppender ignoreList = loggerContext.getListAppender("ignoreList");
+ ListAppender ignoreList = getListAppender("ignoreList");
assertThat(ignoreList.getEvents()).hasSize(1);
}
+ public ListAppender getListAppender(String name) {
+ return (ListAppender) loggerContext.getConfiguration().getAppenders().get(name);
+ }
+
}

0 comments on commit fdcd20f

Please sign in to comment.