Skip to content

Instantly share code, notes, and snippets.

@hakansander
Last active April 24, 2020 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakansander/d83510c158fa01bfba8b64068ffa872c to your computer and use it in GitHub Desktop.
Save hakansander/d83510c158fa01bfba8b64068ffa872c to your computer and use it in GitHub Desktop.
@RunWith(SpringRunner.class)
@RestClientTest(InvoiceServiceImp.class)
@PropertySource("classpath:application-test.properties")
public class InvoiceServiceTest {
@Autowired
private MockRestServiceServer mockServer;
@Autowired
private InvoiceService invoiceService;
@Autowired
private InvoiceServiceImp mockService;
private static final ObjectMapper mapper = new ObjectMapper();
private InvoiceResponse responseInvoiceResponseEntity;
@Value("${invoiceInfo.path}")
private String invoiceUrl;
@Test
public void testWhenPhoneNumEnteredAndDataExists_thenReturnHttp200() throws Exception {
String mockPhoneNum = "534*******";
final FileInputStream fileInputStream = new FileInputStream(ResourceUtils.getFile("classpath:response_http200.json"));
final String staticResponse = StreamUtils.copyToString(fileInputStream, Charset.defaultCharset());
InvoiceResponse mockInvoiceResponse = mapper.readValue(staticResponse, InvoiceResponse.class);
this.mockServer.expect(ExpectedCount.once(), requestTo(invoiceUrl
+ mockPhoneNum))
.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
.andRespond(withStatus(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON)
.body(mapper.writeValueAsString(mockInvoiceResponse)));
responseInvoiceResponseEntity = mockService.getInvoiceInfo(mockPhoneNum);
mockServer.verify();
Assert.assertEquals("200",
responseInvoiceResponseEntity.
getStatusCode());
}
@Test
public void testWhenPhoneNumEnteredAndNoDataFound_thenReturnHttp204() throws Exception {
String mockPhoneNum = "542*******";
final FileInputStream fileInputStream = new FileInputStream(ResourceUtils.getFile("classpath:response_http204.json"));
final String staticResponse = StreamUtils.copyToString(fileInputStream, Charset.defaultCharset());
InvoiceResponse mockInvoiceResponse = mapper.readValue(staticResponse, InvoiceResponse.class);
this.mockServer.expect(ExpectedCount.once(), requestTo(invoiceUrl
+ mockPhoneNum))
.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
.andRespond(withStatus(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON)
.body(mapper.writeValueAsString(mockInvoiceResponse)));
responseInvoiceResponseEntity = mockService.getInvoiceInfo(mockPhoneNum);
mockServer.verify();
Assert.assertEquals("204", responseInvoiceResponseEntity.
getStatusCode());
}
@Test
public void testWhenInternalServerError_thenReturnHttp500() throws Exception {
String mockPhoneNum = "542*******";
this.mockServer.expect(ExpectedCount.once(), requestTo(invoiceUrl
+ mockPhoneNum))
.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
.andRespond((response) -> { throw new RuntimeException(); });
responseInvoiceResponseEntity = mockService.getInvoiceInfo(mockPhoneNum);
mockServer.verify();
Assert.assertEquals("500", responseInvoiceResponseEntity.
getStatusCode());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment