Last active
April 24, 2020 10:10
-
-
Save hakansander/81994e8d327d440d31fa065e41c5ceb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringRunner.class) | |
@WebMvcTest(InvoiceController.class) | |
public class InvoiceControllerTest { | |
@Autowired | |
private MockMvc mockMvc; | |
@MockBean | |
private InvoiceService invoiceService; | |
private static final ObjectMapper mapper = new ObjectMapper(); | |
@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); | |
when(invoiceService.getInvoiceInfo(mockPhoneNumber)) | |
.thenReturn(mockInvoiceResponse); | |
mockMvc.perform(get("/invoiceQuery/{phoneNumber}", mockPhoneNumber)) | |
.andExpect(jsonPath("$.statusCode", is("200"))); | |
} | |
@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); | |
when(invoiceService.getInvoiceInfo(mockPhoneNumber)) | |
.thenReturn(mockInvoiceResponse); | |
mockMvc.perform(get("/invoiceQuery/{phoneNumber}", mockPhoneNumber)) | |
.andExpect(jsonPath("$.statusCode", is("204"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment