spring-boot-test

Matching object in JSON with jsonpath in Spring Boot Test

匆匆过客 提交于 2019-12-07 15:56:28
I'm trying to write unit tests for a rest endpoint with Spring Boot Test that's going well but when I try to assert on an object in the json response with jsonPath an AssertionError is thrown even when contents are identical and the same. Sample Json { "status": 200, "data": [ { "id": 1, "placed_by": 1, "weight": 0.1, "weight_metric": "KG", "sent_on": null, "delivered_on": null, "status": "PLACED", "from": "1 string, string, string, string", "to": "1 string, string, string, string", "current_location": "1 string, string, string, string" } ] } Code in Kotlin mockMvc.perform( get("/api/v1/stuff"

Excluding Spring Integration from Spring Boot Test with Spring Boot 1.4

对着背影说爱祢 提交于 2019-12-07 03:27:24
I've recently began using Spring Boot 1.4 and the new Spring Boot Test feature. I'm using spring-boot-starter-integration with Spring Boot 1.4.2.RELEASE and attempting to test my repository with the new @DataJpaTest When I run my test I get an exception about no qualifying bean. The bean in question is the handler for my integration beans. How do I exclude integration from running during my JPA test? Application Class: import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation

How to set content-type for a spring boot test case which returns PDF file

蓝咒 提交于 2019-12-06 16:26:30
问题 I am currently testing one of my services with Spring boot test.The service exports all user data and produces a CSV or PDF after successful completion. A file is downloade in browser. Below is the code i have wrote in my test class MvcResult result = MockMvc.perform(post("/api/user-accounts/export").param("query","id=='123'") .contentType(MediaType.APPLICATION_JSON_VALUE) .accept(MediaType.APPLICATION_PDF_VALUE) .content(TestUtil.convertObjectToJsonBytes(userObjectDTO))) .andExpect(status()

@ComponentScan in application class breaks @WebMvcTest and @SpringBootTest

限于喜欢 提交于 2019-12-06 06:14:07
I am creating tests with @WebMvcTest annotation and found that if I have a @ComponentScan annotation in the application class it will break the expected behavior of the tests. According to the WebMvcTest javadoc: Using this annotation will disable full auto-configuration and instead apply only configuration relevant to MVC tests (i.e. @Controller , @ControllerAdvice , @JsonComponent Filter , WebMvcConfigurer and HandlerMethodArgumentResolver beans but not @Component , @Service or @Repository beans)." The problem is that with @ComponentScan it is instantiating beans annotated with @Service . If

@WithUserDetails does not seem to work

被刻印的时光 ゝ 提交于 2019-12-05 21:29:22
I have an application in which I use Spring Social Security for authentication and authorization. Unfortunately I am having some problems with mocking Spring Security. It seems that it does not work at all. I have a REST controller that returns 404 Not Found if the identifier of the entity it should return is not available. If the user is not logged in then any page redirects to the social login page of my app. I have read here that the @WithUserDetails annotation would suit me the best. So my test method looks like this @Test @SqlGroup({ @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD

How to set content-type for a spring boot test case which returns PDF file

谁说我不能喝 提交于 2019-12-04 22:09:17
I am currently testing one of my services with Spring boot test.The service exports all user data and produces a CSV or PDF after successful completion. A file is downloade in browser. Below is the code i have wrote in my test class MvcResult result = MockMvc.perform(post("/api/user-accounts/export").param("query","id=='123'") .contentType(MediaType.APPLICATION_JSON_VALUE) .accept(MediaType.APPLICATION_PDF_VALUE) .content(TestUtil.convertObjectToJsonBytes(userObjectDTO))) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_PDF_VALUE)) .andReturn(); String content

Context hierarchy in Spring Boot based tests

折月煮酒 提交于 2019-12-04 11:04:39
问题 My Spring Boot application is started like this: new SpringApplicationBuilder() .sources(ParentCtxConfig.class) .child(ChildFirstCtxConfig.class) .sibling(ChildSecondCtxConfig.class) .run(args); Config classes are annotated with @SpringBootApplication . As a result, I have one root context and two child web contexts. I want to write integration test and I would like to have same context hierarchy there. I want at least to test first child context (configured with ChildFirstCtxConfig.class )

Adding configuration class to SpringBootTest breaks component scan

余生颓废 提交于 2019-12-02 13:38:13
问题 I am trying to disable real Mongo connection and replace it with Fongo mock in tests. Here is my test class: @SpringBootTest @RunWith(SpringRunner.class) public class ControllerTest { @Autowired private WebApplicationContext wac; @Autowired private ObjectMapper objectMapper; @MockBean private MyService service; private MockMvc mockMvc; @Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test public void performTest() throws Exception { ...

SpringApplicationConfiguration cannot be resolved in a Spring Boot test

六月ゝ 毕业季﹏ 提交于 2019-12-02 01:42:01
I've generated a Spring Boot web application using Spring Initializr, using embedded Tomcat + Thymeleaf template engine, and package as an executable JAR file. Technologies used : Spring Boot 1.4.2.RELEASE, Spring 4.3.4.RELEASE, Thymeleaf 2.1.5.RELEASE, Tomcat Embed 8.5.6, Maven 3, Java 8. Now I want to implement a Test using the class SpringApplicationConfiguration , so I imported <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> But I get this error even I've updated the project several times The

SpringApplicationConfiguration not found: Erroneous spring-boot-starter-test content?

折月煮酒 提交于 2019-11-30 00:14:16
Getting a compilation error in Maven: [INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] /C:/prototypes/demo-sse-spring-boot-master/src/test/java/com/cedric/demo/sse/SseDemoApplicationTests.java:[6,37] package org.springframework.boot.test does not exist [ERROR] /C:/TITAN/demo-sse-spring-boot-master/src/test/java/com/cedric/demo/sse/SseDemoApplicationTests.java:[10,2] cannot find symbol symbol: class SpringApplicationConfiguration [INFO] 2 errors [INFO] -------------------