Skip to content

Commit

Permalink
Merge pull request #4 from mechero/sb2.5_java17
Browse files Browse the repository at this point in the history
Updated to Spring Boot 2.5, JUnit 5, Java 17
  • Loading branch information
mechero committed Oct 8, 2021
2 parents f13534e + 859087e commit 13a999d
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 62 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -14,14 +14,14 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>14</java.version>
<java.version>17</java.version>
</properties>

<dependencies>
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -4,7 +4,7 @@

This sample application made with Spring Boot is intended to show the different approach for testing, from Unit Tests with MockMVC in Standalone mode to full `@SpringBootTest` as Integration tests between the modules.

The complete guide is available on [The Practical Developer Blog](https://thepracticaldeveloper.com/2017/07/31/guide-spring-boot-controller-tests/).
The complete guide is available on [The Practical Developer Blog](https://thepracticaldeveloper.com/guide-spring-boot-controller-tests/).

## The application

Expand All @@ -29,4 +29,4 @@ Finally, `SuperHeroControllerSpringBootTest` shows how to write a `@SpringBootTe

![@SpringBootTest using context and web server](images/tests_springboot_wm.png)

To check conclusion and more information please visit [the blog](https://thepracticaldeveloper.com/2017/07/31/guide-spring-boot-controller-tests/).
To check conclusion and more information please visit [the blog](https://thepracticaldeveloper.com/guide-spring-boot-controller-tests/).
50 changes: 3 additions & 47 deletions src/main/java/io/tpd/superheroes/domain/SuperHero.java
@@ -1,50 +1,6 @@
package io.tpd.superheroes.domain;

public final class SuperHero {

private String firstName;
private String lastName;
private String heroName;

// Empty constructor is needed for Jackson to recreate the object from JSON
public SuperHero() {
}

public SuperHero(String firstName, String lastName, String heroName) {
this.firstName = firstName;
this.lastName = lastName;
this.heroName = heroName;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getHeroName() {
return heroName;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

SuperHero superHero = (SuperHero) o;

if (firstName != null ? !firstName.equals(superHero.firstName) : superHero.firstName != null) return false;
if (lastName != null ? !lastName.equals(superHero.lastName) : superHero.lastName != null) return false;
return heroName != null ? heroName.equals(superHero.heroName) : superHero.heroName == null;
}

@Override
public int hashCode() {
int result = firstName != null ? firstName.hashCode() : 0;
result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
result = 31 * result + (heroName != null ? heroName.hashCode() : 0);
return result;
}
public record SuperHero(String firstName,
String lastName,
String heroName) {
}
Expand Up @@ -34,7 +34,7 @@ public SuperHero getSuperHero(int id) {

@Override
public Optional<SuperHero> getSuperHero(String heroName) {
return superHeroList.stream().filter(h -> h.getHeroName().equals(heroName)).findAny();
return superHeroList.stream().filter(h -> h.heroName().equals(heroName)).findAny();
}

@Override
Expand Down
Expand Up @@ -4,7 +4,6 @@
import io.tpd.superheroes.exceptions.NonExistingHeroException;
import io.tpd.superheroes.repository.SuperHeroRepository;
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.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand All @@ -13,7 +12,6 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Optional;
Expand All @@ -28,7 +26,6 @@
*
* @author moises.macero
*/
@ExtendWith(SpringExtension.class)
@AutoConfigureJsonTesters
@WebMvcTest(SuperHeroController.class)
public class SuperHeroControllerMockMvcWithContextTest {
Expand Down
Expand Up @@ -4,7 +4,6 @@
import io.tpd.superheroes.exceptions.NonExistingHeroException;
import io.tpd.superheroes.repository.SuperHeroRepository;
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.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand All @@ -14,7 +13,6 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Optional;
Expand All @@ -30,7 +28,6 @@
*
* @author moises.macero
*/
@ExtendWith(SpringExtension.class)
@AutoConfigureJsonTesters
@SpringBootTest
@AutoConfigureMockMvc
Expand Down
Expand Up @@ -4,14 +4,12 @@
import io.tpd.superheroes.exceptions.NonExistingHeroException;
import io.tpd.superheroes.repository.SuperHeroRepository;
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.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.Optional;

Expand All @@ -21,11 +19,10 @@

/**
* This class demonstrates how to test a controller using Spring Boot Test
* (what makes it much closer to a Integration Test)
* (what makes it much closer to an Integration Test)
*
* @author moises.macero
*/
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SuperHeroControllerSpringBootTest {

Expand Down

0 comments on commit 13a999d

Please sign in to comment.