* BAEL-4331

* Add an integration test
This commit is contained in:
Amy DeGregorio 2021-02-08 21:11:25 -05:00 committed by GitHub
parent d89a51b555
commit 6c5c6fe317
8 changed files with 169 additions and 0 deletions

View File

@ -62,6 +62,7 @@
<module>spring-boot-properties-3</module>
<module>spring-boot-property-exp</module>
<module>spring-boot-runtime</module>
<module>spring-boot-runtime-2</module>
<module>spring-boot-security</module>
<module>spring-boot-springdoc</module>
<module>spring-boot-swagger</module>

View File

@ -0,0 +1,6 @@
## Spring Boot Runtime 2
This module contains articles about administering a Spring Boot runtime
### Relevant Articles:
-

View File

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<artifactId>spring-boot-runtime-2</artifactId>
<packaging>jar</packaging>
<name>spring-boot-runtime-2</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources/heap</directory>
<targetPath>${project.build.directory}</targetPath>
<filtering>true</filtering>
<includes>
<include>${project.name}.conf</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<configuration>
<mainClass>com.baeldung.heap.HeapSizeDemoApplication</mainClass>
</configuration>
</execution>
</executions>
<configuration>
<executable>true</executable>
<jvmArguments>
-Xms256m
-Xmx1g
</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,12 @@
package com.baeldung.heap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HeapSizeDemoApplication {
public static void main(String[] args) {
SpringApplication.run(HeapSizeDemoApplication.class, args);
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.heap;
public class MemoryStats {
private long heapSize;
private long heapMaxSize;
private long heapFreeSize;
public long getHeapSize() {
return heapSize;
}
public void setHeapSize(long heapSize) {
this.heapSize = heapSize;
}
public long getHeapMaxSize() {
return heapMaxSize;
}
public void setHeapMaxSize(long heapMaxSize) {
this.heapMaxSize = heapMaxSize;
}
public long getHeapFreeSize() {
return heapFreeSize;
}
public void setHeapFreeSize(long heapFreeSize) {
this.heapFreeSize = heapFreeSize;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.heap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MemoryStatusController {
@GetMapping("memory-status")
public MemoryStats getMemoryStatistics() {
MemoryStats stats = new MemoryStats();
stats.setHeapSize(Runtime.getRuntime()
.totalMemory());
stats.setHeapMaxSize(Runtime.getRuntime()
.maxMemory());
stats.setHeapFreeSize(Runtime.getRuntime()
.freeMemory());
return stats;
}
}

View File

@ -0,0 +1 @@
JAVA_OPTS="-Xms512m -Xmx1024m"

View File

@ -0,0 +1,32 @@
package com.baeldung.heap;
import static org.hamcrest.Matchers.notANumber;
import static org.hamcrest.Matchers.not;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@RunWith(SpringRunner.class)
@WebMvcTest(MemoryStatusController.class)
public class MemoryStatusControllerIntegrationTest {
@Autowired
private MockMvc mvc;
@Test
public void whenGetMemoryStatistics_thenReturnJsonArray() throws Exception {
mvc.perform(get("/memory-status").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("heapSize", not(notANumber())))
.andExpect(jsonPath("heapMaxSize", not(notANumber())))
.andExpect(jsonPath("heapFreeSize", not(notANumber())));
}
}