refactored @SpringBootConfiguration examples

This commit is contained in:
Tom Hombergs
2019-03-01 11:52:52 +01:00
parent 71fad7e002
commit d01cccb574
15 changed files with 153 additions and 80 deletions

View File

@@ -6,18 +6,13 @@ import io.reflectoring.customer.CustomerConfiguration;
import io.reflectoring.customer.data.CustomerRepository;
import io.reflectoring.flight.FlightConfiguration;
import io.reflectoring.flight.data.FlightService;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootConfiguration
@Configuration
@Import({CustomerConfiguration.class, FlightConfiguration.class})
@EnableAutoConfiguration
@ComponentScan
public class BookingConfiguration {

View File

@@ -1,14 +1,17 @@
package io.reflectoring.customer;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import io.reflectoring.customer.business.CustomerService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootConfiguration
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class CustomerConfiguration {
@Bean
public CustomerService customerService(){
return new CustomerService();
}
}

View File

@@ -0,0 +1,4 @@
package io.reflectoring.customer.business;
public class CustomerService {
}

View File

@@ -0,0 +1,14 @@
package io.reflectoring.customer.web;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomerController {
@PostMapping("/customer")
public String hello() {
return "hello";
}
}

View File

@@ -6,11 +6,9 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootConfiguration
@EnableAutoConfiguration
@Configuration
@EnableScheduling
@ComponentScan
public class FlightConfiguration {

View File

@@ -0,0 +1,10 @@
package io.reflectoring.booking;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@SpringBootConfiguration
@EnableAutoConfiguration
class BookingTestConfiguration extends BookingConfiguration {
}

View File

@@ -4,14 +4,12 @@ import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@SpringBootTest(classes = BookingConfiguration.class)
class BookingConfigurationTest {
@SpringBootTest(classes = BookingTestConfiguration.class)
class BookingTestConfigurationTest {
@Autowired
private ApplicationContext applicationContext;

View File

@@ -1,15 +0,0 @@
package io.reflectoring.customer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@SpringBootTest(classes = CustomerConfiguration.class)
class CustomerConfigurationTest {
@Test
void bookingConfigurationLoads() {
}
}

View File

@@ -0,0 +1,42 @@
package io.reflectoring.customer;
import io.reflectoring.booking.business.BookingService;
import io.reflectoring.booking.data.BookingRepository;
import io.reflectoring.booking.web.BookingController;
import io.reflectoring.customer.business.CustomerService;
import io.reflectoring.customer.data.CustomerRepository;
import io.reflectoring.customer.web.CustomerController;
import io.reflectoring.flight.data.FlightService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
class CustomerModuleTest {
@Autowired(required = false)
private BookingController bookingController;
@Autowired(required = false)
private BookingService bookingService;
@Autowired(required = false)
private BookingRepository bookingRepository;
@Autowired
private CustomerController customerController;
@Autowired
private CustomerService customerService;
@Autowired
private CustomerRepository customerRepository;
@Test
void onlyCustomerModuleIsLoaded() {
assertThat(customerController).isNotNull();
assertThat(customerService).isNotNull();
assertThat(customerRepository).isNotNull();
assertThat(bookingController).isNull();
assertThat(bookingService).isNull();
assertThat(bookingRepository).isNull();
}
}

View File

@@ -0,0 +1,13 @@
package io.reflectoring.customer;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootConfiguration
@EnableAutoConfiguration
class CustomerTestConfiguration extends CustomerConfiguration {
}

View File

@@ -0,0 +1,24 @@
package io.reflectoring.customer.data;
import io.reflectoring.booking.data.BookingRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import static org.assertj.core.api.Assertions.*;
@DataJpaTest
class CustomerModuleDataLayerTests {
@Autowired
private CustomerRepository customerRepository;
@Autowired(required = false)
private BookingRepository bookingRepository;
@Test
void onlyCustomerRepositoryIsLoaded() {
assertThat(customerRepository).isNotNull();
assertThat(bookingRepository).isNull();
}
}

View File

@@ -1,43 +0,0 @@
package io.reflectoring.customer.data;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
class CustomerRepositoryTest {
@Autowired
private CustomerRepository repository;
@Autowired
private ApplicationContext applicationContext;
@BeforeEach
void printApplicationContext() {
Arrays.stream(applicationContext.getBeanDefinitionNames())
.map(name -> applicationContext.getBean(name).getClass().getName())
.sorted()
.forEach(System.out::println);
}
@Test
void findsByName() {
Customer customer = Customer.builder()
.name("Hurley")
.build();
repository.save(customer);
List<Customer> foundCustomers = repository.findByName("Hurley");
assertThat(foundCustomers).hasSize(1);
}
}

View File

@@ -0,0 +1,24 @@
package io.reflectoring.customer.web;
import io.reflectoring.booking.web.BookingController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import static org.assertj.core.api.Assertions.*;
@WebMvcTest
class CustomerModuleWebLayerTests {
@Autowired
private CustomerController customerController;
@Autowired(required = false)
private BookingController bookingController;
@Test
void onlyCustomerControllerIsLoaded() {
assertThat(customerController).isNotNull();
assertThat(bookingController).isNull();
}
}

View File

@@ -1,4 +0,0 @@
package io.reflectoring.flight;
public class FlightMockConfiguration {
}

View File

@@ -0,0 +1,10 @@
package io.reflectoring.flight;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@SpringBootConfiguration
@EnableAutoConfiguration
class FlightTestConfiguration extends FlightConfiguration {
}