Guest User

Untitled

a guest
Jan 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class DemoApplicationTests {
  4. @Autowired
  5. private ApplicationContext context;
  6. @Autowired
  7. private CustomerRepository repository;
  8.  
  9. private WebTestClient client;
  10.  
  11. @Before
  12. public void setUp() throws Exception {
  13. client = WebTestClient.bindToApplicationContext(context).build();
  14. }
  15.  
  16. @Test
  17. public void givenCustomers_whenGetMethod_thenReturnOkCustomersBody() {
  18. repository.saveAll(TestUnits.mockCustomers()).collectList().block();
  19.  
  20. client.get().uri("/customers")
  21. .exchange()
  22. .expectStatus().isOk()
  23. .expectBodyList(Customer.class)
  24. .hasSize(4);
  25. }
  26.  
  27. @Test
  28. public void givenID_whenGetMethodByID_thenReturnOkCustomerBody() {
  29. String mockedId = repository.save(TestUnits.mockCustomer()).map(Customer::getId).block();
  30.  
  31. client.get().uri(String.format("/customers/%s", mockedId))
  32. .exchange()
  33. .expectStatus().isOk()
  34. .expectBody(Customer.class);
  35. }
  36.  
  37. @Test
  38. public void givenFistName_whenGetMethodByFirstName_thenReturnOkCustomerBody() {
  39. String mockedId = repository.save(TestUnits.mockCustomer()).map(Customer::getFirstName).block();
  40.  
  41. client.get().uri(String.format("/customers?name=%s", mockedId))
  42. .exchange()
  43. .expectStatus().isOk()
  44. .expectBody(Customer.class);
  45. }
  46.  
  47. @Test
  48. public void givenCustomer_whenPostMethod_thenReturnCreatedCustomerBody() {
  49. client.post().uri("/customers")
  50. .body(TestUnits.mockCustomerMono(), Customer.class)
  51. .exchange()
  52. .expectStatus().isCreated()
  53. .expectBody(Customer.class);
  54. }
  55.  
  56. @Test
  57. public void givenID_whenPutMethodByID_thenReturnOkCustomerBody() {
  58. String mockedId = repository.save(TestUnits.mockCustomer()).map(Customer::getId).block();
  59.  
  60. client.put().uri(String.format("/customers/%s", mockedId))
  61. .body(TestUnits.mockCustomerForUpdate(), Customer.class)
  62. .exchange()
  63. .expectStatus().isOk()
  64. .expectBody()
  65. .jsonPath("$.age").isEqualTo(21);
  66. }
  67.  
  68. @Test
  69. public void givenID_whenDeleteMethodByID_thenReturnOkNoBody() {
  70. String mockedId = repository.save(TestUnits.mockCustomer()).map(Customer::getId).block();
  71.  
  72. client.delete().uri(String.format("/customers/%s", mockedId))
  73. .exchange()
  74. .expectStatus().isOk()
  75. .expectBody()
  76. .isEmpty();
  77. }
  78. }
Add Comment
Please, Sign In to add comment