Skip to content

Commit

Permalink
Mock Java mail sender
Browse files Browse the repository at this point in the history
  • Loading branch information
johanah29 committed Oct 23, 2024
1 parent a6c364a commit be39cbe
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@
import io.openbas.utils.mockUser.WithMockObserverUser;
import io.openbas.utils.mockUser.WithMockPlannerUser;
import jakarta.annotation.Resource;
import jakarta.mail.internet.MimeMessage;
import jakarta.servlet.ServletException;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.ResourceUtils;
Expand All @@ -40,11 +47,13 @@
import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestInstance(PER_CLASS)
@ExtendWith(MockitoExtension.class)
class InjectApiTest extends IntegrationTest {

static Exercise EXERCISE;
Expand Down Expand Up @@ -80,6 +89,8 @@ class InjectApiTest extends IntegrationTest {
private UserRepository userRepository;
@Resource
private ObjectMapper objectMapper;
@MockBean
private JavaMailSender javaMailSender;

@BeforeAll
void beforeAll() {
Expand Down Expand Up @@ -374,6 +385,11 @@ void executeEmailInjectForExerciseTest() throws Exception {
"application/xlsx",
in.readAllBytes());

// Mock the behavior of JavaMailSender
doNothing().when(javaMailSender).send(ArgumentMatchers.any(SimpleMailMessage.class));
MimeMessage mimeMessage = mock(MimeMessage.class);
when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage);

// -- EXECUTE --
String response = mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject")
.file(inputJson)
Expand Down

0 comments on commit be39cbe

Please sign in to comment.