Я сталкиваюсь с проблемой при попытке создать фиктивный запрос Mvc get
. Я получаю нулевой результат при запросе объекта JSON. У меня есть то, что я могу создать post
штраф, но изо всех сил пытается получить данные при вызове конечной точки GET в моем контроллере.
AddressStepDefs.java
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= WebEnvironment.MOCK)
@Transactional
@AutoConfigureMockMvc
/**
* Address Step Definition class to execute Scenario(s) contained in Address.feature
* @author Lewis Jones
*
*/
public class AddressStepDefs {
@Autowired
private WebApplicationContext wac;
@Autowired
private MockMvc mockMvc;
private ResultActions result;
@Autowired
@MockBean
private AddressRepository addressRepo;
/**
* Build the Controller under test
*/
@BeforeClass
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(new AddressController()).build();
}
/**
* Set the mock server up
*/
@Before
public void serverSetup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
/**
* Build the WebApplicationContext
*/
@Given("The server is up and running")
public void the_server_is_up_and_running() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@When("I request to view an Address with id {int} at {string}")
public void i_request_to_view_an_Address_with_id_at(Integer id, String request) throws Exception {
/** Build a GET request using mockMvc **/
result = this.mockMvc.perform(get(request + id).contentType(MediaType.APPLICATION_JSON));
}
@Then("the response code should be OK {int} and the resulting Address object json should be:")
public void the_response_code_should_be_OK_and_the_resulting_Address_object_json_should_be(Integer responseCode, String json) throws Exception {
result.andExpect(status().is(responseCode));
result.andExpect(content().string(json));
}
- Контроллер конечная точка и запрос в порядке.
- В базе данных есть данные.
- Это работает БЕЗ
@MockBean
, но тогда мой post
фактически вставляет данные в базу данных. (Это не то, что я хочу) - Я пытался
@InjectMocks
, не повезло.
Куда я иду не так? У меня есть правильные аннотации?