├── .gitignore ├── src ├── main │ ├── resources │ │ ├── application.properties │ │ └── db │ │ │ └── migration │ │ │ ├── V1__create_person_table.sql │ │ │ └── V2__add_first_and_lastname_to_person_table.sql │ └── java │ │ └── de │ │ └── mvitz │ │ └── bsbt │ │ ├── Application.java │ │ ├── SomeService.java │ │ ├── TimeController.java │ │ └── Person.java └── test │ └── java │ └── de │ └── mvitz │ └── bsbt │ ├── EqualsHashCodeTests.java │ ├── SetupConstructorTests.java │ ├── SetupDefaultTests.java │ ├── SetupObjectMotherTests.java │ ├── InstancePerTestTests.java │ ├── LoggingExtensionsTests.java │ ├── SetupBuilderTests.java │ ├── SetupMakeItEasyTests.java │ ├── TimeControllerTest.java │ ├── SetupDataFakerTests.java │ ├── SetupInstancioTests.java │ ├── V2AddFirstAndLastnameToPersonTableTest.java │ ├── SetupRandomTests.java │ ├── ParameterizedTestTests.java │ ├── AssertionsCustomTests.java │ ├── WithLocalDateTime.java │ └── MigrationTest.java ├── compose.yml ├── .editorconfig ├── README.md ├── .gitattributes ├── .mvn └── wrapper │ └── maven-wrapper.properties ├── pom.xml ├── mvnw.cmd └── mvnw /.gitignore: -------------------------------------------------------------------------------- 1 | # IntelliJ 2 | .idea/ 3 | *.iml 4 | 5 | # Mac 6 | .DS_Store 7 | 8 | # Maven 9 | .mvn/wrapper/MavenWrapperDownloader.java 10 | target/ 11 | 12 | # sdkman 13 | .sdkmanrc 14 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:postgresql://localhost:5432/test 2 | spring.datasource.username=postgres 3 | spring.datasource.password=somerandompassword 4 | spring.main.banner-mode=off 5 | -------------------------------------------------------------------------------- /src/main/resources/db/migration/V1__create_person_table.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE person ( 2 | id SERIAL PRIMARY KEY, 3 | name VARCHAR NOT NULL 4 | ); 5 | 6 | INSERT INTO person (name) VALUES 7 | ('Michael Vitz'), 8 | ('Test Mensch'), 9 | ('Nochein Test'); 10 | -------------------------------------------------------------------------------- /compose.yml: -------------------------------------------------------------------------------- 1 | name: "beyond-boot-testing" 2 | 3 | services: 4 | postgres: 5 | image: postgres:15 6 | ports: 7 | - "5432:5432" 8 | environment: 9 | POSTGRES_DB: "test" 10 | POSTGRES_USER: "postgres" 11 | POSTGRES_PASSWORD: "somerandompassword" 12 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/EqualsHashCodeTests.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import nl.jqno.equalsverifier.EqualsVerifier; 4 | import org.junit.jupiter.api.Test; 5 | 6 | class EqualsHashCodeTests { 7 | 8 | @Test 9 | void equalsAndHashCode_shouldFulfillContract() { 10 | EqualsVerifier.forClass(Person.class).verify(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | charset = utf-8 7 | end_of_line = lf 8 | indent_size = 4 9 | indent_style = space 10 | insert_final_newline = true 11 | max_line_length = 80 12 | trim_trailing_whitespace = true 13 | 14 | [*.xml] 15 | indent_size = 2 16 | 17 | [*.yml] 18 | indent_size = 2 19 | 20 | [COMMIT_EDITMSG] 21 | max_line_length = 72 22 | trim_trailing_whitespace = false 23 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/SetupConstructorTests.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | class SetupConstructorTests { 6 | 7 | @Test 8 | void total_shouldIncludeDiscount_whenPersonIsAdult() { 9 | // given 10 | var adult = new Person("NameDoesNotMatter", 18); 11 | 12 | // when 13 | // ... 14 | 15 | // then 16 | // ... 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/SetupDefaultTests.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | class SetupDefaultTests { 6 | 7 | @Test 8 | void total_shouldIncludeDiscount_whenPersonIsAdult() { 9 | // given 10 | var adult = new Person(); 11 | adult.setAge(18); 12 | 13 | // when 14 | // ... 15 | 16 | // then 17 | // ... 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Beyond Built-in: Advanced Testing Techniques for Spring Boot Applications 2 | 3 | This repository contains all code used for the presentation. 4 | 5 | The slides of the presentation can be found 6 | [here](https://www.innoq.com/en/talks/2024/05/beyond-built-in-advanced-testing-techniques-for-spring-boot-applications/). 7 | 8 | Additional you can read my article [Testing in Spring Boot applications](https://www.innoq.com/en/articles/2023/10/spring-boot-testing/). 9 | -------------------------------------------------------------------------------- /src/main/resources/db/migration/V2__add_first_and_lastname_to_person_table.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE person 2 | ADD COLUMN firstname VARCHAR, 3 | ADD COLUMN lastname VARCHAR; 4 | 5 | UPDATE person p 6 | SET 7 | firstname = split_part(p.name, ' ', 1), 8 | lastname = split_part(p.name, ' ', 2) 9 | FROM person po 10 | WHERE p.name = po.name; 11 | 12 | ALTER TABLE person 13 | ALTER COLUMN firstname SET NOT NULL, 14 | ALTER COLUMN lastname SET NOT NULL, 15 | DROP COLUMN name; 16 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/SetupObjectMotherTests.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | class SetupObjectMotherTests { 6 | 7 | @Test 8 | void total_shouldIncludeDiscount_whenPersonIsAdult() { 9 | // given 10 | var adult = Persons.MICHAEL; // or Persons.michael(); 11 | 12 | // when 13 | // ... 14 | 15 | // then 16 | // ... 17 | } 18 | 19 | static class Persons { 20 | 21 | static final Person MICHAEL = new Person("Michael", 37); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Handle line endings automatically for files detected as text and leave all 2 | # files detected as binary untouched. 3 | * text=auto 4 | 5 | 6 | # These files are text and should be normalized 7 | # (Convert crlf => lf) 8 | *.java text 9 | *.md text 10 | *.properties text 11 | *.xml text 12 | *.yml text 13 | 14 | 15 | # These files are binary and should be left untouched 16 | # (binary is a macro for -text -diff) 17 | 18 | 19 | # Windows 20 | *.cmd text eol=crlf 21 | 22 | 23 | # Unix/Linux 24 | mvnw text eol=lf 25 | 26 | 27 | # LFS 28 | 29 | -------------------------------------------------------------------------------- /src/main/java/de/mvitz/bsbt/Application.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.annotation.Bean; 6 | 7 | import java.time.Clock; 8 | 9 | @SpringBootApplication 10 | public class Application { 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(Application.class, args); 14 | } 15 | 16 | @Bean 17 | public Clock clock() { 18 | return Clock.systemUTC(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/de/mvitz/bsbt/SomeService.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | public class SomeService { 7 | 8 | private static final Logger LOGGER = LoggerFactory.getLogger(SomeService.class); 9 | 10 | static void doSomething(int tries) { 11 | try { 12 | // ... 13 | throw new Exception("Some error"); 14 | } catch (Exception e) { 15 | if (tries > 2) { 16 | // ... 17 | LOGGER.error("Giving up"); 18 | } else { 19 | // ... 20 | LOGGER.warn("Failure, trying again later"); 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/de/mvitz/bsbt/TimeController.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import java.time.Clock; 7 | import java.time.LocalDateTime; 8 | import java.time.format.DateTimeFormatter; 9 | 10 | @RestController 11 | public class TimeController { 12 | 13 | static DateTimeFormatter DATE_TIME_FORMAT = 14 | DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 15 | 16 | private final Clock clock; 17 | 18 | public TimeController(Clock clock) { 19 | this.clock = clock; 20 | } 21 | 22 | @GetMapping("/time") 23 | public String now() { 24 | LocalDateTime now = LocalDateTime.now(clock); 25 | return now.format(DATE_TIME_FORMAT); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/InstancePerTestTests.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | 7 | class InstancePerTestTests { 8 | 9 | Person adultPerson = new Person(42); 10 | 11 | /* Not required because by default JUnit creates a new instance for every test 12 | @BeforeEach 13 | void setUp() { 14 | adultPerson = new Person(42); 15 | } 16 | */ 17 | 18 | @Test 19 | void test1() { 20 | assertThat(adultPerson.getAge()).isEqualTo(42); 21 | 22 | adultPerson.setAge(21); 23 | assertThat(adultPerson.getAge()).isEqualTo(21); 24 | } 25 | 26 | @Test 27 | void test2() { 28 | assertThat(adultPerson.getAge()).isEqualTo(42); 29 | 30 | adultPerson.setAge(37); 31 | assertThat(adultPerson.getAge()).isEqualTo(37); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/LoggingExtensionsTests.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import ch.qos.logback.classic.spi.ILoggingEvent; 4 | import com.innoq.junit.jupiter.logging.Logging; 5 | import com.innoq.junit.jupiter.logging.LoggingEvents; 6 | import org.junit.jupiter.api.Test; 7 | 8 | import static de.mvitz.bsbt.SomeService.doSomething; 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | // see https://github.com/innoq/junit5-logging-extension for extension implementation 12 | class LoggingExtensionsTests { 13 | 14 | @Test 15 | void doSomething_shouldLogError_whenMoreThanTwoTries( 16 | @Logging LoggingEvents events) { 17 | // when 18 | doSomething(3); 19 | 20 | // then 21 | assertThat(events.all()) 22 | .isNotEmpty() 23 | .extracting(ILoggingEvent::getFormattedMessage) 24 | .containsExactly("Giving up"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/SetupBuilderTests.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | class SetupBuilderTests { 6 | 7 | @Test 8 | void total_shouldIncludeDiscount_whenPersonIsAdult() { 9 | // given 10 | var adult = Persons.aPerson() 11 | .withAge(18) 12 | .build(); 13 | 14 | // when 15 | // ... 16 | 17 | // then 18 | // ... 19 | } 20 | 21 | static class Persons { 22 | 23 | static Builder aPerson() { 24 | return new Builder(); 25 | } 26 | 27 | static class Builder { 28 | 29 | Person person = new Person(); 30 | 31 | Builder withAge(int age) { 32 | person.setAge(age); 33 | return this; 34 | } 35 | 36 | Person build() { 37 | return person; 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | wrapperVersion=3.3.2 18 | distributionType=only-script 19 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.7/apache-maven-3.9.7-bin.zip 20 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/SetupMakeItEasyTests.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import com.natpryce.makeiteasy.Instantiator; 4 | import com.natpryce.makeiteasy.Property; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import static com.natpryce.makeiteasy.MakeItEasy.an; 8 | import static com.natpryce.makeiteasy.MakeItEasy.with; 9 | 10 | class SetupMakeItEasyTests { 11 | 12 | @Test 13 | void total_shouldIncludeDiscount_whenPersonIsAdult() { 14 | // given 15 | var adult = an(Persons.Person, 16 | with(42, Persons.age)) 17 | .make(); 18 | 19 | // when 20 | // ... 21 | 22 | // then 23 | // ... 24 | } 25 | 26 | static class Persons { 27 | 28 | static final Property name = Property.newProperty(); 29 | static final Property age = Property.newProperty(); 30 | 31 | static final Instantiator Person = lookup -> new Person( 32 | lookup.valueOf(name, "Michael"), 33 | lookup.valueOf(age, 42)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/TimeControllerTest.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; 6 | import org.springframework.test.web.servlet.MockMvc; 7 | 8 | import static org.hamcrest.Matchers.equalTo; 9 | import static org.hamcrest.Matchers.is; 10 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 11 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 12 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 13 | 14 | @WebMvcTest 15 | @WithLocalDateTime(date = "2024-05-31", time = "17:42:53") 16 | class TimeControllerTest { 17 | 18 | @Autowired 19 | MockMvc mvc; 20 | 21 | @Test 22 | void now_shouldRenderCurrentTime() throws Exception { 23 | mvc.perform(get("/time")) 24 | .andExpect(status().isOk()) 25 | .andExpect(content().string( 26 | is(equalTo("2024-05-31 17:42:53")))); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/SetupDataFakerTests.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import net.datafaker.Faker; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.Locale; 7 | import java.util.Random; 8 | 9 | class SetupDataFakerTests { 10 | 11 | @Test 12 | void total_shouldIncludeDiscount_whenPersonIsAdult() { 13 | // given 14 | var adult = Persons.aPerson() 15 | .withAge(18) 16 | .build(); 17 | 18 | // when 19 | // ... 20 | 21 | // then 22 | // ... 23 | } 24 | 25 | static class Persons { 26 | 27 | static Faker FAKER = new Faker(Locale.of("es"), new Random(42)); 28 | 29 | static Builder aPerson() { 30 | return new Builder(); 31 | } 32 | 33 | static class Builder { 34 | 35 | Person person = new Person( 36 | FAKER.name().fullName(), 37 | FAKER.number().numberBetween(0, 120)); 38 | 39 | Builder withAge(int age) { 40 | person.setAge(age); 41 | return this; 42 | } 43 | 44 | Person build() { 45 | return person; 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/SetupInstancioTests.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.instancio.Instancio; 4 | import org.instancio.Select; 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SetupInstancioTests { 8 | 9 | @Test 10 | void total_shouldIncludeDiscount_whenPersonIsAdult() { 11 | // given 12 | var adult = Persons.aPerson() 13 | .withAge(18) 14 | .build(); 15 | 16 | // when 17 | // ... 18 | 19 | // then 20 | // ... 21 | } 22 | 23 | static class Persons { 24 | 25 | static Builder aPerson() { 26 | return new Builder(); 27 | } 28 | 29 | static class Builder { 30 | 31 | Person person = Instancio.of(Person.class) 32 | .generate(Select.field(Person::getAge), gen -> gen.ints().min(0).max(120)) 33 | .generate(Select.field(Person::getName), gen -> gen.string().alphaNumeric()) 34 | .create(); 35 | 36 | Builder withAge(int age) { 37 | person.setAge(age); 38 | return this; 39 | } 40 | 41 | Person build() { 42 | return person; 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/V2AddFirstAndLastnameToPersonTableTest.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import de.mvitz.bsbt.MigrationTest.MigrationTestTemplate; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.jdbc.core.JdbcTemplate; 7 | 8 | import static org.assertj.core.api.Assertions.assertThat; 9 | 10 | @MigrationTest(fromVersion = 1, toVersion = 2) 11 | class V2AddFirstAndLastnameToPersonTableTest { 12 | 13 | @Autowired 14 | JdbcTemplate jdbcTemplate; 15 | 16 | @Test 17 | void migration_shouldSplitNameIntoFirstAndLastname(MigrationTestTemplate template) { 18 | template.beforeMigration(() -> { 19 | jdbcTemplate.execute("TRUNCATE TABLE person"); 20 | jdbcTemplate.execute("INSERT INTO person (name) VALUES ('Test Fixture')"); 21 | }); 22 | 23 | template.afterMigration(() -> { 24 | String person = jdbcTemplate.queryForObject( 25 | "SELECT * FROM person", 26 | (rs, rowNum) -> { 27 | return rs.getString("lastname") + ", " + rs.getString("firstname"); 28 | }); 29 | assertThat(person) 30 | .isEqualTo("Fixture, Test"); 31 | }); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/SetupRandomTests.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.apache.commons.lang3.RandomStringUtils; 4 | import org.apache.commons.lang3.RandomUtils; 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SetupRandomTests { 8 | 9 | @Test 10 | void total_shouldIncludeDiscount_whenPersonIsAdult() { 11 | // given 12 | var adult = Persons.aPerson() 13 | .olderThan(18) 14 | // or .thatsAnAdult() 15 | .build(); 16 | 17 | // when 18 | // ... 19 | 20 | // then 21 | // ... 22 | } 23 | 24 | static class Persons { 25 | 26 | static Builder aPerson() { 27 | return new Builder(); 28 | } 29 | 30 | static class Builder { 31 | 32 | Person person = new Person( 33 | RandomStringUtils.randomAlphabetic(2, 20), 34 | RandomUtils.nextInt(0, 120)); 35 | 36 | Builder withAge(int age) { 37 | person.setAge(age); 38 | return this; 39 | } 40 | 41 | Builder olderThan(int age) { 42 | return withAge(RandomUtils.nextInt(age, 120)); 43 | } 44 | 45 | Builder thatsAnAdult() { 46 | return this.olderThan(21); 47 | } 48 | 49 | Person build() { 50 | return person; 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/de/mvitz/bsbt/Person.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import java.util.Objects; 4 | 5 | public final class Person { 6 | 7 | private String name; 8 | private int age; 9 | private String country; 10 | 11 | Person() {} 12 | 13 | public Person(int age) { 14 | this.age = age; 15 | } 16 | 17 | public Person(String name, int age) { 18 | this(age); 19 | this.name = name; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | public int getAge() { 31 | return age; 32 | } 33 | 34 | public void setAge(int age) { 35 | this.age = age; 36 | } 37 | 38 | public void setCountry(String country) { 39 | this.country = country; 40 | } 41 | 42 | public boolean isAdult() { 43 | if ("US".equalsIgnoreCase(country)) { 44 | return age >= 21; 45 | } 46 | return age >= 18; 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | return Objects.hash(name, age, country); 52 | } 53 | 54 | @Override 55 | public boolean equals(Object obj) { 56 | if (!(obj instanceof Person other)) { 57 | return false; 58 | } 59 | return Objects.equals(name, other.name) 60 | && Objects.equals(age, other.age) 61 | && Objects.equals(country, other.country); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/ParameterizedTestTests.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.junit.jupiter.params.ParameterizedTest; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | import org.junit.jupiter.params.provider.MethodSource; 6 | import org.junit.jupiter.params.provider.ValueSource; 7 | 8 | import java.util.stream.Stream; 9 | 10 | import static org.junit.jupiter.api.Assertions.assertEquals; 11 | import static org.junit.jupiter.api.Assertions.assertTrue; 12 | import static org.junit.jupiter.params.provider.Arguments.arguments; 13 | 14 | class ParameterizedTestTests { 15 | 16 | @ParameterizedTest 17 | @ValueSource(ints = { 18, 42, 77, 99, 122 }) 18 | void isAdult_shouldReturnTrue_whenPersonIsOverEighteen(int age) { 19 | // given 20 | var person = new Person(); 21 | person.setAge(age); 22 | 23 | // when 24 | var isAdult = person.isAdult(); 25 | 26 | // then 27 | assertTrue(isAdult); 28 | } 29 | 30 | @ParameterizedTest 31 | @MethodSource("isAdultWithCountryExamples") 32 | void isAdult_shouldWork_whenPersonIsAdultInGivenCountry(String country, int age, boolean shouldBeAdult) { 33 | // given 34 | var person = new Person(); 35 | person.setCountry(country); 36 | person.setAge(age); 37 | 38 | // when 39 | var isAdult = person.isAdult(); 40 | 41 | // then 42 | assertEquals(shouldBeAdult, isAdult); 43 | } 44 | 45 | static Stream isAdultWithCountryExamples() { 46 | return Stream.of( 47 | arguments("US", 18, false), 48 | arguments("US", 21, true), 49 | arguments("DE", 18, true)); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/AssertionsCustomTests.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.assertj.core.api.AbstractAssert; 4 | import org.assertj.core.api.Assertions; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import static de.mvitz.bsbt.AssertionsCustomTests.PersonAssert.assertThat; 8 | import static org.assertj.core.api.Assertions.assertThat; 9 | 10 | class AssertionsCustomTests { 11 | 12 | @Test 13 | void constructor_shouldSetNameAndAge() { 14 | // when 15 | var michael = new Person("Michael", 38); 16 | 17 | // then 18 | assertThat(michael.getName()).isEqualTo("Michael"); 19 | assertThat(michael.getAge()).isEqualTo(38); 20 | assertThat(michael.isAdult()).isTrue(); 21 | } 22 | 23 | @Test 24 | void constructor_shouldSetNameAndAge2() { 25 | // when 26 | var michael = new Person("Michael", 38); 27 | 28 | // then 29 | Assertions.assertThat(michael) 30 | .extracting(Person::getName, Person::getAge, Person::isAdult) 31 | .containsExactly("Michael", 38, true); 32 | } 33 | 34 | @Test 35 | void constructor_shouldSetNameAndAge3() { 36 | // when 37 | var michael = new Person("Michael", 38); 38 | 39 | // then 40 | assertThat(michael) 41 | .hasName("Michael") 42 | .hasAge(38) 43 | .isAdult(); 44 | } 45 | 46 | static class PersonAssert extends AbstractAssert { 47 | 48 | public PersonAssert(Person actual) { 49 | super(actual, PersonAssert.class); 50 | } 51 | 52 | public PersonAssert hasName(String name) { 53 | return this; 54 | } 55 | 56 | public PersonAssert hasAge(int age) { 57 | return this; 58 | } 59 | 60 | public PersonAssert isAdult() { 61 | isNotNull(); 62 | if (!actual.isAdult()) { 63 | failWithMessage("Expected person to be adult"); 64 | } 65 | return this; 66 | } 67 | 68 | public static PersonAssert assertThat(Person actual) { 69 | return new PersonAssert(actual); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | org.springframework.boot 9 | spring-boot-starter-parent 10 | 3.3.0 11 | 12 | 13 | 14 | de.mvitz 15 | beyond-spring-boot-testing 16 | 0.1.0-SNAPSHOT 17 | 18 | 19 | 21 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-jdbc 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | 33 | org.flywaydb 34 | flyway-database-postgresql 35 | runtime 36 | 37 | 38 | org.postgresql 39 | postgresql 40 | runtime 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-test 46 | test 47 | 48 | 49 | 50 | com.innoq 51 | junit5-logging-extension 52 | 0.2.0 53 | test 54 | 55 | 56 | com.natpryce 57 | make-it-easy 58 | 4.0.1 59 | test 60 | 61 | 62 | net.datafaker 63 | datafaker 64 | 2.2.2 65 | test 66 | 67 | 68 | org.apache.commons 69 | commons-lang3 70 | test 71 | 72 | 73 | org.instancio 74 | instancio-junit 75 | 4.7.0 76 | test 77 | 78 | 79 | nl.jqno.equalsverifier 80 | equalsverifier 81 | 3.16.1 82 | test 83 | 84 | 85 | 86 | org.springframework.boot 87 | spring-boot-devtools 88 | runtime 89 | true 90 | 91 | 92 | 93 | 94 | ${project.artifactId} 95 | 96 | 97 | org.springframework.boot 98 | spring-boot-maven-plugin 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/WithLocalDateTime.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.junit.jupiter.api.extension.AfterEachCallback; 4 | import org.junit.jupiter.api.extension.BeforeEachCallback; 5 | import org.junit.jupiter.api.extension.ExtendWith; 6 | import org.junit.jupiter.api.extension.ExtensionContext; 7 | import org.junit.jupiter.api.extension.ExtensionContext.Namespace; 8 | import org.springframework.boot.autoconfigure.ImportAutoConfiguration; 9 | import org.springframework.boot.test.context.TestConfiguration; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.test.context.TestPropertySource; 12 | 13 | import java.lang.annotation.Documented; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.Target; 17 | import java.time.Clock; 18 | import java.time.Instant; 19 | import java.time.LocalDate; 20 | import java.time.LocalDateTime; 21 | import java.time.LocalTime; 22 | import java.time.ZoneId; 23 | import java.time.format.DateTimeFormatter; 24 | 25 | import static java.lang.annotation.ElementType.TYPE; 26 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 27 | import static java.time.ZoneOffset.UTC; 28 | import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation; 29 | import static org.springframework.test.context.junit.jupiter.SpringExtension.getApplicationContext; 30 | 31 | @Target(TYPE) 32 | @Retention(RUNTIME) 33 | @Documented 34 | @Inherited 35 | @ExtendWith(WithLocalDateTime.WithLocalDateTimeExtension.class) 36 | @TestPropertySource(properties = "spring.main.allow-bean-definition-overriding=true") 37 | @ImportAutoConfiguration(WithLocalDateTime.ClockConfiguration.class) 38 | public @interface WithLocalDateTime { 39 | 40 | String date(); 41 | String time(); 42 | 43 | @TestConfiguration 44 | class ClockConfiguration { 45 | 46 | @Bean 47 | public Clock clock() { 48 | return new DelegatingClock(Clock.systemUTC()); 49 | } 50 | } 51 | 52 | class WithLocalDateTimeExtension implements BeforeEachCallback, AfterEachCallback { 53 | 54 | private static final Namespace EXTENSION_SCOPE = Namespace.create(WithLocalDateTimeExtension.class); 55 | private static final Class DELEGATING_CLOCK_KEY = DelegatingClock.class; 56 | 57 | @Override 58 | public void beforeEach(ExtensionContext extensionContext) { 59 | findAnnotation(extensionContext.getTestClass(), WithLocalDateTime.class) 60 | .ifPresent((withLocalDateTime) -> setClockTo(extensionContext, withLocalDateTime)); 61 | } 62 | 63 | private static void setClockTo(ExtensionContext extensionContext, WithLocalDateTime withLocalDateTime) { 64 | DelegatingClock delegatingClock = delegatingClockFrom(extensionContext); 65 | 66 | Clock delegate = delegatingClock.getDelegate(); 67 | extensionContext.getStore(EXTENSION_SCOPE).put(DELEGATING_CLOCK_KEY, delegate); 68 | 69 | LocalDateTime localDateTime = localDateTimeFrom(withLocalDateTime); 70 | delegatingClock.setDelegate(fixedClock(localDateTime)); 71 | } 72 | 73 | private static DelegatingClock delegatingClockFrom(ExtensionContext context) { 74 | Clock clock = getApplicationContext(context).getBean(Clock.class); 75 | 76 | if (!(clock instanceof DelegatingClock)) { 77 | throw new IllegalStateException("clock '" + clock + "' must be of type '" + DelegatingClock.class.getName() + "'"); 78 | } 79 | 80 | return (DelegatingClock) clock; 81 | } 82 | 83 | private static Clock fixedClock(LocalDateTime localDateTime) { 84 | return Clock.fixed(localDateTime.atZone(UTC).toInstant(), UTC); 85 | } 86 | 87 | @Override 88 | public void afterEach(ExtensionContext extensionContext) { 89 | findAnnotation(extensionContext.getTestClass(), WithLocalDateTime.class) 90 | .ifPresent((withLocalDateTime) -> resetClockFrom(extensionContext)); 91 | } 92 | 93 | private static void resetClockFrom(ExtensionContext extensionContext) { 94 | DelegatingClock delegatingClock = delegatingClockFrom(extensionContext); 95 | 96 | Clock delegate = (Clock) extensionContext.getStore(EXTENSION_SCOPE).remove(DELEGATING_CLOCK_KEY); 97 | 98 | delegatingClock.setDelegate(delegate); 99 | } 100 | 101 | private static LocalDateTime localDateTimeFrom(WithLocalDateTime withLocalDateTime) { 102 | return LocalDateTime.of( 103 | LocalDate.parse(withLocalDateTime.date(), DateTimeFormatter.ofPattern("yyyy-MM-dd")), 104 | LocalTime.parse(withLocalDateTime.time(), DateTimeFormatter.ofPattern("HH:mm:ss"))); 105 | } 106 | } 107 | 108 | class DelegatingClock extends Clock { 109 | 110 | private Clock delegate; 111 | 112 | DelegatingClock(Clock delegate) { 113 | this.delegate = delegate; 114 | } 115 | 116 | Clock getDelegate() { 117 | return delegate; 118 | } 119 | 120 | void setDelegate(Clock delegate) { 121 | this.delegate = delegate; 122 | } 123 | 124 | @Override 125 | public ZoneId getZone() { 126 | return delegate.getZone(); 127 | } 128 | 129 | @Override 130 | public Clock withZone(ZoneId zone) { 131 | return delegate.withZone(zone); 132 | } 133 | 134 | @Override 135 | public Instant instant() { 136 | return delegate.instant(); 137 | } 138 | } 139 | } 140 | 141 | -------------------------------------------------------------------------------- /src/test/java/de/mvitz/bsbt/MigrationTest.java: -------------------------------------------------------------------------------- 1 | package de.mvitz.bsbt; 2 | 3 | import org.flywaydb.core.Flyway; 4 | import org.junit.jupiter.api.extension.*; 5 | import org.junit.jupiter.api.function.Executable; 6 | import org.springframework.boot.autoconfigure.ImportAutoConfiguration; 7 | import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.jdbc.core.JdbcTemplate; 10 | import org.springframework.jdbc.support.MetaDataAccessException; 11 | 12 | import javax.sql.DataSource; 13 | import java.lang.annotation.Documented; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.Target; 17 | import java.sql.ResultSet; 18 | 19 | import static java.lang.String.valueOf; 20 | import static java.lang.annotation.ElementType.TYPE; 21 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 22 | import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation; 23 | import static org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData; 24 | import static org.springframework.test.context.junit.jupiter.SpringExtension.getApplicationContext; 25 | 26 | @Target(TYPE) 27 | @Retention(RUNTIME) 28 | @Documented 29 | @Inherited 30 | @SpringBootTest 31 | @ImportAutoConfiguration(exclude = FlywayAutoConfiguration.class) 32 | @ExtendWith(MigrationTest.FlywayMigrationTestExtension.class) 33 | public @interface MigrationTest { 34 | 35 | int fromVersion(); 36 | 37 | int toVersion(); 38 | 39 | class FlywayMigrationTestExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver { 40 | 41 | @Override 42 | public void beforeEach(ExtensionContext context) throws Exception { 43 | // drop all tables 44 | JdbcTemplate jdbcTemplate = getApplicationContext(context).getBean(JdbcTemplate.class); 45 | dropAllTables(jdbcTemplate); 46 | } 47 | 48 | @Override 49 | public void afterEach(ExtensionContext context) throws Exception { 50 | // drop all tables and reapply all migrations 51 | JdbcTemplate jdbcTemplate = getApplicationContext(context).getBean(JdbcTemplate.class); 52 | dropAllTables(jdbcTemplate); 53 | 54 | DataSource dataSource = jdbcTemplate.getDataSource(); 55 | 56 | var flyway = Flyway.configure() 57 | .dataSource(dataSource) 58 | .locations("/db/migration") 59 | .load(); 60 | 61 | flyway.migrate(); 62 | } 63 | 64 | @Override 65 | public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { 66 | return MigrationTestTemplate.class.equals(parameterContext.getParameter().getType()) 67 | && findAnnotation(extensionContext.getTestClass(), MigrationTest.class).isPresent(); 68 | } 69 | 70 | @Override 71 | public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { 72 | return findAnnotation(extensionContext.getTestClass(), MigrationTest.class) 73 | .map((migrationTest) -> { 74 | DataSource dataSource = getApplicationContext(extensionContext).getBean(DataSource.class); 75 | int fromVersion = migrationTest.fromVersion(); 76 | int toVersion = migrationTest.toVersion(); 77 | 78 | return new MigrationTestTemplate(dataSource, fromVersion, toVersion); 79 | }) 80 | .orElseThrow(() -> new IllegalStateException("unable to create MigrationRestTemplate parameter without @MigrationTest present on test class")); 81 | } 82 | 83 | private static void dropAllTables(JdbcTemplate jdbcTemplate) throws MetaDataAccessException { 84 | DataSource dataSource = jdbcTemplate.getDataSource(); 85 | 86 | extractDatabaseMetaData(dataSource, (databaseMetaData) -> { 87 | ResultSet resultSet = databaseMetaData.getTables(null, null, "%", new String[]{"TABLE"}); 88 | 89 | while (resultSet.next()) { 90 | String tableName = resultSet.getString(3); 91 | jdbcTemplate.execute("DROP TABLE \"" + tableName + "\""); 92 | } 93 | 94 | return null; 95 | }); 96 | } 97 | } 98 | 99 | class MigrationTestTemplate { 100 | 101 | private final DataSource dataSource; 102 | private final int fromVersion; 103 | private final int toVersion; 104 | 105 | MigrationTestTemplate(DataSource dataSource, int fromVersion, int toVersion) { 106 | this.dataSource = dataSource; 107 | this.fromVersion = fromVersion; 108 | this.toVersion = toVersion; 109 | } 110 | 111 | public void beforeMigration(Executable executable) { 112 | try { 113 | migrateUpTo(fromVersion); 114 | executable.execute(); 115 | } catch (Throwable throwable) { 116 | throw new IllegalStateException("unable to execute pre-migration steps", throwable); 117 | } 118 | } 119 | 120 | public void afterMigration(Executable executable) { 121 | try { 122 | migrateUpTo(toVersion); 123 | executable.execute(); 124 | } catch (Throwable throwable) { 125 | throw new IllegalStateException("unable to execute post-migration steps", throwable); 126 | } 127 | } 128 | 129 | private void migrateUpTo(int upToVersion) { 130 | Flyway.configure() 131 | .dataSource(dataSource) 132 | .locations("/db/migration") 133 | .target(valueOf(upToVersion)) 134 | .load() 135 | .migrate(); 136 | } 137 | } 138 | } 139 | 140 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | <# : batch portion 2 | @REM ---------------------------------------------------------------------------- 3 | @REM Licensed to the Apache Software Foundation (ASF) under one 4 | @REM or more contributor license agreements. See the NOTICE file 5 | @REM distributed with this work for additional information 6 | @REM regarding copyright ownership. The ASF licenses this file 7 | @REM to you under the Apache License, Version 2.0 (the 8 | @REM "License"); you may not use this file except in compliance 9 | @REM with the License. You may obtain a copy of the License at 10 | @REM 11 | @REM http://www.apache.org/licenses/LICENSE-2.0 12 | @REM 13 | @REM Unless required by applicable law or agreed to in writing, 14 | @REM software distributed under the License is distributed on an 15 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | @REM KIND, either express or implied. See the License for the 17 | @REM specific language governing permissions and limitations 18 | @REM under the License. 19 | @REM ---------------------------------------------------------------------------- 20 | 21 | @REM ---------------------------------------------------------------------------- 22 | @REM Apache Maven Wrapper startup batch script, version 3.3.2 23 | @REM 24 | @REM Optional ENV vars 25 | @REM MVNW_REPOURL - repo url base for downloading maven distribution 26 | @REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven 27 | @REM MVNW_VERBOSE - true: enable verbose log; others: silence the output 28 | @REM ---------------------------------------------------------------------------- 29 | 30 | @IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) 31 | @SET __MVNW_CMD__= 32 | @SET __MVNW_ERROR__= 33 | @SET __MVNW_PSMODULEP_SAVE=%PSModulePath% 34 | @SET PSModulePath= 35 | @FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( 36 | IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) 37 | ) 38 | @SET PSModulePath=%__MVNW_PSMODULEP_SAVE% 39 | @SET __MVNW_PSMODULEP_SAVE= 40 | @SET __MVNW_ARG0_NAME__= 41 | @SET MVNW_USERNAME= 42 | @SET MVNW_PASSWORD= 43 | @IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*) 44 | @echo Cannot start maven from wrapper >&2 && exit /b 1 45 | @GOTO :EOF 46 | : end batch / begin powershell #> 47 | 48 | $ErrorActionPreference = "Stop" 49 | if ($env:MVNW_VERBOSE -eq "true") { 50 | $VerbosePreference = "Continue" 51 | } 52 | 53 | # calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties 54 | $distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl 55 | if (!$distributionUrl) { 56 | Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" 57 | } 58 | 59 | switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { 60 | "maven-mvnd-*" { 61 | $USE_MVND = $true 62 | $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" 63 | $MVN_CMD = "mvnd.cmd" 64 | break 65 | } 66 | default { 67 | $USE_MVND = $false 68 | $MVN_CMD = $script -replace '^mvnw','mvn' 69 | break 70 | } 71 | } 72 | 73 | # apply MVNW_REPOURL and calculate MAVEN_HOME 74 | # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ 75 | if ($env:MVNW_REPOURL) { 76 | $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" } 77 | $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')" 78 | } 79 | $distributionUrlName = $distributionUrl -replace '^.*/','' 80 | $distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' 81 | $MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain" 82 | if ($env:MAVEN_USER_HOME) { 83 | $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain" 84 | } 85 | $MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' 86 | $MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" 87 | 88 | if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { 89 | Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" 90 | Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" 91 | exit $? 92 | } 93 | 94 | if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { 95 | Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" 96 | } 97 | 98 | # prepare tmp dir 99 | $TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile 100 | $TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" 101 | $TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null 102 | trap { 103 | if ($TMP_DOWNLOAD_DIR.Exists) { 104 | try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } 105 | catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } 106 | } 107 | } 108 | 109 | New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null 110 | 111 | # Download and Install Apache Maven 112 | Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." 113 | Write-Verbose "Downloading from: $distributionUrl" 114 | Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" 115 | 116 | $webclient = New-Object System.Net.WebClient 117 | if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { 118 | $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) 119 | } 120 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 121 | $webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null 122 | 123 | # If specified, validate the SHA-256 sum of the Maven distribution zip file 124 | $distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum 125 | if ($distributionSha256Sum) { 126 | if ($USE_MVND) { 127 | Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." 128 | } 129 | Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash 130 | if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { 131 | Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." 132 | } 133 | } 134 | 135 | # unzip and move 136 | Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null 137 | Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null 138 | try { 139 | Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null 140 | } catch { 141 | if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { 142 | Write-Error "fail to move MAVEN_HOME" 143 | } 144 | } finally { 145 | try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } 146 | catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } 147 | } 148 | 149 | Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" 150 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Apache Maven Wrapper startup batch script, version 3.3.2 23 | # 24 | # Optional ENV vars 25 | # ----------------- 26 | # JAVA_HOME - location of a JDK home dir, required when download maven via java source 27 | # MVNW_REPOURL - repo url base for downloading maven distribution 28 | # MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven 29 | # MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output 30 | # ---------------------------------------------------------------------------- 31 | 32 | set -euf 33 | [ "${MVNW_VERBOSE-}" != debug ] || set -x 34 | 35 | # OS specific support. 36 | native_path() { printf %s\\n "$1"; } 37 | case "$(uname)" in 38 | CYGWIN* | MINGW*) 39 | [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" 40 | native_path() { cygpath --path --windows "$1"; } 41 | ;; 42 | esac 43 | 44 | # set JAVACMD and JAVACCMD 45 | set_java_home() { 46 | # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched 47 | if [ -n "${JAVA_HOME-}" ]; then 48 | if [ -x "$JAVA_HOME/jre/sh/java" ]; then 49 | # IBM's JDK on AIX uses strange locations for the executables 50 | JAVACMD="$JAVA_HOME/jre/sh/java" 51 | JAVACCMD="$JAVA_HOME/jre/sh/javac" 52 | else 53 | JAVACMD="$JAVA_HOME/bin/java" 54 | JAVACCMD="$JAVA_HOME/bin/javac" 55 | 56 | if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then 57 | echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 58 | echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 59 | return 1 60 | fi 61 | fi 62 | else 63 | JAVACMD="$( 64 | 'set' +e 65 | 'unset' -f command 2>/dev/null 66 | 'command' -v java 67 | )" || : 68 | JAVACCMD="$( 69 | 'set' +e 70 | 'unset' -f command 2>/dev/null 71 | 'command' -v javac 72 | )" || : 73 | 74 | if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then 75 | echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 76 | return 1 77 | fi 78 | fi 79 | } 80 | 81 | # hash string like Java String::hashCode 82 | hash_string() { 83 | str="${1:-}" h=0 84 | while [ -n "$str" ]; do 85 | char="${str%"${str#?}"}" 86 | h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) 87 | str="${str#?}" 88 | done 89 | printf %x\\n $h 90 | } 91 | 92 | verbose() { :; } 93 | [ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } 94 | 95 | die() { 96 | printf %s\\n "$1" >&2 97 | exit 1 98 | } 99 | 100 | trim() { 101 | # MWRAPPER-139: 102 | # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. 103 | # Needed for removing poorly interpreted newline sequences when running in more 104 | # exotic environments such as mingw bash on Windows. 105 | printf "%s" "${1}" | tr -d '[:space:]' 106 | } 107 | 108 | # parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties 109 | while IFS="=" read -r key value; do 110 | case "${key-}" in 111 | distributionUrl) distributionUrl=$(trim "${value-}") ;; 112 | distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; 113 | esac 114 | done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties" 115 | [ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties" 116 | 117 | case "${distributionUrl##*/}" in 118 | maven-mvnd-*bin.*) 119 | MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ 120 | case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in 121 | *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; 122 | :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; 123 | :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; 124 | :Linux*x86_64*) distributionPlatform=linux-amd64 ;; 125 | *) 126 | echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 127 | distributionPlatform=linux-amd64 128 | ;; 129 | esac 130 | distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" 131 | ;; 132 | maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; 133 | *) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; 134 | esac 135 | 136 | # apply MVNW_REPOURL and calculate MAVEN_HOME 137 | # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ 138 | [ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" 139 | distributionUrlName="${distributionUrl##*/}" 140 | distributionUrlNameMain="${distributionUrlName%.*}" 141 | distributionUrlNameMain="${distributionUrlNameMain%-bin}" 142 | MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" 143 | MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" 144 | 145 | exec_maven() { 146 | unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : 147 | exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" 148 | } 149 | 150 | if [ -d "$MAVEN_HOME" ]; then 151 | verbose "found existing MAVEN_HOME at $MAVEN_HOME" 152 | exec_maven "$@" 153 | fi 154 | 155 | case "${distributionUrl-}" in 156 | *?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; 157 | *) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; 158 | esac 159 | 160 | # prepare tmp dir 161 | if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then 162 | clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } 163 | trap clean HUP INT TERM EXIT 164 | else 165 | die "cannot create temp dir" 166 | fi 167 | 168 | mkdir -p -- "${MAVEN_HOME%/*}" 169 | 170 | # Download and Install Apache Maven 171 | verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." 172 | verbose "Downloading from: $distributionUrl" 173 | verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" 174 | 175 | # select .zip or .tar.gz 176 | if ! command -v unzip >/dev/null; then 177 | distributionUrl="${distributionUrl%.zip}.tar.gz" 178 | distributionUrlName="${distributionUrl##*/}" 179 | fi 180 | 181 | # verbose opt 182 | __MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' 183 | [ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v 184 | 185 | # normalize http auth 186 | case "${MVNW_PASSWORD:+has-password}" in 187 | '') MVNW_USERNAME='' MVNW_PASSWORD='' ;; 188 | has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; 189 | esac 190 | 191 | if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then 192 | verbose "Found wget ... using wget" 193 | wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" 194 | elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then 195 | verbose "Found curl ... using curl" 196 | curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" 197 | elif set_java_home; then 198 | verbose "Falling back to use Java to download" 199 | javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" 200 | targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" 201 | cat >"$javaSource" <<-END 202 | public class Downloader extends java.net.Authenticator 203 | { 204 | protected java.net.PasswordAuthentication getPasswordAuthentication() 205 | { 206 | return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); 207 | } 208 | public static void main( String[] args ) throws Exception 209 | { 210 | setDefault( new Downloader() ); 211 | java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); 212 | } 213 | } 214 | END 215 | # For Cygwin/MinGW, switch paths to Windows format before running javac and java 216 | verbose " - Compiling Downloader.java ..." 217 | "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" 218 | verbose " - Running Downloader.java ..." 219 | "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" 220 | fi 221 | 222 | # If specified, validate the SHA-256 sum of the Maven distribution zip file 223 | if [ -n "${distributionSha256Sum-}" ]; then 224 | distributionSha256Result=false 225 | if [ "$MVN_CMD" = mvnd.sh ]; then 226 | echo "Checksum validation is not supported for maven-mvnd." >&2 227 | echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 228 | exit 1 229 | elif command -v sha256sum >/dev/null; then 230 | if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then 231 | distributionSha256Result=true 232 | fi 233 | elif command -v shasum >/dev/null; then 234 | if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then 235 | distributionSha256Result=true 236 | fi 237 | else 238 | echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 239 | echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 240 | exit 1 241 | fi 242 | if [ $distributionSha256Result = false ]; then 243 | echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 244 | echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 245 | exit 1 246 | fi 247 | fi 248 | 249 | # unzip and move 250 | if command -v unzip >/dev/null; then 251 | unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" 252 | else 253 | tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" 254 | fi 255 | printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url" 256 | mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" 257 | 258 | clean || : 259 | exec_maven "$@" 260 | --------------------------------------------------------------------------------