title: How to make a method do nothing in junit integration test?
body: <p>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 <code>@Autowire</code> on the mail sender in the test class to make it's method <em>doNothing</em> but it failed since it's not a mock. I can't really make it a <code>@Mock</code> since I'm not calling the controller method but rather making an actual request with <code>import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;</code> Here are the classes: </p>
  
  <pre><code>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("test@gmail.com");
  
          MockHttpServletRequestBuilder request = put(CONTROLLER_URL + "/verify");
          request.content(getRequestBodyFromObject(dto));
  
          mockMvc.perform(request).andExpect(status().isOk());
      }
  }
  </code></pre>
  
  <pre><code>@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);
      }
  }
  </code></pre>
  
  <pre><code>@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("test@gmail.com");
          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());
      }
  }
  </code></pre>