DATAREST-1567 - Polishing.

Tweaked test setups to work without the explicit component scanning.

Original pull request: #382.
This commit is contained in:
Oliver Drotbohm
2020-09-25 09:18:50 +02:00
parent 8ff438678d
commit 25cfb6ecb9
7 changed files with 71 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 original author or authors.
* Copyright 2016-2020 original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.tests.AbstractWebIntegrationTests;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.data.rest.webmvc.RepositoryRestController;
@@ -46,8 +47,24 @@ import org.springframework.web.bind.annotation.RequestMethod;
@ContextConfiguration
public class CorsIntegrationTests extends AbstractWebIntegrationTests {
@Configuration
static class CorsConfig extends JpaRepositoryConfig {
@Bean
AuthorsPdfController authorsPdfController() {
return new AuthorsPdfController();
}
@Bean
BooksPdfController booksPdfController() {
return new BooksPdfController();
}
@Bean
BooksXmlController booksXmlController() {
return new BooksXmlController();
}
@Bean
RepositoryRestConfigurer repositoryRestConfigurer() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 original author or authors.
* Copyright 2016-2020 original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,16 +23,15 @@ import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.tests.AbstractWebIntegrationTests;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
@@ -41,8 +40,6 @@ import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkRelation;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -52,18 +49,22 @@ import org.springframework.web.bind.annotation.ResponseBody;
*
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = JpaDefaultPageableWebTests.Config.class)
@ContextConfiguration
public class JpaDefaultPageableWebTests extends AbstractWebIntegrationTests {
@Configuration
@Import({ RepositoryRestMvcConfiguration.class, JpaRepositoryConfig.class })
@EnableJpaRepositories(considerNestedRepositories = true)
static class Config implements RepositoryRestConfigurer {
static class Config {
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setDefaultPageSize(1);
@Bean
MyRestController myRestController() {
return new MyRestController();
}
@Bean
RepositoryRestConfigurer repositoryRestConfigurer() {
return RepositoryRestConfigurer.withConfig(config -> config.setDefaultPageSize(1));
}
}

View File

@@ -17,25 +17,30 @@ package org.springframework.data.rest.webmvc.jpa;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
import static org.springframework.data.rest.webmvc.util.TestUtils.*;
import static org.springframework.http.HttpHeaders.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import net.minidev.json.JSONArray;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import net.minidev.json.JSONArray;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.tests.CommonWebTests;
import org.springframework.data.rest.webmvc.jpa.JpaRepositoryConfig.BooksHtmlController;
import org.springframework.data.rest.webmvc.jpa.JpaRepositoryConfig.OrdersJsonController;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkRelation;
@@ -63,7 +68,7 @@ import com.jayway.jsonpath.JsonPath;
* @author Ľubomír Varga
*/
@Transactional
@ContextConfiguration(classes = JpaRepositoryConfig.class)
@ContextConfiguration(classes = JpaRepositoryConfig.class, initializers = JpaWebTests.JpaContextInitializer.class)
public class JpaWebTests extends CommonWebTests {
private static final MediaType TEXT_URI_LIST = MediaType.valueOf("text/uri-list");
@@ -75,6 +80,17 @@ public class JpaWebTests extends CommonWebTests {
ObjectMapper mapper = new ObjectMapper();
static class JpaContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
@Override
public void initialize(GenericApplicationContext ctx) {
ctx.registerBean(AuthorsController.class);
ctx.registerBean(BooksHtmlController.class);
ctx.registerBean(OrdersJsonController.class);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.AbstractWebIntegrationTests#setUp()