-
+
diff --git a/src/test/java/tech/zerofiltre/testing/calcul/controller/CalculatorControllerSIT.java b/src/test/java/tech/zerofiltre/testing/calcul/controller/CalculatorControllerSIT.java
index 382bbe7..a9a26ca 100644
--- a/src/test/java/tech/zerofiltre/testing/calcul/controller/CalculatorControllerSIT.java
+++ b/src/test/java/tech/zerofiltre/testing/calcul/controller/CalculatorControllerSIT.java
@@ -1,57 +1,73 @@
package tech.zerofiltre.testing.calcul.controller;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import javax.inject.Inject;
-
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
-import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
-
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import tech.zerofiltre.testing.calcul.domain.Calculator;
+import tech.zerofiltre.testing.calcul.domain.model.CalculationModel;
+import tech.zerofiltre.testing.calcul.repository.CalculationModelRepository;
import tech.zerofiltre.testing.calcul.service.CalculatorService;
+import tech.zerofiltre.testing.calcul.service.CalculatorServiceImpl;
import tech.zerofiltre.testing.calcul.service.SolutionFormatter;
-@WebMvcTest(controllers = { CalculatorController.class, CalculatorService.class })
+@Disabled
@ExtendWith(SpringExtension.class)
-public class CalculatorControllerSIT {
-
- @Inject
- private MockMvc mockMvc;
-
- @MockBean
- private SolutionFormatter solutionFormatter;
-
- @MockBean
- private Calculator calculator;
-
- @Test
- public void givenACalculatorApp_whenRequestToAdd_thenSolutionIsShown() throws Exception {
- // GIVEN
- when(calculator.add(2, 3)).thenReturn(5);
-
- // WHEN
- final MvcResult result = mockMvc.perform(
- MockMvcRequestBuilders.post("/calculator")
- .param("leftArgument", "2")
- .param("rightArgument", "3")
- .param("calculationType", "ADDITION"))
- .andExpect(MockMvcResultMatchers.status().is2xxSuccessful())
- .andReturn();
-
- // THEN
- assertThat(result.getResponse().getContentAsString())
- .contains("id=\"solution\"")
- .contains(">55 actualDigits = calculatorUnderTest.digitsSet(number);
assertThat(actualDigits).containsExactlyInAnyOrder(1, 2, 3, 4);
}
@Test
- public void listDigits_shouldReturnsTheListOfZero_ofZero() {
+ void listDigits_shouldReturnsTheListOfZero_ofZero() {
final int number = 0;
final Set actualDigits = calculatorUnderTest.digitsSet(number);
assertThat(actualDigits).containsExactly(0);
@@ -158,7 +158,7 @@ public void listDigits_shouldReturnsTheListOfZero_ofZero() {
@Disabled("Stoppé car cela échoue tous les mardis")
@Test
- public void testDate() {
+ void testDate() {
// GIVEN
final LocalDateTime dateTime = LocalDateTime.now();
@@ -169,7 +169,7 @@ public void testDate() {
}
@Test
- public void fact12_shouldReturnsTheCorrectAnswer() {
+ void fact12_shouldReturnsTheCorrectAnswer() {
// GIVEN
final int number = 12;
@@ -182,7 +182,7 @@ public void fact12_shouldReturnsTheCorrectAnswer() {
}
@Test
- public void digitsSetOfFact12_shouldReturnsTheCorrectAnswser() {
+ void digitsSetOfFact12_shouldReturnsTheCorrectAnswser() {
// GIVEN
final int cacheFactorial = 479001600;
@@ -194,7 +194,7 @@ public void digitsSetOfFact12_shouldReturnsTheCorrectAnswser() {
}
@Test
- public void multiplyAndDivide_shouldBeIdentity() {
+ void multiplyAndDivide_shouldBeIdentity() {
// GIVEN
final Random r = new Random();
final int a = 1 + r.nextInt(100);
diff --git a/src/test/java/tech/zerofiltre/testing/calcul/domain/ConversionCalculatorTest.java b/src/test/java/tech/zerofiltre/testing/calcul/domain/ConversionCalculatorTest.java
index 1fb80e8..188e0a8 100644
--- a/src/test/java/tech/zerofiltre/testing/calcul/domain/ConversionCalculatorTest.java
+++ b/src/test/java/tech/zerofiltre/testing/calcul/domain/ConversionCalculatorTest.java
@@ -12,7 +12,7 @@
@Tag("ConversionTests")
@DisplayName("Réussir à convertir entre différentes unités.")
-public class ConversionCalculatorTest {
+ class ConversionCalculatorTest {
private ConversionCalculator calculatorUnderTest = new ConversionCalculator();
@@ -22,14 +22,14 @@ public class ConversionCalculatorTest {
class TemperatureTests {
@Test
@DisplayName("Soit une T° à 0°C, lorsque l'on convertit en °F, alors on obtient 32°F.")
- public void celsiusToFahrenheit_returnsAFahrenheitTempurature_whenCelsiusIsZero() {
+ void celsiusToFahrenheit_returnsAFahrenheitTempurature_whenCelsiusIsZero() {
Double actualFahrenheit = calculatorUnderTest.celsiusToFahrenheit(0.0);
assertThat(actualFahrenheit).isCloseTo(32.0, withinPercentage(0.01));
}
@Test
@DisplayName("Soit une T° à 32°F, lorsque l'on convertit en °C, alors on obtient 0°C.")
- public void fahrenheitToCelsius_returnsZeroCelciusTempurature_whenThirtyTwo() {
+ void fahrenheitToCelsius_returnsZeroCelciusTempurature_whenThirtyTwo() {
Double actualCelsius = calculatorUnderTest.fahrenheitToCelsius(32.0);
assertThat(actualCelsius).isCloseTo(0.0, withinPercentage(0.01));
}
@@ -37,14 +37,14 @@ public void fahrenheitToCelsius_returnsZeroCelciusTempurature_whenThirtyTwo() {
@Test
@DisplayName("Soit un volume de 3.78541 litres, en gallons, on obtient 1 gallon.")
- public void litresToGallons_returnsOneGallon_whenConvertingTheEquivalentLitres() {
+ void litresToGallons_returnsOneGallon_whenConvertingTheEquivalentLitres() {
Double actualLitres = calculatorUnderTest.litresToGallons(3.78541);
assertThat(actualLitres).isCloseTo(1.0, withinPercentage(0.01));
}
@Test
@DisplayName("L'aire d'un disque de rayon 1 doit valoir PI.")
- public void radiusToAreaOfCircle_returnsPi_whenWeHaveARadiusOfOne() {
+ void radiusToAreaOfCircle_returnsPi_whenWeHaveARadiusOfOne() {
Double actualArea = calculatorUnderTest.radiusToAreaOfCircle(1.0);
assertThat(actualArea).isCloseTo(PI, withinPercentage(0.01));
}
diff --git a/src/test/java/tech/zerofiltre/testing/calcul/domain/DoubleCalculatorTest.java b/src/test/java/tech/zerofiltre/testing/calcul/domain/DoubleCalculatorTest.java
index 31abf37..a86975f 100644
--- a/src/test/java/tech/zerofiltre/testing/calcul/domain/DoubleCalculatorTest.java
+++ b/src/test/java/tech/zerofiltre/testing/calcul/domain/DoubleCalculatorTest.java
@@ -4,17 +4,17 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
-public class DoubleCalculatorTest {
+ class DoubleCalculatorTest {
private Calculator calculatorUnderTest;
@BeforeEach
- public void initCalculator() {
+ void initCalculator() {
calculatorUnderTest = new Calculator();
}
@Test
- @Disabled("Test ambigu et hors limite du type double")
- public void subTwoDoubleNumbers_shouldReturnsTheCorrectAnswer() {
+ @Disabled("Test ambigue et hors limite du type double")
+ void subTwoDoubleNumbers_shouldReturnsTheCorrectAnswer() {
// GIVEN
// WHEN
diff --git a/src/test/java/tech/zerofiltre/testing/calcul/domain/StatisticsCalculatorTest.java b/src/test/java/tech/zerofiltre/testing/calcul/domain/StatisticsCalculatorTest.java
index a798cd2..06df414 100644
--- a/src/test/java/tech/zerofiltre/testing/calcul/domain/StatisticsCalculatorTest.java
+++ b/src/test/java/tech/zerofiltre/testing/calcul/domain/StatisticsCalculatorTest.java
@@ -16,7 +16,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
-public class StatisticsCalculatorTest {
+ class StatisticsCalculatorTest {
@Spy
IntSummaryStatistics summaryStatistics = new IntSummaryStatistics();
@@ -24,12 +24,12 @@ public class StatisticsCalculatorTest {
StatisticsCalculator underTest;
@BeforeEach
- public void setUp() {
+ void setUp() {
underTest = new StatisticsCalculator(summaryStatistics);
}
@Test
- public void average_shouldSample_allIntegersProvided() {
+ void average_shouldSample_allIntegersProvided() {
final ArgumentCaptor sampleCaptor = ArgumentCaptor.forClass(Integer.class);
final List samples = Arrays.asList(2, 8, 5, 3, 7);
@@ -41,7 +41,7 @@ public void average_shouldSample_allIntegersProvided() {
}
@Test
- public void average_shouldReturnTheMean_ofAListOfIntegers() {
+ void average_shouldReturnTheMean_ofAListOfIntegers() {
final List samples = Arrays.asList(2, 8, 5, 3, 7);
final Integer result = underTest.average(samples);
diff --git a/src/test/java/tech/zerofiltre/testing/calcul/e2e/MultiplicationJourneyE2E.java b/src/test/java/tech/zerofiltre/testing/calcul/e2e/MultiplicationJourneyE2E.java
new file mode 100644
index 0000000..24eac6f
--- /dev/null
+++ b/src/test/java/tech/zerofiltre/testing/calcul/e2e/MultiplicationJourneyE2E.java
@@ -0,0 +1,72 @@
+package tech.zerofiltre.testing.calcul.e2e;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import io.github.bonigarcia.wdm.WebDriverManager;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.openqa.selenium.By;
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.firefox.FirefoxDriver;
+import org.openqa.selenium.support.ui.ExpectedConditions;
+import org.openqa.selenium.support.ui.WebDriverWait;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
+import org.springframework.boot.web.server.LocalServerPort;
+
+@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
+class MultiplicationJourneyE2E {
+
+ @LocalServerPort
+ private int port;
+
+ private WebDriver webDriver;
+ private String baseUrl;
+
+ @BeforeAll
+ static void setUpFireFoxDriver() {
+ WebDriverManager.firefoxdriver().setup();
+ }
+
+ @BeforeEach
+ void setUpWebDriver() {
+ webDriver = new FirefoxDriver();
+ baseUrl = "http://localhost:" + port + "/calculator";
+
+ }
+
+ @AfterEach
+ void quitWebDriver() {
+ if (webDriver != null) {
+ webDriver.quit();
+ }
+ }
+
+ @Test
+ void multiplyTwoBySixteenMustReturn32() {
+
+ //GIVEN
+ webDriver.get(baseUrl);
+ WebElement leftField = webDriver.findElement(By.id("left"));
+ WebElement typeDropDown = webDriver.findElement(By.id("type"));
+ WebElement rightField = webDriver.findElement(By.id("right"));
+ WebElement submitButton = webDriver.findElement(By.id("submit"));
+
+ //WHEN
+ leftField.sendKeys("2");
+ typeDropDown.sendKeys("x");
+ rightField.sendKeys("16");
+ submitButton.click();
+
+ //THEN
+ WebDriverWait waiter = new WebDriverWait(webDriver, 5);
+ WebElement solutionElement = waiter.until(ExpectedConditions.presenceOfElementLocated(By.id("solution")));
+ String solution = solutionElement.getText();
+ assertThat(solution).isEqualTo("32");
+ }
+
+
+}
diff --git a/src/test/java/tech/zerofiltre/testing/calcul/repository/CalculationModelRepositorySIT.java b/src/test/java/tech/zerofiltre/testing/calcul/repository/CalculationModelRepositorySIT.java
new file mode 100644
index 0000000..955100d
--- /dev/null
+++ b/src/test/java/tech/zerofiltre/testing/calcul/repository/CalculationModelRepositorySIT.java
@@ -0,0 +1,24 @@
+package tech.zerofiltre.testing.calcul.repository;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
+import tech.zerofiltre.testing.calcul.domain.model.CalculationModel;
+import tech.zerofiltre.testing.calcul.domain.model.CalculationType;
+
+@DataJpaTest
+public class CalculationModelRepositorySIT {
+
+ @Autowired
+ CalculationModelRepository calculationModelRepository;
+
+ @Test
+ void save_mustGenerateId() {
+ CalculationModel calculationModel = new CalculationModel(CalculationType.ADDITION, 1, 2, 3);
+ calculationModelRepository.save(calculationModel);
+ assertThat(calculationModel.getId()).isNotNull();
+ }
+
+}
diff --git a/src/test/java/tech/zerofiltre/testing/calcul/service/BatchCalculatorServiceTest.java b/src/test/java/tech/zerofiltre/testing/calcul/service/BatchCalculatorServiceTest.java
index 55f708f..89e9b91 100644
--- a/src/test/java/tech/zerofiltre/testing/calcul/service/BatchCalculatorServiceTest.java
+++ b/src/test/java/tech/zerofiltre/testing/calcul/service/BatchCalculatorServiceTest.java
@@ -25,7 +25,7 @@
import tech.zerofiltre.testing.calcul.domain.model.CalculationType;
@ExtendWith(MockitoExtension.class)
-public class BatchCalculatorServiceTest {
+ class BatchCalculatorServiceTest {
@Mock
CalculatorService calculatorService;
@@ -35,7 +35,7 @@ public class BatchCalculatorServiceTest {
BatchCalculatorService batchCalculatorServiceNoMock;
@BeforeEach
- public void init() {
+ void init() {
batchCalculatorService = new BatchCalculatorServiceImpl(calculatorService);
batchCalculatorServiceNoMock = new BatchCalculatorServiceImpl(
@@ -44,7 +44,7 @@ public void init() {
}
@Test
- public void givenOperationsList_whenbatchCalculate_thenReturnsCorrectAnswerList()
+ void givenOperationsList_whenbatchCalculate_thenReturnsCorrectAnswerList()
throws IOException, URISyntaxException {
// GIVEN
final Stream operations = Arrays.asList("2 + 2", "5 - 4", "6 x 8", "9 / 3").stream();
@@ -57,7 +57,7 @@ public void givenOperationsList_whenbatchCalculate_thenReturnsCorrectAnswerList(
}
@Test
- public void givenOperationsList_whenbatchCalculate_thenCallsServiceWithCorrectArguments() {
+ void givenOperationsList_whenbatchCalculate_thenCallsServiceWithCorrectArguments() {
// GIVEN
final Stream operations = Arrays.asList("2 + 2", "5 - 4", "6 x 8", "9 / 3").stream();
final ArgumentCaptor calculationModelCaptor = ArgumentCaptor.forClass(CalculationModel.class);
@@ -79,7 +79,7 @@ public void givenOperationsList_whenbatchCalculate_thenCallsServiceWithCorrectAr
}
@Test
- public void givenOperationsList_whenbatchCalculate_thenCallsServiceAndReturnsAnswer() {
+ void givenOperationsList_whenbatchCalculate_thenCallsServiceAndReturnsAnswer() {
// GIVEN
final Stream operations = Arrays.asList("2 + 2", "5 - 4", "6 x 8", "9 / 3").stream();
when(calculatorService.calculate(any(CalculationModel.class)))
@@ -113,7 +113,7 @@ public void givenOperationsList_whenbatchCalculate_thenCallsServiceAndReturnsAns
}
@Test
- public void givenOperationsList_whenbatchCalculate_thenCallsServiceAndReturnsAnswer2() {
+ void givenOperationsList_whenbatchCalculate_thenCallsServiceAndReturnsAnswer2() {
// GIVEN
final Stream operations = Arrays.asList("2 + 2", "5 - 4", "6 x 8", "9 / 3").stream();
when(calculatorService.calculate(any(CalculationModel.class)))
diff --git a/src/test/java/tech/zerofiltre/testing/calcul/service/CalculatorServiceIT.java b/src/test/java/tech/zerofiltre/testing/calcul/service/CalculatorServiceIT.java
index 18f204b..dc6a2c3 100644
--- a/src/test/java/tech/zerofiltre/testing/calcul/service/CalculatorServiceIT.java
+++ b/src/test/java/tech/zerofiltre/testing/calcul/service/CalculatorServiceIT.java
@@ -3,29 +3,28 @@
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
-
import tech.zerofiltre.testing.calcul.domain.Calculator;
import tech.zerofiltre.testing.calcul.domain.model.CalculationModel;
import tech.zerofiltre.testing.calcul.domain.model.CalculationType;
-public class CalculatorServiceIT {
+class CalculatorServiceIT {
- // Mettre en place des objets réels non mockés
- private final Calculator calculator = new Calculator();
- private final SolutionFormatter formatter = new SolutionFormatterImpl();
+ // Mettre en place des objets réels non mockés
+ private final Calculator calculator = new Calculator();
+ private final SolutionFormatter formatter = new SolutionFormatterImpl();
- // Initialiser la classe à tester
- private final CalculatorService underTest = new CalculatorServiceImpl(calculator, formatter);
+ // Initialiser la classe à tester
+ private final CalculatorService underTest = new CalculatorServiceImpl(calculator, formatter);
- @Test
- public void calculatorService_shouldCalculateASolution_whenGivenACalculationModel() {
- // GIVEN
- final CalculationModel calculation = new CalculationModel(CalculationType.ADDITION,
- 100, 101);
- // WHEN
- final CalculationModel result = underTest.calculate(calculation);
+ @Test
+ void calculatorService_shouldCalculateASolution_whenGivenACalculationModel() {
+ // GIVEN
+ final CalculationModel calculation = new CalculationModel(CalculationType.ADDITION,
+ 100, 101);
+ // WHEN
+ final CalculationModel result = underTest.calculate(calculation);
- // THEN
- assertThat(result.getSolution()).isEqualTo(201);
- }
+ // THEN
+ assertThat(result.getSolution()).isEqualTo(201);
+ }
}
diff --git a/src/test/java/tech/zerofiltre/testing/calcul/service/CalculatorServiceTest.java b/src/test/java/tech/zerofiltre/testing/calcul/service/CalculatorServiceTest.java
index 8c18448..11334d6 100644
--- a/src/test/java/tech/zerofiltre/testing/calcul/service/CalculatorServiceTest.java
+++ b/src/test/java/tech/zerofiltre/testing/calcul/service/CalculatorServiceTest.java
@@ -9,133 +9,131 @@
import static org.mockito.Mockito.when;
import java.util.Random;
-
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-
import tech.zerofiltre.testing.calcul.domain.Calculator;
import tech.zerofiltre.testing.calcul.domain.model.CalculationModel;
import tech.zerofiltre.testing.calcul.domain.model.CalculationType;
@ExtendWith(MockitoExtension.class)
-public class CalculatorServiceTest {
-
- @Mock
- Calculator calculator;
-
- @Mock
- SolutionFormatter solutionFormatter;
-
- CalculatorService classUnderTest;
-
- @BeforeEach
- public void init() {
- classUnderTest = new CalculatorServiceImpl(calculator, solutionFormatter);
- }
-
- @Test
- public void calculate_shouldUseCalculator_forAddition() {
- // GIVEN
- when(calculator.add(1, 2)).thenReturn(3);
-
- // WHEN
- final int result = classUnderTest.calculate(
- new CalculationModel(CalculationType.ADDITION, 1, 2)).getSolution();
-
- // THEN
- verify(calculator).add(1, 2);
- assertThat(result).isEqualTo(3);
- }
-
- @Test
- public void calculate_shouldUseCalculator_forSubstraction() {
- // GIVEN
- when(calculator.sub(3, 2)).thenReturn(1);
-
- // WHEN
- final int result = classUnderTest.calculate(
- new CalculationModel(CalculationType.SUBTRACTION, 3, 2))
- .getSolution();
-
- // THEN
- verify(calculator).sub(3, 2);
- assertThat(result).isEqualTo(1);
- }
-
- @Test
- public void calculate_shouldUseCalculator_forMultiplication() {
- // GIVEN
- when(calculator.multiply(-3, 2)).thenReturn(-6);
-
- // WHEN
- final int result = classUnderTest.calculate(
- new CalculationModel(CalculationType.MULTIPLICATION, -3, 2))
- .getSolution();
-
- // THEN
- verify(calculator).multiply(-3, 2);
- assertThat(result).isEqualTo(-6);
- }
-
- @Test
- public void calculate_shouldUseCalculator_forDivision() {
- // GIVEN
- when(calculator.divide(6, 3)).thenReturn(2);
-
- // WHEN
- final int result = classUnderTest.calculate(
- new CalculationModel(CalculationType.DIVISION, 6, 3))
- .getSolution();
-
- // THEN
- verify(calculator).divide(6, 3);
- assertThat(result).isEqualTo(2);
- }
-
- @Test
- public void calculate_shouldUseCalculator_forAnyAddition() {
- // GIVEN
- final Random r = new Random();
- when(calculator.add(any(Integer.class), any(Integer.class))).thenReturn(3);
-
- // WHEN
- final int result = classUnderTest.calculate(
- new CalculationModel(CalculationType.ADDITION, r.nextInt(), r.nextInt())).getSolution();
-
- // THEN
- verify(calculator, times(1)).add(any(Integer.class), any(Integer.class));
- verify(calculator, never()).sub(any(Integer.class), any(Integer.class));
- assertThat(result).isEqualTo(3);
- }
-
- @Test
- public void calculate_shouldThrowIllegalArgumentAxception_forADivisionBy0() {
- // GIVEN
- when(calculator.divide(1, 0)).thenThrow(new ArithmeticException());
-
- // WHEN
- assertThrows(IllegalArgumentException.class, () -> classUnderTest.calculate(
- new CalculationModel(CalculationType.DIVISION, 1, 0)));
-
- // THEN
- verify(calculator, times(1)).divide(1, 0);
- }
-
- @Test
- public void calculate_shouldFormatSolution_forAnAddition() {
- // GIVEN
- when(calculator.add(10000, 3000)).thenReturn(13000);
- when(solutionFormatter.format(13000)).thenReturn("13 000");
-
- // WHEN
- final String formattedResult = classUnderTest.calculate(
- new CalculationModel(CalculationType.ADDITION, 10000, 3000)).getFormattedSolution();
-
- // THEN
- assertThat(formattedResult).isEqualTo("13 000");
- }
+class CalculatorServiceTest {
+
+ @Mock
+ Calculator calculator;
+
+ @Mock
+ SolutionFormatter solutionFormatter;
+
+ CalculatorService classUnderTest;
+
+ @BeforeEach
+ void init() {
+ classUnderTest = new CalculatorServiceImpl(calculator, solutionFormatter);
+ }
+
+ @Test
+ void calculate_shouldUseCalculator_forAddition() {
+ // GIVEN
+ when(calculator.add(1, 2)).thenReturn(3);
+
+ // WHEN
+ final int result = classUnderTest.calculate(
+ new CalculationModel(CalculationType.ADDITION, 1, 2)).getSolution();
+
+ // THEN
+ verify(calculator).add(1, 2);
+ assertThat(result).isEqualTo(3);
+ }
+
+ @Test
+ void calculate_shouldUseCalculator_forSubstraction() {
+ // GIVEN
+ when(calculator.sub(3, 2)).thenReturn(1);
+
+ // WHEN
+ final int result = classUnderTest.calculate(
+ new CalculationModel(CalculationType.SUBTRACTION, 3, 2))
+ .getSolution();
+
+ // THEN
+ verify(calculator).sub(3, 2);
+ assertThat(result).isEqualTo(1);
+ }
+
+ @Test
+ void calculate_shouldUseCalculator_forMultiplication() {
+ // GIVEN
+ when(calculator.multiply(-3, 2)).thenReturn(-6);
+
+ // WHEN
+ final int result = classUnderTest.calculate(
+ new CalculationModel(CalculationType.MULTIPLICATION, -3, 2))
+ .getSolution();
+
+ // THEN
+ verify(calculator).multiply(-3, 2);
+ assertThat(result).isEqualTo(-6);
+ }
+
+ @Test
+ void calculate_shouldUseCalculator_forDivision() {
+ // GIVEN
+ when(calculator.divide(6, 3)).thenReturn(2);
+
+ // WHEN
+ final int result = classUnderTest.calculate(
+ new CalculationModel(CalculationType.DIVISION, 6, 3))
+ .getSolution();
+
+ // THEN
+ verify(calculator).divide(6, 3);
+ assertThat(result).isEqualTo(2);
+ }
+
+ @Test
+ void calculate_shouldUseCalculator_forAnyAddition() {
+ // GIVEN
+ final Random r = new Random();
+ when(calculator.add(any(Integer.class), any(Integer.class))).thenReturn(3);
+
+ // WHEN
+ final int result = classUnderTest.calculate(
+ new CalculationModel(CalculationType.ADDITION, r.nextInt(), r.nextInt())).getSolution();
+
+ // THEN
+ verify(calculator, times(1)).add(any(Integer.class), any(Integer.class));
+ verify(calculator, never()).sub(any(Integer.class), any(Integer.class));
+ assertThat(result).isEqualTo(3);
+ }
+
+ @Test
+ void calculate_shouldThrowIllegalArgumentAxception_forADivisionBy0() {
+ // GIVEN
+ when(calculator.divide(1, 0)).thenThrow(new ArithmeticException());
+
+ // WHEN
+ CalculationModel calculationModel = new CalculationModel(CalculationType.DIVISION, 1, 0);
+ assertThrows(IllegalArgumentException.class, () -> classUnderTest.calculate(calculationModel));
+
+ // THEN
+ verify(calculator, times(1)).divide(1, 0);
+ }
+
+ @Test
+ void calculate_shouldFormatSolution_forAnAddition() {
+ // GIVEN
+ when(calculator.add(10000, 3000)).thenReturn(13000);
+ when(solutionFormatter.format(13000)).thenReturn("13 000");
+
+ // WHEN
+ final String formattedResult = classUnderTest.calculate(
+ new CalculationModel(CalculationType.ADDITION, 10000, 3000)).getFormattedSolution();
+
+ // THEN
+ assertThat(formattedResult).isEqualTo("13 000");
+ }
}
diff --git a/src/test/java/tech/zerofiltre/testing/calcul/service/SolutionFormatterTest.java b/src/test/java/tech/zerofiltre/testing/calcul/service/SolutionFormatterTest.java
index 15f0006..3e3ab4a 100644
--- a/src/test/java/tech/zerofiltre/testing/calcul/service/SolutionFormatterTest.java
+++ b/src/test/java/tech/zerofiltre/testing/calcul/service/SolutionFormatterTest.java
@@ -5,25 +5,25 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-public class SolutionFormatterTest {
+class SolutionFormatterTest {
- private SolutionFormatter solutionFormatter;
+ private SolutionFormatter solutionFormatter;
- @BeforeEach
- public void initFormatter() {
- solutionFormatter = new SolutionFormatterImpl();
- }
+ @BeforeEach
+ void initFormatter() {
+ solutionFormatter = new SolutionFormatterImpl();
+ }
- @Test
- public void format_shouldFormatAnyBigNumber() {
- // GIVEN
- final int number = 1234567890;
+ @Test
+ void format_shouldFormatAnyBigNumber() {
+ // GIVEN
+ final int number = 1234567890;
- // WHEN
- final String result = solutionFormatter.format(number);
+ // WHEN
+ final String result = solutionFormatter.format(number);
- // THEN
- assertThat(result).isEqualTo("1 234 567 890");
- }
+ // THEN
+ assertThat(result).isEqualTo("1 234 567 890");
+ }
}