Who am I? ● Software Engineer at Pivotal (Spring Boot, Spring Framework and start.spring.io) ● Involved in open source for 15 years (Apache Maven, Spring Framework) ● snicoll on the Web (Twitter, Github) - [email protected] ● Based in Liège, Belgium (come over, we have waffles)
Who am I? ● Software Engineer at Pivotal (Spring Boot, Spring Framework and start.spring.io) ● Involved in open source for 15 years (Apache Maven, Spring Framework) ● snicoll on the Web (Twitter, Github) - [email protected] ● Based in Liège, Belgium (come over, we have waffles)
Prerequisite “They have a good notion of Java and OOP, know how to use IntelliJ, are used to building tools such as Maven and Gradle, and know JUnit 5 very well. Maurício Aniche
Prerequisite “They have a good notion of Java and OOP, know how to use IntelliJ, are used to building tools such as Maven and Gradle, and know JUnit 5 very well. Maurício Aniche
Summary ● Conventions can significantly improve your productivity ● Best practices for teams and the ecosystem at large ● Provide sane defaults when you don’t or shouldn’t care ● Make upgrade and maintenance easier ● …. but shouldn’t get in your way ● Built on top of an API you could use to implement something slightly or totally different ● Can be customised without having you to redefine everything ● Depending on the scope, not everything can be the target of a convention
Unit tests should not require any “framework” @Service public class SubmissionService { @Autowired private SubmissionRepository submissions; @Transactional public Submission create(SubmissionRequest request) { Submission submission = new Submission(); ... return this.submissions.save(submission); } }
Unit tests should not require any “framework” @Service public class SubmissionService { private final SubmissionRepository submissions; public SubmissionService(SubmissionRepository submissions) { this.submissions = submissions; } @Transactional public Submission create(SubmissionRequest request) { Submission submission = new Submission(); ... return this.submissions.save(submission); } }
Unit testing a client for an HTTP API class SpringBootMetadataReaderTests { private final RestTemplate restTemplate = new RestTemplate(); private final MockRestServiceServer server = MockRestServiceServer.bindTo( this.restTemplate).build(); @Test void readAvailableVersions() throws IOException { this.server.expect(requestTo("https://spring.io/project_metadata/spring-boot")) .andRespond(withSuccess(new ClassPathResource( "metadata/sagan/spring-boot.json"), MediaType.APPLICATION_JSON)); ... this.server.verify(); } }
ApplicationContextRunner ● Test utility to prepare a mini ApplicationContext suitable for a very precise scenario: ● Specific auto-configuration(s) ● User configuration, if any ● Environment tuning (properties customization) ● System properties handling ● Starts/Stop the context automatically with a ContextConsumer callback ● AssertJ style assert ● hasSingleBean, doesNotHaveBean, hasFailed ● getBean, getBeans, etc