http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-test-autoconfigure/

어찌 저찌 성공은 한듯.......해서...

기념으로다 블로그에 포스팅....ㅎ

package kr.pe.fountain.web.index;


import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.reactive.server.WebTestClient.BodySpec;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {IndexRouter.class, IndexHandler.class, IndexService.class})
@WebFluxTest
public class IndexTest {

//	private static final Logger logger = LoggerFactory.getLogger(IndexTest.class);

	@Autowired
    private WebTestClient webClient;
	
	@Test
	public void testWithParameter() {
		BodySpec<String, ?> bodyStr = webClient.get().uri("/{name}", "KS").accept(MediaType.TEXT_HTML)
                             .exchange()
                             .expectStatus().isOk()
                             .expectBody(String.class);
		
		assertTrue(bodyStr.returnResult().getResponseBody().contains("KS"));
	}
	
	@Test
	public void testWithoutParameter() {
		BodySpec<String, ?> bodyStr = webClient.get().uri("/").accept(MediaType.TEXT_HTML)
                             .exchange()
                             .expectStatus().isOk()
                             .expectBody(String.class);

		assertTrue(bodyStr.returnResult().getResponseBody().contains("World"));
	}

}
package kr.pe.fountain.web.index;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

@Configuration
public class IndexRouter {
    @Bean
    public RouterFunction<ServerResponse> route(IndexHandler indexHandler) {

        return RouterFunctions.route(RequestPredicates.GET("/"), indexHandler::hello)
        		.andRoute(RequestPredicates.GET("/{name}"), indexHandler::hello);

    }
}
package kr.pe.fountain.web.index;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import reactor.core.publisher.Mono;

@Component
public class IndexHandler {

//	private static final Logger logger = LoggerFactory.getLogger(IndexHandler.class);
	
	@Autowired IndexService indexService;

	Mono<ServerResponse> hello(ServerRequest request) {
		String retMsg = request.pathVariables().containsKey("name")==false?null:request.pathVariable("name");
		String response = indexService.hello(retMsg);

		Map<String, Object> renderAttributes = new HashMap<>();
		renderAttributes.put("name", response);

		return ServerResponse.ok().contentType(MediaType.TEXT_HTML).render("index", renderAttributes);
	}
}
package kr.pe.fountain.web.index;

import org.springframework.stereotype.Component;

@Component
public class IndexService {

//	private static final Logger logger = LoggerFactory.getLogger(IndexService.class);
	
	public String hello(String paramStr) {
		String returnStr;
		if( paramStr==null ) returnStr = "World!";
		else returnStr = paramStr;
		return "Hello, " + returnStr;
	}

}

항상 환경 세팅만 해 놓은 spring boot에 최근부터 reactive 어쩌고 하면서, 새로운 뭔가가 추가되었길래,

(https://spring.io/)


그거로 다시 환경 세팅하고,


기본적인 코드를 junit 테스트 돌려 보려고 했으나...


계속 실패하였다...


아마도..


https://stackoverflow.com/questions/48842652/using-webfluxtest-to-test-routerfunction/48843944#48843944


이 것과 관련한 문제인 듯한데...


여기나오는 식으로 해도 안되다가...


어제 밤...


@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {테스트 하고자하는 라우터 및 핸들러 클래스})
@WebFluxTest


이렇게만 하고 다시 해 보니......

성공...


근데.....

왜 성공한건진....아직 잘 모르겠다...;;



하여간...


티스토리에 한번 써볼라고...짧게나마 정리 해 봄..;;


통계에서 보니 거의 메일 유입이 있는걸로 나오네.....

모지??

몰까??



이러면서 글 한번 깨작 ㅋ

휴면상태에서 복귀!!^^;;
티스토리에도 드뎌입성~ㅋ

이번엔 좀 쓰긴해야하는데~ㅋ

+ Recent posts