Skip to content

Commit

Permalink
Use AssertJ instead of Junit asserts (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
brenuart authored Oct 8, 2021
1 parent 24fd074 commit 43e9955
Show file tree
Hide file tree
Showing 23 changed files with 271 additions and 277 deletions.
180 changes: 98 additions & 82 deletions src/test/java/net/logstash/logback/ConfigurationTest.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
package net.logstash.logback.appender;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import net.logstash.logback.CachingAbbreviator;

import ch.qos.logback.classic.pattern.Abbreviator;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand All @@ -40,14 +40,14 @@ public void test() {

CachingAbbreviator abbreviator = new CachingAbbreviator(delegate);

Assertions.assertEquals("abbreviation", abbreviator.abbreviate("full"));
Assertions.assertEquals("abbreviation", abbreviator.abbreviate("full"));
assertThat(abbreviator.abbreviate("full")).isEqualTo("abbreviation");
assertThat(abbreviator.abbreviate("full")).isEqualTo("abbreviation");

verify(delegate, times(1)).abbreviate("full");

abbreviator.clear();

Assertions.assertEquals("abbreviation", abbreviator.abbreviate("full"));
assertThat(abbreviator.abbreviate("full")).isEqualTo("abbreviation");

verify(delegate, times(2)).abbreviate("full");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
import ch.qos.logback.core.status.StatusManager;
import ch.qos.logback.core.util.Duration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -507,7 +506,7 @@ public void testKeepAlive() throws Exception {

// Wait for a bit more than 2 keep alive messages then make sure we got the expected content
Thread.sleep(250);
Assertions.assertArrayEquals(expectedKeepAlivesBytes, bos.toByteArray());
assertThat(bos.toByteArray()).containsExactly(expectedKeepAlivesBytes);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ public void testCreateLiteTimeoutBlocking_unparsableParam() {
});
}

@SuppressWarnings("unchecked")
private <T> T getFieldValue(Object object, Class<?> clazz, String fieldName) {
try {
Field field = clazz.getDeclaredField(fieldName);
Expand All @@ -277,5 +278,4 @@ private <T> T getFieldValue(Object object, Class<?> clazz, String fieldName) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public class FailureSummaryLoggingAppenderListenerTest {

@Mock
private Appender appender;
private Appender<ILoggingEvent> appender;

@Mock
private Socket socket;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void setMyField(String myField) {
@Test
public void testWriteTo() throws IOException {

@SuppressWarnings("unchecked")
Supplier<MyClass> supplier = mock(Supplier.class);

when(supplier.get()).thenReturn(new MyClass("value"));
Expand All @@ -71,7 +72,7 @@ public void testWriteTo() throws IOException {

verify(supplier).get();

assertThat(writer.toString()).isEqualTo("{\"myObject\":{\"myField\":\"value\"}}");
assertThat(writer).hasToString("{\"myObject\":{\"myField\":\"value\"}}");

// execute again, to ensure that supplier is not invoked again
generator.writeStartObject();
Expand All @@ -81,7 +82,6 @@ public void testWriteTo() throws IOException {

verify(supplier).get();

assertThat(writer.toString()).isEqualTo("{\"myObject\":{\"myField\":\"value\"}} {\"myObject\":{\"myField\":\"value\"}}");

assertThat(writer).hasToString("{\"myObject\":{\"myField\":\"value\"}} {\"myObject\":{\"myField\":\"value\"}}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@
package net.logstash.logback.argument;


import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.util.stream.Stream;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.helpers.MessageFormatter;

public class StructuredArgumentsToStringTest {

private static class BuguyToString {
private static class BuguyToString {
@Override
@SuppressWarnings("serial")
public String toString() {
throw new NullPointerException("npe") {
@Override
Expand All @@ -53,8 +54,9 @@ private static Stream<Arguments> data() {
@ParameterizedTest
@MethodSource("data")
public void testToString(String expected, Object arg) throws Exception {
Assertions.assertEquals(expected, StructuredArguments.toString(arg));
Assertions.assertEquals(MessageFormatter.format("{}", arg).getMessage(), StructuredArguments.toString(arg));
assertThat(StructuredArguments.toString(arg))
.isEqualTo(expected)
.isEqualTo(MessageFormatter.format("{}", arg).getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package net.logstash.logback.composite.accessevent;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class IncludeExcludeHeaderFilterTest {
Expand All @@ -26,27 +26,27 @@ public class IncludeExcludeHeaderFilterTest {

@Test
public void testEmpty() {
Assertions.assertTrue(filter.includeHeader("HeaderA", "1"));
Assertions.assertTrue(filter.includeHeader("Headera", "2"));
Assertions.assertTrue(filter.includeHeader("HeaderB", "3"));
Assertions.assertTrue(filter.includeHeader("Headerb", "4"));
assertThat(filter.includeHeader("HeaderA", "1")).isTrue();
assertThat(filter.includeHeader("Headera", "2")).isTrue();
assertThat(filter.includeHeader("HeaderB", "3")).isTrue();
assertThat(filter.includeHeader("Headerb", "4")).isTrue();
}
@Test
public void testIncludeOnly() {
filter.addInclude("headera");
Assertions.assertTrue(filter.includeHeader("HeaderA", "1"));
Assertions.assertTrue(filter.includeHeader("Headera", "2"));
Assertions.assertFalse(filter.includeHeader("HeaderB", "3"));
Assertions.assertFalse(filter.includeHeader("Headerb", "3"));
assertThat(filter.includeHeader("HeaderA", "1")).isTrue();
assertThat(filter.includeHeader("Headera", "2")).isTrue();
assertThat(filter.includeHeader("HeaderB", "3")).isFalse();
assertThat(filter.includeHeader("Headerb", "3")).isFalse();
}

@Test
public void testExcludeOnly() {
filter.addExclude("headera");
Assertions.assertFalse(filter.includeHeader("HeaderA", "1"));
Assertions.assertFalse(filter.includeHeader("Headera", "2"));
Assertions.assertTrue(filter.includeHeader("HeaderB", "3"));
Assertions.assertTrue(filter.includeHeader("Headerb", "4"));
assertThat(filter.includeHeader("HeaderA", "1")).isFalse();
assertThat(filter.includeHeader("Headera", "2")).isFalse();
assertThat(filter.includeHeader("HeaderB", "3")).isTrue();
assertThat(filter.includeHeader("Headerb", "4")).isTrue();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class ArgumentsJsonProviderTest {
private static class Foo {
private String k6 = "v6";

@SuppressWarnings("unused")
public String getK6() {
return k6;
}
Expand Down Expand Up @@ -89,7 +90,7 @@ public void testUnwrapped() throws IOException {

generator.flush();

assertThat(writer.toString()).isEqualTo(
assertThat(writer).hasToString(
"{"
+ "\"k0\":\"v0\","
+ "\"k1\":\"v1\","
Expand All @@ -112,7 +113,7 @@ public void testWrapped() throws IOException {

generator.flush();

assertThat(writer.toString()).isEqualTo(
assertThat(writer).hasToString(
"{"
+ "\"args\":{"
+ "\"k0\":\"v0\","
Expand All @@ -138,7 +139,7 @@ public void testIncludeNonStructuredArguments() throws IOException {

generator.flush();

assertThat(writer.toString()).isEqualTo(
assertThat(writer).hasToString(
"{"
+ "\"k0\":\"v0\","
+ "\"k1\":\"v1\","
Expand All @@ -163,7 +164,7 @@ public void testIncludeNonStructuredArgumentsAndCustomPrefix() throws IOExceptio

generator.flush();

assertThat(writer.toString()).isEqualTo(
assertThat(writer).hasToString(
"{"
+ "\"k0\":\"v0\","
+ "\"k1\":\"v1\","
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public void testNoCallerData() throws IOException {
provider.writeTo(generator, event);

verifyNoMoreInteractions(generator);

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
package net.logstash.logback.composite.loggingevent;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -80,7 +79,7 @@ public void testFieldNames() throws IOException {

@Test
public void testMessageSplitDisabledByDefault() throws Exception {
assertNull(provider.getMessageSplitRegex());
assertThat(provider.getMessageSplitRegex()).isNull();

mockEventMessage("###");
provider.writeTo(generator, event);
Expand All @@ -91,7 +90,7 @@ public void testMessageSplitDisabledByDefault() throws Exception {
@Test
public void testMessageSplitDisabledForNullRegex() throws Exception {
provider.setMessageSplitRegex(null);
assertNull(provider.getMessageSplitRegex());
assertThat(provider.getMessageSplitRegex()).isNull();

mockEventMessage("###");
provider.writeTo(generator, event);
Expand All @@ -102,7 +101,7 @@ public void testMessageSplitDisabledForNullRegex() throws Exception {
@Test
public void testMessageSplitDisabledForEmptyRegex() throws Exception {
provider.setMessageSplitRegex("");
assertNull(provider.getMessageSplitRegex());
assertThat(provider.getMessageSplitRegex()).isNull();

mockEventMessage("###");
provider.writeTo(generator, event);
Expand All @@ -113,7 +112,7 @@ public void testMessageSplitDisabledForEmptyRegex() throws Exception {
@Test
public void testMessageSplitWithSystemSeparator() throws IOException {
provider.setMessageSplitRegex("SYSTEM");
assertEquals(System.lineSeparator(), provider.getMessageSplitRegex());
assertThat(provider.getMessageSplitRegex()).isEqualTo(System.lineSeparator());

mockEventMessage(System.lineSeparator());
provider.writeTo(generator, event);
Expand All @@ -124,7 +123,7 @@ public void testMessageSplitWithSystemSeparator() throws IOException {
@Test
public void testMessageSplitWithUnixSeparator() throws IOException {
provider.setMessageSplitRegex("UNIX");
assertEquals("\n", provider.getMessageSplitRegex());
assertThat(provider.getMessageSplitRegex()).isEqualTo("\n");

mockEventMessage("\n");
provider.writeTo(generator, event);
Expand All @@ -135,7 +134,7 @@ public void testMessageSplitWithUnixSeparator() throws IOException {
@Test
public void testMessageSplitWithWindowsSeparator() throws IOException {
provider.setMessageSplitRegex("WINDOWS");
assertEquals("\r\n", provider.getMessageSplitRegex());
assertThat(provider.getMessageSplitRegex()).isEqualTo("\r\n");

mockEventMessage("\r\n");
provider.writeTo(generator, event);
Expand All @@ -146,7 +145,7 @@ public void testMessageSplitWithWindowsSeparator() throws IOException {
@Test
public void testMessageSplitWithCustomRegex() throws IOException {
provider.setMessageSplitRegex("#+");
assertEquals("#+", provider.getMessageSplitRegex());
assertThat(provider.getMessageSplitRegex()).isEqualTo("#+");

mockEventMessage("###");
provider.writeTo(generator, event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package net.logstash.logback.composite.loggingevent;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -25,7 +26,6 @@
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.ThrowableProxy;
import com.fasterxml.jackson.core.JsonGenerator;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -58,7 +58,8 @@ public void testDefaultName() throws IOException {
// THEN
ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class);
verify(generator).writeStringField(eq(StackHashJsonProvider.FIELD_NAME), hashCaptor.capture());
Assertions.assertTrue(HEX_PATTERN.matcher(hashCaptor.getValue()).matches(), "Did not produce an hexadecimal integer: " + hashCaptor.getValue());

assertThat(hashCaptor.getValue()).matches(HEX_PATTERN);
}

@Test
Expand All @@ -72,6 +73,7 @@ public void testFieldName() throws IOException {
// THEN
ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class);
verify(generator).writeStringField(eq("newFieldName"), hashCaptor.capture());
Assertions.assertTrue(HEX_PATTERN.matcher(hashCaptor.getValue()).matches(), "Did not produce an hexadecimal integer: " + hashCaptor.getValue());

assertThat(hashCaptor.getValue()).matches(HEX_PATTERN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void basicEscape() throws Exception {
generator.writeEndObject();
generator.flush();

assertThat(writer.toString()).isEqualTo("{\"message\":\"M!===message_Мо?===сообщение\"}");
assertThat(writer).hasToString("{\"message\":\"M!===message_Мо?===сообщение\"}");
}

@Test
Expand All @@ -64,7 +64,7 @@ public void noEscapeSequence() throws Exception {
generator.writeEndObject();
generator.flush();

assertThat(writer.toString()).isEqualTo("{\"message\":\"..===.y.\"}");
assertThat(writer).hasToString("{\"message\":\"..===.y.\"}");
}

@Test
Expand All @@ -81,7 +81,7 @@ public void noStandard() throws Exception {
generator.writeEndObject();
generator.flush();

assertThat(writer.toString()).isEqualTo("{\"message\":\"foo\nbar\"}");
assertThat(writer).hasToString("{\"message\":\"foo\nbar\"}");
}

}
Loading

0 comments on commit 43e9955

Please sign in to comment.