Pros
Easy to write
Provide a spec for components
Great for pure components
Cons
Heavy use of mocking
Hard to isolate test from implementation
Inputs
Outputs
Application
Pros
Provide a spec for application
Implementation agnostic
Test API
Cons
Dependencies mocking
Performance overhead
Harder to debug
Inputs
Outputs
Application
Pros
Test complete integration
Implementation agnostic
User interactions
Cons
Dependencies mocking
Environment setup
Performance overhead
Harder to debug
Unstable
User inputs
Webpage
Application
Front
Inputs
Outputs
Application
Test
Inputs
Outputs
Application
Test
Mock
Mock
Mock
Inputs
Outputs
Application
Test
Mock
Mock
Mock
Test
Inputs
Outputs
Application
@RunWith(SpringRunner.class)
@Transactional
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AccountRestControllerIntTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper mapper;
@Test
@WithMockBasicAuth(principal = "test@test.com")
public void test_get_user_accounts() throws Exception {
MvcResult result = mockMvc.perform(get("/account")
.contentType(MediaType.APPLICATION_JSON)).andReturn();
Account[] accounts = mapper.readValue(result.getResponse().getContentAsString(), Account[].class);
Assert.assertEquals(1, accounts.length);
Assert.assertEquals("Compte Chéque", accounts.get(0).name);
}
}
private ClientAndServer mockServer;
@Before
public void startMockServer() {
mockServer = startClientAndServer(9999);
mockServer.when(request().withMethod("GET")
.withPath("/users/me/connections").withQueryStringParameter("expand"))
.respond(response().withStatusCode(200)
.withBody("{\"connections\": [{\"id\": 505}], \"total\": 1}"));
}
@After
public void stopMockServer() {
mockServer.stop();
}