* 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 4a817cbdd9
commit d038cf4cb7
11 changed files with 385 additions and 1 deletions

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