Skip to content

Instantly share code, notes, and snippets.

@jpragma

jpragma/MockitoExtensions.java Secret

Created Mar 30, 2020
Embed
What would you like to do?
Legacy code end-to-end test simplified example
package com.jpragma.jtest;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import java.util.Arrays;
public class MockitoExtensions {
private static Answer<?> alwaysThrowingAnswer = (invocation -> {
throw new IllegalArgumentException("Unexpected call to " + invocation.getMethod() +
" with " + Arrays.toString(invocation.getArguments()));
});
public static <T> T strictMock(Class<T> clazz) {
return Mockito.mock(clazz, alwaysThrowingAnswer);
}
}
package com.jpragma.jtest.xyz;
import com.jpragma.jtest.MockitoExtensions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = XyzComponentIT.Config.class)
public class XyzComponentIT {
@Autowired
XyzController controller;
@Autowired
AddressValidator addressValidator;
@Autowired
CustomerRepository customerRepository;
@Autowired
EmailServiceClient emailServiceClient;
@Test
void updateCustomerProfile() {
CustomerProfile customerProfile = new CustomerProfile()
.setId("001")
.setFirstName("John")
.setLastName("Doe")
.setAddress(
new PrimaryAddress()
.setStreet("1 Main st")
.setCity("Toronto")
.setRegion("ON")
.setCountry("Canada")
);
doReturn(true).when(addressValidator).validateAddress(any(PrimaryAddress.class));
doNothing().when(customerRepository).persistProfile(any(CustomerProfile.class));
String email = "jdoe@email.com";
doReturn(email).when(customerRepository).getCustomerEmail(customerProfile.getId());
doNothing().when(emailServiceClient).sendEmail(anyString(), anyString());
controller.updateCustomerProfile(customerProfile);
verify(customerRepository).persistProfile(customerProfile);
verify(emailServiceClient).sendEmail(email, "Profile Update");
}
@TestConfiguration
static class Config {
@Bean
XyzController controller() {
return new XyzController(profileUpdateService());
}
@Bean
ProfileUpdateService profileUpdateService() {
return new ProfileUpdateServiceImpl(addressValidator(), customerRepository(), emailServiceClient());
}
@Bean
AddressValidator addressValidator() {
return MockitoExtensions.strictMock(AddressValidator.class);
}
@Bean
CustomerRepository customerRepository() {
return MockitoExtensions.strictMock(CustomerRepository.class);
}
@Bean
EmailServiceClient emailServiceClient() {
return MockitoExtensions.strictMock(EmailServiceClient.class);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.