Created
September 7, 2025 20:23
-
-
Save guiathayde/d207e1dad73b1d985f603fe96efde306 to your computer and use it in GitHub Desktop.
Ex3: 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.ex3; | |
| 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 static org.junit.jupiter.api.Assertions.assertEquals; | |
| import static org.mockito.Mockito.*; | |
| @ExtendWith(MockitoExtension.class) | |
| public class SomatoriaTest { | |
| @Mock | |
| private MathOps mathOpsMock; | |
| @Mock | |
| private Primo primoMock; | |
| private Somatoria somatoria; | |
| @BeforeEach | |
| public void setUp() { | |
| somatoria = new Somatoria(mathOpsMock); | |
| } | |
| @Test | |
| public void testSomaDeFatoriaisComVetor1() { | |
| // Arrange | |
| int[] numeros = {5, 10}; | |
| // Configure os mocks | |
| when(primoMock.ehPrimo(5)).thenReturn(true); // 5 é primo | |
| when(primoMock.ehPrimo(10)).thenReturn(false); // 10 não é primo | |
| when(mathOpsMock.fatorial(10)).thenReturn(3628800); // fatorial de 10 = 3628800 | |
| // Act | |
| int resultado = somatoria.somaDeFatoriais(numeros, primoMock); | |
| // Assert | |
| assertEquals(3628800, resultado); | |
| verify(primoMock).ehPrimo(5); | |
| verify(primoMock).ehPrimo(10); | |
| verify(mathOpsMock, never()).fatorial(5); // Não deve calcular fatorial de número primo | |
| verify(mathOpsMock).fatorial(10); | |
| } | |
| @Test | |
| public void testSomaDeFatoriaisComVetor2() { | |
| // Arrange | |
| int[] numeros = {3, 4, 4, 5}; | |
| // Configure os mocks | |
| when(primoMock.ehPrimo(3)).thenReturn(true); // 3 é primo | |
| when(primoMock.ehPrimo(4)).thenReturn(false); // 4 não é primo | |
| when(primoMock.ehPrimo(5)).thenReturn(true); // 5 é primo | |
| when(mathOpsMock.fatorial(4)).thenReturn(24); // fatorial de 4 = 24 | |
| // Act | |
| int resultado = somatoria.somaDeFatoriais(numeros, primoMock); | |
| // Assert | |
| assertEquals(48, resultado); // 24 + 24 = 48 (fatorial de 4, duas vezes) | |
| verify(primoMock).ehPrimo(3); | |
| verify(primoMock, times(2)).ehPrimo(4); // Verificado duas vezes, pois 4 aparece duas vezes no array | |
| verify(primoMock).ehPrimo(5); | |
| verify(mathOpsMock, never()).fatorial(3); // Não deve calcular fatorial de número primo | |
| verify(mathOpsMock, times(2)).fatorial(4); // Chamado duas vezes | |
| verify(mathOpsMock, never()).fatorial(5); // Não deve calcular fatorial de número primo | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment