Skip to content

Commit

Permalink
refactored the testing codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Jun 29, 2019
1 parent 3cac4e3 commit e68ac42
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
25 changes: 11 additions & 14 deletions vanilla/src/main/java/com/example/demo/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.*;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import reactor.netty.DisposableServer;
import reactor.netty.http.server.HttpServer;

import java.time.Duration;

@Configuration
@ComponentScan
@PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true)
Expand All @@ -25,18 +20,20 @@ public class Application {

public static void main(String[] args) throws Exception {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
Application.class)) {
context.getBean(DisposableServer.class).onDispose().block();
Application.class)) {
context.getBean(HttpServer.class)
.bindUntilJavaShutdown(Duration.ofSeconds(60), null);
}
}

@Profile("default")
@Bean
public DisposableServer nettyHttpServer(ApplicationContext context) {
public HttpServer httpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host("localhost").port(this.port);
return httpServer.handle(adapter).bindNow();
return HttpServer.create()
.host("localhost")
.port(this.port)
.handle(adapter);
}

}
36 changes: 26 additions & 10 deletions vanilla/src/test/java/com/example/demo/IntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,59 @@
package com.example.demo;

import java.time.Duration;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.netty.DisposableServer;
import reactor.netty.http.server.HttpServer;

/**
*
* @author hantsy
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Application.class)
public class IntegrationTests {

@Value("#{@nettyHttpServer.port()}")
@Value("${server.port:8080}")
int port;

WebTestClient rest;

@Autowired
HttpServer httpServer;

private DisposableServer disposableServer;

@Before
public void setup() {
this.disposableServer = this.httpServer.bindNow();
this.rest = WebTestClient
.bindToServer()
.responseTimeout(Duration.ofDays(1))
.baseUrl("http://localhost:" + this.port)
.build();
.bindToServer()
.responseTimeout(Duration.ofDays(1))
.baseUrl("http://localhost:" + this.port)
.build();
}

@After
public void teardown() {
this.disposableServer.dispose();
}

@Test
public void getAllPostsWillBeOk() throws Exception {
this.rest
.get()
.uri("/posts")
.exchange()
.expectStatus().isOk();
.get()
.uri("/posts")
.exchange()
.expectStatus()
.isOk();
}

}

0 comments on commit e68ac42

Please sign in to comment.