spring-test-mvc

Kotlin + SpringBootTest + Junit 5 + AutoConfigureMockMvc: test passing when it was supposed to fail (seems @BeforeEach not taking effect)

大城市里の小女人 提交于 2021-02-11 04:52:41
问题 I coded a very simple and common CRUD in Kotlin. I want to do basic tests as testing post, delete, get and put. Probably I understood something wrong: I used Beforeeach aimed to insert a register so I could check during get test. I don't get exception but it seems during get test it always returning ok when it should be NOT_FOUND for any other id different than 1 in bellow test. Any clue or guidance in right direction will be wellcome even if see other bad practice bellow based on my purpose

Kotlin + SpringBootTest + Junit 5 + AutoConfigureMockMvc: test passing when it was supposed to fail (seems @BeforeEach not taking effect)

[亡魂溺海] 提交于 2021-02-11 04:51:13
问题 I coded a very simple and common CRUD in Kotlin. I want to do basic tests as testing post, delete, get and put. Probably I understood something wrong: I used Beforeeach aimed to insert a register so I could check during get test. I don't get exception but it seems during get test it always returning ok when it should be NOT_FOUND for any other id different than 1 in bellow test. Any clue or guidance in right direction will be wellcome even if see other bad practice bellow based on my purpose

@Autowire MockMvc - Spring Data Rest

允我心安 提交于 2021-02-07 18:41:36
问题 Given the Repository public interface ResourceRepository extends CrudRepository<Resource, Long> { ... } The following test code: @WebMvcTest @RunWith(SpringRunner.class) public class RestResourceTests { @Autowired private MockMvc mockMvc; @Test public void create_ValidResource_Should201() { String requestJson = "..."; mockMvc.perform( post("/resource") .content(requestJson) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isCreated()); // This fails with 404 } } In order to fix

How to add basic auth to Autowired testRestTemplate in SpringBootTest; Spring Boot 1.4

夙愿已清 提交于 2021-02-07 06:17:03
问题 My OAuth integration test before Spring Boot 1.4 looked as follows(updates just to not use deprecated features): @RunWith(SpringRunner.class) @SpringBootTest(classes = { ApplicationConfiguration.class }, webEnvironment = WebEnvironment.RANDOM_PORT) public class OAuth2IntegrationTest { @Value("${local.server.port}") private int port; private static final String CLIENT_NAME = "client"; private static final String CLIENT_PASSWORD = "123456"; @Test public void testOAuthAccessTokenIsReturned() {

How to add basic auth to Autowired testRestTemplate in SpringBootTest; Spring Boot 1.4

纵然是瞬间 提交于 2021-02-07 06:13:24
问题 My OAuth integration test before Spring Boot 1.4 looked as follows(updates just to not use deprecated features): @RunWith(SpringRunner.class) @SpringBootTest(classes = { ApplicationConfiguration.class }, webEnvironment = WebEnvironment.RANDOM_PORT) public class OAuth2IntegrationTest { @Value("${local.server.port}") private int port; private static final String CLIENT_NAME = "client"; private static final String CLIENT_PASSWORD = "123456"; @Test public void testOAuthAccessTokenIsReturned() {

Spring Boot Testing @WebMvcTest for a Controller appears to load other controllers in the context

大城市里の小女人 提交于 2021-02-06 10:57:56
问题 Here's my test case for a Spring Controller @RunWith(SpringRunner.class) @WebMvcTest(value = MyController.class) public class MyControllerTest { @MockBean private MyService myService; } So this is a unit test specifically for the methods in MyController. But when I run the test, Spring appears to begin instantiating OtherController and all of it's dependencies. I have tried updating the above as @RunWith(SpringRunner.class) @WebMvcTest(value = MyController.class, excludeFilters =