Skip to content

Instantly share code, notes, and snippets.

View hakansander's full-sized avatar

Hakan Sander hakansander

  • Istanbul/Turkey
View GitHub Profile
@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(); });
{
"success": false,
"responseMsg": "No invoice data found for given phone number",
"statusCode": "204",
"response": "NO_CONTENT",
"data": {}
}
@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
@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
@Test
public void testWhenPhoneNumEnteredAndNoDataFound_thenReturnHttp204() throws Exception {
final String mockPhoneNumber = "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);
@hakansander
hakansander / InvoiceSuccessControllerUnitTest.java
Last active April 24, 2020 10:09
Success case of Invoice Controller
@Test
public void testWhenPhoneNumEnteredAndDataExists_thenReturnHttp200() throws Exception {
final String mockPhoneNumber = "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);
@RunWith(SpringRunner.class)
@RestClientTest(InvoiceServiceImp.class)
@PropertySource("classpath:application-test.properties")
public class InvoiceServiceTest {
@Autowired
private MockRestServiceServer mockServer;
@Autowired
@RunWith(SpringRunner.class)
@WebMvcTest(InvoiceController.class)
public class InvoiceControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private InvoiceService invoiceService;
@hakansander
hakansander / response_http200.json
Last active April 24, 2020 10:10
Sample response for Unit Testing Medium Blog Code Snippet
{
"success": true,
"responseMsg": "Successfully returned invoice list",
"statusCode": "200",
"response": "Success",
"data": {
"InvoiceList": [
{
"Id": "35011",
"Name": "Bonus Demand Tariff Package",
@hakansander
hakansander / InvoiceService.java
Last active April 22, 2020 12:08
BillingService.java Code Snippet for Unit Testing Blog Article in Medium
public InvoiceQueryInfo getInvoiceInfo(String phoneNumber) {
try {
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
HttpHeaders requestHeaders = new HttpHeaders();
final String uuid = UUID.randomUUID().toString();
requestHeaders.add("id", uuid);
HttpEntity<?> httpEntity = new HttpEntity<>(body, requestHeaders);
requestHeaders.setContentType(MediaType.APPLICATION_JSON);