refactored @SpringBootConfiguration examples
This commit is contained in:
@@ -6,18 +6,13 @@ import io.reflectoring.customer.CustomerConfiguration;
|
|||||||
import io.reflectoring.customer.data.CustomerRepository;
|
import io.reflectoring.customer.data.CustomerRepository;
|
||||||
import io.reflectoring.flight.FlightConfiguration;
|
import io.reflectoring.flight.FlightConfiguration;
|
||||||
import io.reflectoring.flight.data.FlightService;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Import;
|
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})
|
@Import({CustomerConfiguration.class, FlightConfiguration.class})
|
||||||
@EnableAutoConfiguration
|
|
||||||
@ComponentScan
|
@ComponentScan
|
||||||
public class BookingConfiguration {
|
public class BookingConfiguration {
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
package io.reflectoring.customer;
|
package io.reflectoring.customer;
|
||||||
|
|
||||||
import org.springframework.boot.SpringBootConfiguration;
|
import io.reflectoring.customer.business.CustomerService;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
|
||||||
|
|
||||||
@SpringBootConfiguration
|
@Configuration
|
||||||
@EnableAutoConfiguration
|
|
||||||
@ComponentScan
|
@ComponentScan
|
||||||
public class CustomerConfiguration {
|
public class CustomerConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public CustomerService customerService(){
|
||||||
|
return new CustomerService();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package io.reflectoring.customer.business;
|
||||||
|
|
||||||
|
public class CustomerService {
|
||||||
|
}
|
||||||
@@ -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";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,11 +6,9 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
@SpringBootConfiguration
|
@Configuration
|
||||||
@EnableAutoConfiguration
|
|
||||||
@EnableScheduling
|
@EnableScheduling
|
||||||
@ComponentScan
|
@ComponentScan
|
||||||
public class FlightConfiguration {
|
public class FlightConfiguration {
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,14 +4,12 @@ import java.util.Arrays;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
|
||||||
|
|
||||||
@SpringBootTest(classes = BookingConfiguration.class)
|
@SpringBootTest(classes = BookingTestConfiguration.class)
|
||||||
class BookingConfigurationTest {
|
class BookingTestConfigurationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext applicationContext;
|
private ApplicationContext applicationContext;
|
||||||
@@ -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() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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 {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package io.reflectoring.flight;
|
|
||||||
|
|
||||||
public class FlightMockConfiguration {
|
|
||||||
}
|
|
||||||
@@ -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 {
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user