Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
673944f
commit e20ea05
Showing
2 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
junit5-nested-tests/src/test/java/com/arhohuttunen/junit5/MoneyWithoutNestedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.arhohuttunen.junit5; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class MoneyWithoutNestedTest { | ||
@Test | ||
@DisplayName("monies with same amounts and currency are equal") | ||
void moniesWithSameAmountsAndCurrencyAreEqual() { | ||
CurrencyUnit eur = CurrencyUnit.of("EUR"); | ||
Money first = Money.of(eur, 3.99); | ||
Money second = Money.of(eur, 3.99); | ||
|
||
assertEquals(second, first); | ||
} | ||
|
||
@Test | ||
@DisplayName("monies with different amounts are not equal") | ||
void moniesWithDifferentAmountsAreNotEqual() { | ||
CurrencyUnit eur = CurrencyUnit.of("EUR"); | ||
Money first = Money.of(eur, 3.99); | ||
Money second = Money.of(eur, 3.89); | ||
|
||
assertNotEquals(second, first); | ||
} | ||
|
||
@Test | ||
@DisplayName("monies with different currencies are not equal") | ||
void moniesWithDifferentCurrenciesAreNotEqual() { | ||
CurrencyUnit eur = CurrencyUnit.of("EUR"); | ||
CurrencyUnit usd = CurrencyUnit.of("USD"); | ||
Money first = Money.of(eur, 3.99); | ||
Money second = Money.of(usd, 3.99); | ||
|
||
assertNotEquals(second, first); | ||
} | ||
|
||
@Test | ||
@DisplayName("can add monies of same currency") | ||
void addMoneyWithSameCurrency() { | ||
CurrencyUnit eur = CurrencyUnit.of("EUR"); | ||
Money money = Money.of(eur, 4.25); | ||
Money addend = Money.of(eur, 1.4); | ||
|
||
Money sum = money.add(addend); | ||
|
||
assertEquals(BigDecimal.valueOf(5.65), sum.getAmount()); | ||
} | ||
|
||
@Test | ||
@DisplayName("cannot add monies of different currency") | ||
void addMoneyWithDifferentCurrency() { | ||
CurrencyUnit eur = CurrencyUnit.of("EUR"); | ||
CurrencyUnit usd = CurrencyUnit.of("USD"); | ||
Money money = Money.of(eur, 4.25); | ||
Money addend = Money.of(usd, 1.4); | ||
|
||
assertThrows(CurrencyMismatchException.class, () -> money.add(addend)); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
...ested-tests/src/test/java/com/arhohuttunen/junit5/ProductControllerWithoutNestedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.arhohuttunen.junit5; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WebMvcTest | ||
class ProductControllerWithoutNestedTest { | ||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@MockBean | ||
private ProductRepository productRepository; | ||
|
||
@Test | ||
@DisplayName("POST returns HTTP status Bad Request when fields are missing") | ||
void postReturnsHttpStatusBadRequestWhenFieldsAreMissing() throws Exception { | ||
Product product = new Product(null, null, null); | ||
|
||
mockMvc.perform(post("/product") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(product))) | ||
.andExpect(status().isBadRequest()); | ||
} | ||
|
||
@Test | ||
@DisplayName("POST does not create a product when fields are missing") | ||
void postDoesNotCreateProductWhenFieldsAreMissing() throws Exception { | ||
Product product = new Product(null, null, null); | ||
|
||
mockMvc.perform(post("/product") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(product))) | ||
.andExpect(status().isBadRequest()); | ||
|
||
verify(productRepository, never()).save(product); | ||
} | ||
|
||
@Test | ||
@DisplayName("POST returns HTTP status Created when fields are valid") | ||
void postReturnsHttpStatusCreatedWhenFieldsAreValid() throws Exception { | ||
Product product = new Product(1L, "Toothbrush", BigDecimal.valueOf(5.0)); | ||
|
||
mockMvc.perform(post("/product") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(product))) | ||
.andExpect(status().isCreated()); | ||
} | ||
|
||
@Test | ||
@DisplayName("GET returns HTTP status Not Found when product is not found") | ||
void getReturnsHttpStatusNotFoundWhenProductIsNotFound() throws Exception { | ||
doThrow(new ProductNotFoundException()).when(productRepository).findById(1L); | ||
|
||
mockMvc.perform(get("/product/{productId}", 1L)) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
@DisplayName("GET returns HTTP status OK when product is found") | ||
void getReturnsHttpStatusOkWhenProductIsFound() throws Exception { | ||
mockMvc.perform(get("/product/{productId}", 1L)) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
@DisplayName("GET returns found product as JSON when product is found") | ||
void getReturnsFoundProductAsJson() throws Exception { | ||
Product product = new Product(1L, "Toothbrush", BigDecimal.valueOf(5.0)); | ||
when(productRepository.findById(1L)).thenReturn(product); | ||
|
||
mockMvc.perform(get("/product/{productId}", 1L)) | ||
.andExpect(jsonPath("$.id", is(1))) | ||
.andExpect(jsonPath("$.name", is("Toothbrush"))) | ||
.andExpect(jsonPath("$.price", is(5.0))); | ||
} | ||
|
||
@Test | ||
@DisplayName("DELETE returns HTTP status Not Found when product is not found") | ||
void deleteReturnsHttpStatusNotFoundWhenProductIsNotFound() throws Exception { | ||
doThrow(new ProductNotFoundException()).when(productRepository).deleteById(1L); | ||
|
||
mockMvc.perform(delete("/product/{productId}", 1L)) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
@DisplayName("DELETE returns HTTP status No Content when product is found") | ||
void deleteReturnsHttpStatusNoContentWhenProductIsFound() throws Exception { | ||
mockMvc.perform(delete("/product/{productId}", 1L)) | ||
.andExpect(status().isNoContent()); | ||
} | ||
} |