* BAEL-4811

* BAEL-4811 Add unit tests
This commit is contained in:
Amy DeGregorio 2021-12-14 13:15:43 -05:00 committed by GitHub
parent 53b9df34a2
commit 1bf2293e0b
11 changed files with 385 additions and 1 deletions

View File

@ -135,7 +135,8 @@
</build>
<properties>
<exec.mainClass>com.baeldung.micronaut.helloworld.server.ServerApplication</exec.mainClass>
<!-- <exec.mainClass>com.baeldung.micronaut.helloworld.server.ServerApplication</exec.mainClass> -->
<exec.mainClass>com.baeldung.micronaut.vs.springboot.CompareApplication</exec.mainClass>
<micronaut.version>1.0.0.RC2</micronaut.version>
<jdk.version>1.8</jdk.version>
<annotation.api.version>1.3.2</annotation.api.version>

View File

@ -0,0 +1,9 @@
package com.baeldung.micronaut.vs.springboot;
import io.micronaut.runtime.Micronaut;
public class CompareApplication {
public static void main(String[] args) {
Micronaut.run(CompareApplication.class);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.micronaut.vs.springboot.client;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.client.annotation.Client;
@Client("/math")
public interface ArithmeticClient {
@Get("/sum/{number1}/{number2}")
String sum(float number1, float number2);
@Get("/subtract/{number1}/{number2}")
String subtract(float number1, float number2);
@Get("/multiply/{number1}/{number2}")
String multiply(float number1, float number2);
@Get("/divide/{number1}/{number2}")
String divide(float number1, float number2);
@Get("/memory")
String memory();
}

View File

@ -0,0 +1,41 @@
package com.baeldung.micronaut.vs.springboot.client;
import javax.inject.Singleton;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.RxHttpClient;
import io.micronaut.http.client.annotation.Client;
@Singleton
public class ArithmeticClientImpl {
private RxHttpClient httpClient;
public ArithmeticClientImpl(@Client("/") RxHttpClient httpClient) {
this.httpClient = httpClient;
}
public String sum(float number1, float number2) {
HttpRequest<String> req = HttpRequest.GET("/math/sum/" + number1 + "/" + number2);
return httpClient.retrieve(req).blockingFirst();
}
public String subtract(float number1, float number2) {
HttpRequest<String> req = HttpRequest.GET("/math/subtract/" + number1 + "/" + number2);
return httpClient.retrieve(req).blockingFirst();
}
public String multiply(float number1, float number2) {
HttpRequest<String> req = HttpRequest.GET("/math/multiply/" + number1 + "/" + number2);
return httpClient.retrieve(req).blockingFirst();
}
public String divide(float number1, float number2) {
HttpRequest<String> req = HttpRequest.GET("/math/divide/" + number1 + "/" + number2);
return httpClient.retrieve(req).blockingFirst();
}
public String memory() {
HttpRequest<String> req = HttpRequest.GET("/math/memory");
return httpClient.retrieve(req).blockingFirst();
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.micronaut.vs.springboot.controller;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import javax.inject.Inject;
import com.baeldung.micronaut.vs.springboot.service.ArithmeticService;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
@Controller("/math")
public class ArithmeticController {
@Inject
private ArithmeticService arithmeticService;
@Get("/sum/{number1}/{number2}")
public float getSum(float number1, float number2) {
return arithmeticService.add(number1, number2);
}
@Get("/subtract/{number1}/{number2}")
public float getDifference(float number1, float number2) {
return arithmeticService.subtract(number1, number2);
}
@Get("/multiply/{number1}/{number2}")
public float getMultiplication(float number1, float number2) {
return arithmeticService.multiply(number1, number2);
}
@Get("/divide/{number1}/{number2}")
public float getDivision(float number1, float number2) {
return arithmeticService.divide(number1, number2);
}
@Get("/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;
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.micronaut.vs.springboot.service;
import javax.inject.Singleton;
@Singleton
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;
}
}

View File

@ -0,0 +1,61 @@
package com.baeldung.micronaut.vs.springboot;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import io.micronaut.context.ApplicationContext;
import io.micronaut.runtime.server.EmbeddedServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.baeldung.micronaut.vs.springboot.client.ArithmeticClientImpl;
public class ArithmeticClientUnitTest {
private EmbeddedServer server;
private ArithmeticClientImpl client;
@Before
public void setup() {
server = ApplicationContext.run(EmbeddedServer.class);
client = server.getApplicationContext().getBean(ArithmeticClientImpl.class);
}
@After
public void cleanup() {
server.stop();
}
@Test
public void givenTwoNumbers_whenAdd_thenCorrectAnswerReturned() {
String expected = Float.valueOf(10 + 20).toString();
assertEquals(expected, client.sum(10, 20));
}
@Test
public void givenTwoNumbers_whenSubtract_thenCorrectAnswerReturned() {
String expected = Float.valueOf(20 - 10).toString();
assertEquals(expected, client.subtract(20, 10));
}
@Test
public void givenTwoNumbers_whenMultiply_thenCorrectAnswerReturned() {
String expected = Float.valueOf(10 * 20).toString();
assertEquals(expected, client.multiply(10, 20));
}
@Test
public void givenTwoNumbers_whenDivide_thenCorrectAnswerReturned() {
String expected = Float.valueOf(30 / 10).toString();
assertEquals(expected, client.divide(30, 10));
}
@Test
public void whenMemory_thenCorrectAnswerReturned() {
String expected = "Initial:";
assertThat(client.memory(), containsString(expected));
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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:")));
}
}