Created
September 7, 2025 20:22
-
-
Save guiathayde/22b18d1fad0662e7a6beaaf727a55c85 to your computer and use it in GitHub Desktop.
SomatoriaTest.java
This file contains hidden or 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
| package org.example.s03.ex2; | |
| import org.junit.jupiter.api.BeforeEach; | |
| import org.junit.jupiter.api.Test; | |
| import org.mockito.Mock; | |
| import org.mockito.MockitoAnnotations; | |
| import static org.junit.jupiter.api.Assertions.assertEquals; | |
| import static org.mockito.Mockito.*; | |
| public class SomatoriaTest { | |
| @Mock | |
| private MathOps mathOps; | |
| private Somatoria somatoria; | |
| @BeforeEach | |
| public void setup() { | |
| MockitoAnnotations.openMocks(this); | |
| somatoria = new Somatoria(mathOps); | |
| } | |
| @Test | |
| public void testSomaDeFatoriaisComVetorTreseQuatro() { | |
| // Arrange | |
| int[] numeros = {3, 4}; | |
| // Mock para fatorial(3) = 6 e fatorial(4) = 24 | |
| when(mathOps.fatorial(3)).thenReturn(6); | |
| when(mathOps.fatorial(4)).thenReturn(24); | |
| // Act | |
| int resultado = somatoria.somaDeFatoriais(numeros); | |
| // Assert | |
| assertEquals(30, resultado); // 6 + 24 = 30 | |
| // Verifica se fatorial foi chamado exatamente uma vez para cada número | |
| verify(mathOps, times(1)).fatorial(3); | |
| verify(mathOps, times(1)).fatorial(4); | |
| } | |
| @Test | |
| public void testSomaDeFatoriaisComVetorZeroAteQuatro() { | |
| // Arrange | |
| int[] numeros = {0, 1, 2, 3, 4}; | |
| // Mock para cada valor do fatorial | |
| when(mathOps.fatorial(0)).thenReturn(1); | |
| when(mathOps.fatorial(1)).thenReturn(1); | |
| when(mathOps.fatorial(2)).thenReturn(2); | |
| when(mathOps.fatorial(3)).thenReturn(6); | |
| when(mathOps.fatorial(4)).thenReturn(24); | |
| // Act | |
| int resultado = somatoria.somaDeFatoriais(numeros); | |
| // Assert | |
| assertEquals(34, resultado); // 1 + 1 + 2 + 6 + 24 = 34 | |
| // Verifica se fatorial foi chamado exatamente uma vez para cada número | |
| verify(mathOps, times(1)).fatorial(0); | |
| verify(mathOps, times(1)).fatorial(1); | |
| verify(mathOps, times(1)).fatorial(2); | |
| verify(mathOps, times(1)).fatorial(3); | |
| verify(mathOps, times(1)).fatorial(4); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment