forked from michal-lipski/testing-examples
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathStepDefs.java
More file actions
95 lines (76 loc) · 3.59 KB
/
StepDefs.java
File metadata and controls
95 lines (76 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.pik.contact.cucumber;
import com.pik.contact.Application;
import com.pik.contact.domain.Contact;
import com.pik.contact.repository.ContactRepository;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.assertj.core.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.web.context.WebApplicationContext;
import static org.hamcrest.CoreMatchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
@DirtiesContext
@ContextConfiguration(classes = Application.class)
@WebAppConfiguration
public class StepDefs {
@Autowired
private WebApplicationContext wac;
private ResultActions mvcResult;
@Autowired
private ContactRepository contactRepository;
private MockMvc getMockMvc() {
return webAppContextSetup(wac).build();
}
@Before
public void clear_db() {
contactRepository.deleteAll();
}
@Given("^there is a contact with name = \"(.*?)\"$")
public void there_is_a_contact_with_name(String name) {
Contact contact = new Contact(name, "Doe", "Developer",null,null,null);
contact.setId("123");
contactRepository.save(contact);
}
@Then("^the status code should be (\\d+)$")
public void the_status_code_should_be(int code) throws Throwable {
mvcResult.andExpect(status().is(code));
}
@Then("^it should have the field \"([^\"]*)\" containing the value \"([^\"]*)\"$")
public void it_should_have_the_field_containing_the_value(String field, String value) throws Throwable {
mvcResult.andExpect(jsonPath("$." + field, is("test")));
}
@Then("^it should have the field \"([^\"]*)\" containing the value (\\d+)$")
public void it_should_have_the_field_containing_the_value(String field, int value) throws Throwable {
mvcResult.andExpect(jsonPath("$." + field, is(value)));
}
@Then("^it should have the field \"([^\"]*)\"$")
public void it_should_have_the_field(String field) throws Throwable {
mvcResult.andExpect(jsonPath("$." + field).exists());
}
@When("^I perform GET request on \"([^\"]*)\"$")
public void I_perform_GET_request_on(String url) throws Throwable {
mvcResult = getMockMvc().perform(get(url));
}
@And("^the response should be$")
public void the_response_should_be(String responseJson) throws Throwable {
String response = mvcResult.andReturn().getResponse().getContentAsString();
Assertions.assertThat(response).contains(responseJson.replaceAll("\\s", "").replace("\n", "").replace("\t", ""));
}
@Given("^there is a contact with name = '([^\"]*)' and full name = '([^\"]*)'$")
public void there_is_a_contact_with_name_John_and_full_name_Doe(String name, String fullName) {
Contact contact = new Contact(name, fullName, null,null,null,null);
contact.setId("123");
contactRepository.save(contact);
}
}