
JavaEE workshop #10
Viktor Martiš
(Spring-boot, integration tests)
Previous workshop
- IDEA shortcut: multicursor - Alt + J
- Observer pattern
- REST - RestTemplate
- Web services
- SOAP, WSDL
- Spring-ws, soup-ui
Agenda
- IDEA shortcuts: Ctrl + shift + backspace, Ctrl + e
- Chain of responsibility design pattern
- Spring boot
- overview
- starters
- spring configuration
- externalized configuration
- logging
- testing
- production ready
- Lombok
- Integration tests
- @SpringBootTest
- in memory database
- comes under behavioral patterns
- chain of receiver objects having the responsibility, depending on run-time conditions, to either handle a request or forward it to the next receiver on the chain
- Real implementation: servlet filters
Spring boot overview
- for easy to create stand-alone, production-grade Spring-based Applications
- no code generation and non XML configuration
- gradle / maven support
- java / kotlin / groovy
- easy to start with intializr
- executable jar vs. war
- spring boot CLI
- supported of many cloud platform (heroku, cloundfoundry, aws, azure, ...)
- ready to production features
- set of convenient dependency descriptors that you can include in your application
- contain a lot of the dependencies
- name pattern: spring-boot-starter-*
- Automatically configure Spring application based on inclulded jar dependencies
- Application class - place in root package
- @EnableAutoConfiguration
- @ComponentScan - default root package
-
@SpringBootApplication
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan
- properties files/YAML files/ environment variables/ command-line arguments
- profile specific configuration
- profiles
- By default Logback (with bridges for JUL, LOG4J, ...).
- configuration in "application.properties"
- supports custom external configuration
- console
- file
-
spring-boot-starter-test
- include: assertJ, Mockito, JUnit 5, Hamcrast, ..
- @SpringBootTest
- @TestConfiguration
- @MockBean, @SpyBean
-
Auto configured tests
- Spring MVC tests - @WebMvcTest
- JSON Tests - @JsonTest
- Data JPA Tests - @DataJpaTest
- ...
- Generation of getters, setters, constructos, toString, hashCode, ...
- IntelliJ plugin
- Other IDE plugins
Integration testing
- Martin Fowler: "ITs determine if independently developed units of software work correctly when they are connected to each other"
- Can be expensive for resources
- Links:
Java itegration tests
-
maven-failsafe-plugin
- designed for running integration tests
- name convention *IT.java, *IT*.java, *ITCase.java
- runs in verify maven phase (not in test phase)
- IT of repository layer
- HyperSQL vs. Derby vs. H2
- Pros: Fast, no external resoruces needed, easy to run in CI
- Cons: Not real prduction database, SQL syntax can differ, may lead to separate SQL scripts
Java unit tests
-
maven-surefire-plugin
- designed for running unit tests
- name convention *Test.java, *TestCase.java
- runs in test maven phase (not in verify phase)
- Can use Spring context, but not all of it and should not test the integration
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*IT</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<includes>
<include>**/*IT</include>
</includes>
</configuration>
</plugin>Q & A
ITA-05-Java W10
By IT-absolvent
ITA-05-Java W10
Workshop #10
- 483