Merge remote-tracking branch 'upstream/master' into JAVA-8794

This commit is contained in:
Krzysiek
2021-12-16 12:21:43 +01:00
81 changed files with 1150 additions and 63 deletions
+1
View File
@@ -70,6 +70,7 @@
<module>spring-boot-swagger</module>
<module>spring-boot-swagger-jwt</module>
<module>spring-boot-testing</module>
<module>spring-boot-testing-2</module>
<module>spring-boot-vue</module>
<module>spring-boot-actuator</module>
<module>spring-boot-data-2</module>
@@ -5,4 +5,5 @@ This module contains articles about Spring Boot annotations
### Relevant Articles:
- [Spring Conditional Annotations](https://www.baeldung.com/spring-conditional-annotations)
- [Guide to @SpringBootConfiguration in Spring Boot](https://www.baeldung.com/springbootconfiguration-annotation)
- More articles: [[<-- prev]](/spring-boot-modules/spring-boot-annotations)
@@ -0,0 +1 @@
spring.main.web-application-type=none
@@ -0,0 +1,15 @@
package com.baeldung.springbootconfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringContextTest {
@Test
public void contextLoads() {
}
}
@@ -9,5 +9,4 @@ This module contains articles about bootstrapping Spring Boot applications.
- [Deploy a Spring Boot Application to Google App Engine](https://www.baeldung.com/spring-boot-google-app-engine)
- [Deploy a Spring Boot Application to OpenShift](https://www.baeldung.com/spring-boot-deploy-openshift)
- [Deploy a Spring Boot Application to AWS Beanstalk](https://www.baeldung.com/spring-boot-deploy-aws-beanstalk)
- [Guide to @SpringBootConfiguration in Spring Boot](https://www.baeldung.com/springbootconfiguration-annotation)
- [Implement Health Checks in OpenShift](https://www.baeldung.com/ops/openshift-health-checks)
@@ -1,6 +1,7 @@
package com.baeldung.prefix;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@@ -8,12 +9,12 @@ import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class PrefixController {
@Value(value = "${server.port}")
private int serverPort;
@Autowired
private Environment environment;
@GetMapping("/prefix")
public String getServerPortInfo(final Model model) {
model.addAttribute("serverPort", serverPort);
model.addAttribute("serverPort", environment.getProperty("server.port"));
return "prefix";
}
}
@@ -0,0 +1,13 @@
package com.baeldung.micronaut.vs.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@EnableAutoConfiguration
@ComponentScan("com.baeldung.micronaut.vs.springboot")
public class CompareApplication {
public static void main(final String[] args) {
SpringApplication.run(CompareApplication.class, args);
}
}
@@ -0,0 +1,60 @@
package com.baeldung.micronaut.vs.springboot.controller;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.micronaut.vs.springboot.service.ArithmeticService;
@RestController
@RequestMapping("/math")
public class ArithmeticController {
@Autowired
private ArithmeticService arithmeticService;
@GetMapping("/sum/{number1}/{number2}")
public float getSum(@PathVariable("number1") float number1, @PathVariable("number2") float number2) {
return arithmeticService.add(number1, number2);
}
@GetMapping("/subtract/{number1}/{number2}")
public float getDifference(@PathVariable("number1") float number1, @PathVariable("number2") float number2) {
return arithmeticService.subtract(number1, number2);
}
@GetMapping("/multiply/{number1}/{number2}")
public float getMultiplication(@PathVariable("number1") float number1, @PathVariable("number2") float number2) {
return arithmeticService.multiply(number1, number2);
}
@GetMapping("/divide/{number1}/{number2}")
public float getDivision(@PathVariable("number1") float number1, @PathVariable("number2") float number2) {
return arithmeticService.divide(number1, number2);
}
@GetMapping("/memory")
public String getMemoryStatus() {
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
String memoryStats = "";
String init = String.format("Initial: %.2f GB \n",
(double)memoryBean.getHeapMemoryUsage().getInit() /1073741824);
String usedHeap = String.format("Used: %.2f GB \n",
(double)memoryBean.getHeapMemoryUsage().getUsed() /1073741824);
String maxHeap = String.format("Max: %.2f GB \n",
(double)memoryBean.getHeapMemoryUsage().getMax() /1073741824);
String committed = String.format("Committed: %.2f GB \n",
(double)memoryBean.getHeapMemoryUsage().getCommitted() /1073741824);
memoryStats += init;
memoryStats += usedHeap;
memoryStats += maxHeap;
memoryStats += committed;
return memoryStats;
}
}
@@ -0,0 +1,25 @@
package com.baeldung.micronaut.vs.springboot.service;
import org.springframework.stereotype.Service;
@Service
public class ArithmeticService {
public float add(float number1, float number2) {
return number1 + number2;
}
public float subtract(float number1, float number2) {
return number1 - number2;
}
public float multiply(float number1, float number2) {
return number1 * number2;
}
public float divide(float number1, float number2) {
if (number2 == 0) {
throw new IllegalArgumentException("'number2' cannot be zero");
}
return number1 / number2;
}
}
@@ -0,0 +1,68 @@
package com.baeldung.micronaut.vs.springboot.controller;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.Test;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.hamcrest.Matchers.containsString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import com.baeldung.micronaut.vs.springboot.CompareApplication;
@SpringBootTest(classes = CompareApplication.class)
@AutoConfigureMockMvc
public class ArithmeticControllerUnitTest {
@Autowired
private MockMvc mockMvc;
@Test
public void givenTwoNumbers_whenAdd_thenCorrectAnswerReturned() throws Exception {
Float expected = Float.valueOf(10 + 20);
this.mockMvc.perform(MockMvcRequestBuilders.get("/math/sum/10/20")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(expected.toString()));
}
@Test
public void givenTwoNumbers_whenSubtract_thenCorrectAnswerReturned() throws Exception {
Float expected = Float.valueOf(20 - 10);
this.mockMvc.perform(MockMvcRequestBuilders.get("/math/subtract/20/10")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(expected.toString()));
}
@Test
public void givenTwoNumbers_whenMultiply_thenCorrectAnswerReturned() throws Exception {
Float expected = Float.valueOf(20 * 10);
this.mockMvc.perform(MockMvcRequestBuilders.get("/math/multiply/20/10")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(expected.toString()));
}
@Test
public void givenTwoNumbers_whenDivide_thenCorrectAnswerReturned() throws Exception {
Float expected = Float.valueOf(20 / 10);
this.mockMvc.perform(MockMvcRequestBuilders.get("/math/divide/20/10")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(expected.toString()));
}
@Test
public void whenMemory_thenMemoryStringReturned() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/math/memory")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(containsString("Initial:")));
}
}
@@ -0,0 +1,5 @@
/target/
.settings/
.classpath
.project
@@ -0,0 +1,12 @@
## Spring Boot Testing
This module contains articles about Spring Boot testing
### The Course
The "REST With Spring" Classes: http://bit.ly/restwithspring
### Relevant Articles:
- [Setting the Log Level in Spring Boot when Testing](https://www.baeldung.com/spring-boot-testing-log-level)
- More articles: [[<-- prev]](../spring-boot-testing)
@@ -0,0 +1,33 @@
<?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>
<artifactId>spring-boot-testing-2</artifactId>
<name>spring-boot-testing-2</name>
<packaging>jar</packaging>
<description>This is simple boot application for demonstrating testing features.</description>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<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>
<properties>
<start-class>com.baeldung.boot.Application</start-class>
</properties>
</project>
@@ -0,0 +1,12 @@
package com.baeldung.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@@ -0,0 +1,15 @@
package com.baeldung.boot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}
@@ -0,0 +1,3 @@
# logging.level.com.baeldung.testloglevel=DEBUG
# logging.level.root=INFO
@@ -0,0 +1,13 @@
<configuration>
<include resource="/org/springframework/boot/logging/logback/base.xml"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="error">
<appender-ref ref="STDOUT"/>
</root>
<logger name="com.baeldung.testloglevel" level="debug"/>
</configuration>
@@ -10,9 +10,9 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [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)
- [Setting the Log Level in Spring Boot when Testing](https://www.baeldung.com/spring-boot-testing-log-level)
- [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)
- [Prevent ApplicationRunner or CommandLineRunner Beans From Executing During Junit Testing](https://www.baeldung.com/spring-junit-prevent-runner-beans-testing-execution)
- [Testing in Spring Boot](https://www.baeldung.com/spring-boot-testing)
- [Fixing the NoSuchMethodError JUnit Error](https://www.baeldung.com/junit-nosuchmethoderror)
- More articles: [[more -->]](../spring-boot-testing-2)
@@ -9,5 +9,4 @@
<root level="error">
<appender-ref ref="STDOUT"/>
</root>
<logger name="com.baeldung.testloglevel" level="debug"/>
</configuration>