Skip to content

Commit

Permalink
mocked unit test to fulfill coverage (zalando#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Drews committed Jul 14, 2023
1 parent b173dfb commit 3b9622d
Showing 1 changed file with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.zalando.logbook.spring.webflux;

import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -11,8 +12,14 @@
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import org.springframework.web.server.handler.DefaultWebFilterChain;
import org.zalando.logbook.Correlation;
import org.zalando.logbook.HttpLogWriter;
import org.zalando.logbook.Logbook;
Expand All @@ -23,10 +30,12 @@
import org.zalando.logbook.test.TestStrategy;

import java.io.IOException;
import reactor.core.publisher.Mono;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
Expand All @@ -42,9 +51,9 @@ static class FilterConfiguration {
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public Logbook logbook(HttpLogWriter writer) {
return Logbook.builder()
.strategy(new TestStrategy())
.sink(new DefaultSink(new DefaultHttpLogFormatter(), writer))
.build();
.strategy(new TestStrategy())
.sink(new DefaultSink(new DefaultHttpLogFormatter(), writer))
.build();
}

@Bean
Expand Down Expand Up @@ -278,6 +287,26 @@ void shouldLogRequestWithoutBody() throws IOException {
}
}

@Nested
class WithMockedObjects {
@Test
void shouldNotCallWriteOnInappropriateStage() {
final Logbook logbook = mock(Logbook.class);
final LogbookWebFilter underTest = new LogbookWebFilter(logbook);

final WebFilterChain chain = new DefaultWebFilterChain(
filteredExchange ->
Mono.fromCallable(() -> filteredExchange.getResponse().setStatusCode(HttpStatus.OK))
.then(filteredExchange.getResponse().setComplete()),
Collections.singletonList(underTest));
final ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/test").build());

chain.filter(exchange).block();

assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.OK);
}
}

private void sendAndReceive(final WebClient client) {
sendAndReceive(client, "/echo");
}
Expand Down

0 comments on commit 3b9622d

Please sign in to comment.