diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..f56ecce --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,29 @@ +name: PR build +on: + push: + branches: + - master +jobs: + build-backend: + runs-on: self-hosted + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Set up JDK 17 + uses: actions/setup-java@v2 + with: + java-version: '17' + distribution: 'adopt' + - name: Cache Maven packages + uses: actions/cache@v1 + with: + path: ~/.m2/repository + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-maven- + # Step that runs the tests + - name: Run tests + run: | + chmod 777 mvnw + ./mvnw test diff --git a/mvnw b/mvnw old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml index 8f994ed..02f692f 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.5.5 + 2.6.6 diff --git a/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerMockMvcStandaloneTest.java b/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerMockMvcStandaloneTest.java index af52c26..e64b5f1 100644 --- a/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerMockMvcStandaloneTest.java +++ b/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerMockMvcStandaloneTest.java @@ -30,7 +30,7 @@ * @author moises.macero */ @ExtendWith(MockitoExtension.class) -public class SuperHeroControllerMockMvcStandaloneTest { +class SuperHeroControllerMockMvcStandaloneTest { private MockMvc mvc; @@ -44,7 +44,7 @@ public class SuperHeroControllerMockMvcStandaloneTest { private JacksonTester jsonSuperHero; @BeforeEach - public void setup() { + void setup() { // We would need this line if we would not use the MockitoExtension // MockitoAnnotations.initMocks(this); // Here we can't use @AutoConfigureJsonTesters because there isn't a Spring context @@ -57,7 +57,7 @@ public void setup() { } @Test - public void canRetrieveByIdWhenExists() throws Exception { + void canRetrieveByIdWhenExists() throws Exception { // given given(superHeroRepository.getSuperHero(2)) .willReturn(new SuperHero("Rob", "Mannon", "RobotMan")); @@ -76,7 +76,7 @@ public void canRetrieveByIdWhenExists() throws Exception { } @Test - public void canRetrieveByIdWhenDoesNotExist() throws Exception { + void canRetrieveByIdWhenDoesNotExist() throws Exception { // given given(superHeroRepository.getSuperHero(2)) .willThrow(new NonExistingHeroException()); @@ -93,7 +93,7 @@ public void canRetrieveByIdWhenDoesNotExist() throws Exception { } @Test - public void canRetrieveByNameWhenExists() throws Exception { + void canRetrieveByNameWhenExists() throws Exception { // given given(superHeroRepository.getSuperHero("RobotMan")) .willReturn(Optional.of(new SuperHero("Rob", "Mannon", "RobotMan"))); @@ -112,7 +112,7 @@ public void canRetrieveByNameWhenExists() throws Exception { } @Test - public void canRetrieveByNameWhenDoesNotExist() throws Exception { + void canRetrieveByNameWhenDoesNotExist() throws Exception { // given given(superHeroRepository.getSuperHero("RobotMan")) .willReturn(Optional.empty()); @@ -129,7 +129,7 @@ public void canRetrieveByNameWhenDoesNotExist() throws Exception { } @Test - public void canCreateANewSuperHero() throws Exception { + void canCreateANewSuperHero() throws Exception { // when MockHttpServletResponse response = mvc.perform( post("/superheroes/").contentType(MediaType.APPLICATION_JSON).content( @@ -141,7 +141,7 @@ public void canCreateANewSuperHero() throws Exception { } @Test - public void headerIsPresent() throws Exception { + void headerIsPresent() throws Exception { // when MockHttpServletResponse response = mvc.perform( get("/superheroes/2") diff --git a/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerMockMvcWithContextTest.java b/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerMockMvcWithContextTest.java index a840735..9ce3ac4 100644 --- a/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerMockMvcWithContextTest.java +++ b/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerMockMvcWithContextTest.java @@ -28,7 +28,7 @@ */ @AutoConfigureJsonTesters @WebMvcTest(SuperHeroController.class) -public class SuperHeroControllerMockMvcWithContextTest { +class SuperHeroControllerMockMvcWithContextTest { @Autowired private MockMvc mvc; @@ -41,7 +41,7 @@ public class SuperHeroControllerMockMvcWithContextTest { private JacksonTester jsonSuperHero; @Test - public void canRetrieveByIdWhenExists() throws Exception { + void canRetrieveByIdWhenExists() throws Exception { // given given(superHeroRepository.getSuperHero(2)) .willReturn(new SuperHero("Rob", "Mannon", "RobotMan")); @@ -60,7 +60,7 @@ public void canRetrieveByIdWhenExists() throws Exception { } @Test - public void canRetrieveByIdWhenDoesNotExist() throws Exception { + void canRetrieveByIdWhenDoesNotExist() throws Exception { // given given(superHeroRepository.getSuperHero(2)) .willThrow(new NonExistingHeroException()); @@ -77,7 +77,7 @@ public void canRetrieveByIdWhenDoesNotExist() throws Exception { } @Test - public void canRetrieveByNameWhenExists() throws Exception { + void canRetrieveByNameWhenExists() throws Exception { // given given(superHeroRepository.getSuperHero("RobotMan")) .willReturn(Optional.of(new SuperHero("Rob", "Mannon", "RobotMan"))); @@ -96,7 +96,7 @@ public void canRetrieveByNameWhenExists() throws Exception { } @Test - public void canRetrieveByNameWhenDoesNotExist() throws Exception { + void canRetrieveByNameWhenDoesNotExist() throws Exception { // given given(superHeroRepository.getSuperHero("RobotMan")) .willReturn(Optional.empty()); @@ -113,7 +113,7 @@ public void canRetrieveByNameWhenDoesNotExist() throws Exception { } @Test - public void canCreateANewSuperHero() throws Exception { + void canCreateANewSuperHero() throws Exception { // when MockHttpServletResponse response = mvc.perform( post("/superheroes/").contentType(MediaType.APPLICATION_JSON).content( @@ -125,7 +125,7 @@ public void canCreateANewSuperHero() throws Exception { } @Test - public void headerIsPresent() throws Exception { + void headerIsPresent() throws Exception { // when MockHttpServletResponse response = mvc.perform( get("/superheroes/2") diff --git a/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerSpringBootMockTest.java b/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerSpringBootMockTest.java index 1270e75..1665d33 100644 --- a/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerSpringBootMockTest.java +++ b/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerSpringBootMockTest.java @@ -31,7 +31,7 @@ @AutoConfigureJsonTesters @SpringBootTest @AutoConfigureMockMvc -public class SuperHeroControllerSpringBootMockTest { +class SuperHeroControllerSpringBootMockTest { @Autowired private MockMvc mvc; @@ -44,7 +44,7 @@ public class SuperHeroControllerSpringBootMockTest { private JacksonTester jsonSuperHero; @Test - public void canRetrieveByIdWhenExists() throws Exception { + void canRetrieveByIdWhenExists() throws Exception { // given given(superHeroRepository.getSuperHero(2)) .willReturn(new SuperHero("Rob", "Mannon", "RobotMan")); @@ -63,7 +63,7 @@ public void canRetrieveByIdWhenExists() throws Exception { } @Test - public void canRetrieveByIdWhenDoesNotExist() throws Exception { + void canRetrieveByIdWhenDoesNotExist() throws Exception { // given given(superHeroRepository.getSuperHero(2)) .willThrow(new NonExistingHeroException()); @@ -80,7 +80,7 @@ public void canRetrieveByIdWhenDoesNotExist() throws Exception { } @Test - public void canRetrieveByNameWhenExists() throws Exception { + void canRetrieveByNameWhenExists() throws Exception { // given given(superHeroRepository.getSuperHero("RobotMan")) .willReturn(Optional.of(new SuperHero("Rob", "Mannon", "RobotMan"))); @@ -99,7 +99,7 @@ public void canRetrieveByNameWhenExists() throws Exception { } @Test - public void canRetrieveByNameWhenDoesNotExist() throws Exception { + void canRetrieveByNameWhenDoesNotExist() throws Exception { // given given(superHeroRepository.getSuperHero("RobotMan")) .willReturn(Optional.empty()); @@ -116,7 +116,7 @@ public void canRetrieveByNameWhenDoesNotExist() throws Exception { } @Test - public void canCreateANewSuperHero() throws Exception { + void canCreateANewSuperHero() throws Exception { // when MockHttpServletResponse response = mvc.perform( post("/superheroes/").contentType(MediaType.APPLICATION_JSON).content( @@ -128,7 +128,7 @@ public void canCreateANewSuperHero() throws Exception { } @Test - public void headerIsPresent() throws Exception { + void headerIsPresent() throws Exception { // when MockHttpServletResponse response = mvc.perform( get("/superheroes/2") diff --git a/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerSpringBootTest.java b/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerSpringBootTest.java index be3b86d..1959406 100644 --- a/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerSpringBootTest.java +++ b/src/test/java/io/tpd/superheroes/controller/SuperHeroControllerSpringBootTest.java @@ -11,6 +11,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import java.util.Objects; import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; @@ -24,7 +25,7 @@ * @author moises.macero */ @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -public class SuperHeroControllerSpringBootTest { +class SuperHeroControllerSpringBootTest { @MockBean private SuperHeroRepository superHeroRepository; @@ -33,7 +34,7 @@ public class SuperHeroControllerSpringBootTest { private TestRestTemplate restTemplate; @Test - public void canRetrieveByIdWhenExists() { + void canRetrieveByIdWhenExists() { // given given(superHeroRepository.getSuperHero(2)) .willReturn(new SuperHero("Rob", "Mannon", "RobotMan")); @@ -43,11 +44,11 @@ public void canRetrieveByIdWhenExists() { // then assertThat(superHeroResponse.getStatusCode()).isEqualTo(HttpStatus.OK); - assertThat(superHeroResponse.getBody().equals(new SuperHero("Rob", "Mannon", "RobotMan"))); + assertThat(Objects.equals(superHeroResponse.getBody(), new SuperHero("Rob", "Mannon", "RobotMan"))); } @Test - public void canRetrieveByIdWhenDoesNotExist() { + void canRetrieveByIdWhenDoesNotExist() { // given given(superHeroRepository.getSuperHero(2)) .willThrow(new NonExistingHeroException()); @@ -61,7 +62,7 @@ public void canRetrieveByIdWhenDoesNotExist() { } @Test - public void canRetrieveByNameWhenExists() { + void canRetrieveByNameWhenExists() { // given given(superHeroRepository.getSuperHero("RobotMan")) .willReturn(Optional.of(new SuperHero("Rob", "Mannon", "RobotMan"))); @@ -72,11 +73,11 @@ public void canRetrieveByNameWhenExists() { // then assertThat(superHeroResponse.getStatusCode()).isEqualTo(HttpStatus.OK); - assertThat(superHeroResponse.getBody().equals(new SuperHero("Rob", "Mannon", "RobotMan"))); + assertThat(Objects.equals(superHeroResponse.getBody(), new SuperHero("Rob", "Mannon", "RobotMan"))); } @Test - public void canRetrieveByNameWhenDoesNotExist() { + void canRetrieveByNameWhenDoesNotExist() { // given given(superHeroRepository.getSuperHero("RobotMan")) .willReturn(Optional.empty()); @@ -91,7 +92,7 @@ public void canRetrieveByNameWhenDoesNotExist() { } @Test - public void canCreateANewSuperHero() { + void canCreateANewSuperHero() { // when ResponseEntity superHeroResponse = restTemplate.postForEntity("/superheroes/", new SuperHero("Rob", "Mannon", "RobotMan"), SuperHero.class); @@ -101,7 +102,7 @@ public void canCreateANewSuperHero() { } @Test - public void headerIsPresent() throws Exception { + void headerIsPresent() throws Exception { // when ResponseEntity superHeroResponse = restTemplate.getForEntity("/superheroes/2", SuperHero.class); diff --git a/src/test/resources/junit-platform.properties b/src/test/resources/junit-platform.properties new file mode 100644 index 0000000..6076b58 --- /dev/null +++ b/src/test/resources/junit-platform.properties @@ -0,0 +1,4 @@ +junit.jupiter.execution.parallel.enabled = true +junit.jupiter.execution.parallel.mode.default = concurrent +junit.jupiter.execution.parallel.mode.classes.default = concurrent +junit.jupiter.execution.parallel.config.fixed.parallelism = 5