JAVA-21298 Fix failing tests in spring-boot-testing module (#14289)

This commit is contained in:
anuragkumawat
2023-06-23 14:03:16 +05:30
committed by GitHub
parent b39cee5e7a
commit 462cfaf980
15 changed files with 161 additions and 30 deletions
@@ -8,7 +8,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
### Relevant Articles:
- [Testing with Spring and Spock](https://www.baeldung.com/spring-spock-testing)
- [Exclude Auto-Configuration Classes in Spring Boot Tests](https://www.baeldung.com/spring-boot-exclude-auto-configuration-test)
- [Embedded Redis Server with Spring Boot Test](https://www.baeldung.com/spring-embedded-redis)
- [Testing Spring Boot @ConfigurationProperties](https://www.baeldung.com/spring-boot-testing-configurationproperties)
@@ -67,19 +67,6 @@
<version>${redis.version}</version>
<scope>test</scope>
</dependency>
<!-- Spock & Spring -->
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>${spock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>
<version>${spock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -120,21 +107,6 @@
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${gmavenplus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
<configuration>
<targetBytecode>17</targetBytecode>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@@ -148,7 +120,7 @@
<start-class>com.baeldung.boot.Application</start-class>
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
<spock.version>2.4-M1-groovy-4.0</spock.version>
<gmavenplus-plugin.version>2.1.0</gmavenplus-plugin.version>
<gmavenplus-plugin.version>3.0.0</gmavenplus-plugin.version>
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
<redis.version>0.7.2</redis.version>
<spring-boot.version>2.5.0</spring-boot.version>
@@ -1,36 +0,0 @@
package com.baeldung.boot.controller.rest;
import java.util.Optional;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class WebController {
private String name;
@GetMapping
public String salutation() {
return "Hello " + Optional.ofNullable(name).orElse("world") + '!';
}
@PutMapping
@ResponseStatus(HttpStatus.NO_CONTENT)
public void setName(@RequestBody final String name) {
this.name = name;
}
@DeleteMapping
@ResponseStatus(HttpStatus.NO_CONTENT)
public void resetToDefault() {
this.name = null;
}
}
@@ -1,23 +0,0 @@
package com.baeldung.boot
import com.baeldung.boot.controller.rest.WebController
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import spock.lang.Narrative
import spock.lang.Specification
import spock.lang.Title
@Title("Application Specification")
@Narrative("Specification which beans are expected")
@SpringBootTest
class LoadContextTest extends Specification {
@Autowired(required = false)
private WebController webController
def "when context is loaded then all expected beans are created"() {
expect: "the WebController is created"
webController
}
}
@@ -1,46 +0,0 @@
package com.baeldung.boot
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import spock.lang.Narrative
import spock.lang.Specification
import spock.lang.Title
@Title("WebController Specification")
@Narrative("The Specification of the behaviour of the WebController. It can greet a person, change the name and reset it to 'world'")
@SpringBootTest
@AutoConfigureMockMvc
@EnableAutoConfiguration(exclude= SecurityAutoConfiguration.class)
class WebControllerTest extends Specification {
@Autowired
private MockMvc mvc
def "when get is performed then the response has status 200 and content is 'Hello world!'"() {
expect: "Status is 200 and the response is 'Hello world!'"
mvc.perform(MockMvcRequestBuilders.get("/hello")).andExpect(MockMvcResultMatchers.status().isOk()).andReturn().response.contentAsString == "Hello world!"
}
def "when set and delete are performed then the response has status 204 and content changes as expected"() {
given: "a new name"
def NAME = "Emmy"
when: "the name is set"
mvc.perform(MockMvcRequestBuilders.put("/hello").content(NAME)).andExpect(MockMvcResultMatchers.status().isNoContent())
then: "the salutation uses the new name"
mvc.perform(MockMvcRequestBuilders.get("/hello")).andExpect(MockMvcResultMatchers.status().isOk()).andReturn().response.contentAsString == "Hello $NAME!"
when: "the name is deleted"
mvc.perform(MockMvcRequestBuilders.delete("/hello")).andExpect(MockMvcResultMatchers.status().isNoContent())
then: "the salutation uses the default name"
mvc.perform(MockMvcRequestBuilders.get("/hello")).andExpect(MockMvcResultMatchers.status().isOk()).andReturn().response.contentAsString == "Hello world!"
}
}
@@ -0,0 +1,4 @@
validate.propertiesMap.first=prop1
validate.propertiesMap.second=prop2
validate.mail_config.address=user1@test