InsertKnowledge
InsertKnowledge

Reputation: 1028

How to make a method do nothing in junit integration test?

I have a mail service and controller which I want to test together but I don't want to send an email when the test is run. I tried using @Autowire on the mail sender in the test class to make it's method doNothing but it failed since it's not a mock. I can't really make it a @Mock since I'm not calling the controller method but rather making an actual request with import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; Here are the classes:

public class EmailControllerTest extends AbstractMvcTest {
    private static final String CONTROLLER_URL = "/api/emails";

    @Test
    @Sql({DbScripts.EMAILS, DbScripts.IDENTITY_NUMBER_TYPES, DbScripts.SUBJECTS, DbScripts.USERS})
    public void sendVerificationCodeToEmailStandardTest() throws Exception {
        EmailWrapperDto dto = new EmailWrapperDto();
        dto.setEmail("[email protected]");

        MockHttpServletRequestBuilder request = put(CONTROLLER_URL + "/verify");
        request.content(getRequestBodyFromObject(dto));

        mockMvc.perform(request).andExpect(status().isOk());
    }
}
@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = EServicesWebBackendApplication.class)
@ActiveProfiles(ApplicationConstants.SPRING_PROFILE_TEST)
public abstract class AbstractMvcTest {
    @Autowired
    private WebApplicationContext webApplicationContext;

    @Autowired
    private ObjectMapper objectMapper;

    private H2TestDatabaseCleaner dbCleaner = new H2TestDatabaseCleaner();

    @Autowired
    private SessionFactory sessionFactory;

    protected MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = setupMockMvcBuilder(MockMvcBuilders.webAppContextSetup(webApplicationContext), true).build();
    }

    protected DefaultMockMvcBuilder setupMockMvcBuilder(DefaultMockMvcBuilder builder, boolean withCsrf) {
        MockHttpServletRequestBuilder mockServletRequestBuilder = MockMvcRequestBuilders.get("/");

        return builder.defaultRequest(mockServletRequestBuilder.contentType(MediaType.APPLICATION_JSON)
                .header("X-Requested-With", "XMLHttpRequest"));
    }

    protected String getRequestBodyFromObject(Object objectToConvertToJson) throws JsonProcessingException {
        return objectMapper.writeValueAsString(objectToConvertToJson);
    }

    @After
    public void teardown() {
        dbCleaner.clean(sessionFactory);
    }
}
@Service
public class EmailService {
    private static final int TWENTY_FOUR_HOURS_IN_MINUTES = 1440;

    @Autowired
    private MailSender mailSender;

    @Autowired
    private GenericRepository genericRepository;

    @Transactional
    public void sendVerificationCode(String email) {
        User user = genericRepository.findByIdOrElseThrowException(User.class, 11);
        String token = UUID.randomUUID().toString();
        VerificationToken verificationToken = new VerificationToken();
        verificationToken.setToken(token);
        verificationToken.setUser(user);
        verificationToken.setExpirationDate(calculateExpiryDate(TWENTY_FOUR_HOURS_IN_MINUTES));
        genericRepository.save(verificationToken);

        String subject = "Registration Confirmation";

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(email);
        mailMessage.setFrom("[email protected]");
        mailMessage.setSubject(subject);
        mailMessage.setText(token);
        mailSender.send(mailMessage);
    }

    private Date calculateExpiryDate(int expiryTimeInMinutes) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Timestamp(cal.getTime().getTime()));
        cal.add(Calendar.MINUTE, expiryTimeInMinutes);
        return new Date(cal.getTime().getTime());
    }
}

Upvotes: 4

Views: 6948

Answers (1)

lugiorgi
lugiorgi

Reputation: 546

If you use spring's Mockito features you should be able to use the @MockBean annotation like so:

public class EmailControllerTest extends AbstractMvcTest {
   ...

   @MockBean
   private MailSender mailSender;

   ...
}

and then define the (optional, doing nothing is mockito's default behaviour for void method stubs) doNothing action:

Mockito.doNothing().when(mailSender).send(Mockito.any(SimpleMailMessage.class));

This can be useful if you want to use an ArgumentCaptor in a mocked void method:

ArgumentCaptor<SimpleMailMessage> valueCapture = ArgumentCaptor.forClass(SimpleMailMessage.class);
Mockito.doNothing().when(mailSender)
    .send(Mockito.any(SimpleMailMessage.class), valueCapture.capture());

see for reference: https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/mock/mockito/MockBean.html
https://www.baeldung.com/mockito-void-methods

Upvotes: 2

Related Questions