spring-test

How to set role in MockHttpServletRequest?

为君一笑 提交于 2019-12-25 02:44:46
问题 I have read following topic: https://stackoverflow.com/a/18487953/2674303 But it is a bit not my variant. Inside my method controller which I need to test I have following line: httpServletRequest.isUserInRole("ROLE_OWNER"); How can I set role in MockHttpServletRequest ? 回答1: mockMvc.perform(post("/owner/terminals/edit").principal(principal).with(new RequestPostProcessor() { public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { request.addUserRole("ROLE_OWNER");

Spring DataJpaTest do not load javax.transaction.SystemException with Java 9

自作多情 提交于 2019-12-25 00:50:19
问题 I would like to test my service layer with DataJpaTest annotation. My setup is the following : SpringBoot 2.0.0-M7, JAVA 9 and pom xml : <dependencies> <!-- Compile --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId

How to pass ModelAttrubute parameters using MockMvc?

不问归期 提交于 2019-12-24 15:09:39
问题 I have this html spring form: <form:form action="addVacancy" modelAttribute="myVacancy"> <form:label path="name">name</form:label> <form:input path="name" ></form:input> <form:errors path="name" cssClass="error" /> <br> <form:label path="description">description</form:label> <form:input path="description" id="nameInput"></form:input> <form:errors path="description" cssClass="error" /> <br> <form:label path="date">date</form:label> <input type="date" name="date" /> <form:errors path="date"

Spring security DefaultMethodSecurityExpressionHandler bean is not registered for Integration Test's default spring security config

余生长醉 提交于 2019-12-24 02:56:20
问题 I am attempting to write Spring MVC integration test with Spring Security and Thymeleaf for the view layer. I have setup my MockMvc object with Spring Security Integration just like all the examples from the documentation. Integration Test setUp: import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; import static org.springframework.security

Spring Security + Spring-Boot Testing Controller

谁说胖子不能爱 提交于 2019-12-24 02:13:07
问题 I'm trying to test the home controller @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } I'm using spring security using as username "user" and test as password by default but @PreAuthorize is not working @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @PreAuthorize("hasRole('ADMIN')") public class HomeControllerTest { @Autowired private TestRestTemplate restTemplate; @Test @WithMockUser(username = "user",

Can not build MockMvc (There is already handler of type X mapped)

北战南征 提交于 2019-12-23 16:28:10
问题 I have simple Controller class: @Controller public class UserController { @RequestMapping("/user") @ResponseBody @PostAuthorize("hasPermission(returnObject, 'VIEW')") public User getUser(Long id) { // fetch user from DB } } I want to integration-test my controllers with Spring Security. Based on this article I have created the following test: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = WebappConfig.class)

@SQL one time per class

♀尐吖头ヾ 提交于 2019-12-23 12:49:34
问题 I'm writing some integration test using spring framework. I have different SQL scripts for different integration test classes. Something like this: @ContextConfiguration(classes = ...) @Sql("classpath:sportCenter-test.sql") public class SportCenterResourceIT { ... } Everything works perfectly except for the fact that the SQL script is executed before each test, instead of one time per class. I have already searched for some time the spring documentation but I was not able to find something

Merging @Sql from superclass with @Sql in subclass

安稳与你 提交于 2019-12-23 12:17:53
问题 I have an abstract class annotated with @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts="someScript") . I have a test class which inherits from the abstract class. The child class is also annotated with @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts="someOtherScript") . When I was running spring boot 1.2, everything worked as I expected it to: scripts from parent class were run before child class. I upgraded to spring boot 1.3 and now, the child class's @Sql

Unknown data type when using an integer over NamedParameterJDBCTemplate on H2 [SPRING-BOOT]

断了今生、忘了曾经 提交于 2019-12-23 08:39:24
问题 I'm testing a Dao with an In-Memory DB with H2. I'm passing an int to the query with a map to execute it. This query is working OK on Oracle SQL, but is not succeding in H2. DAO @Override public int deleteCancelled(int days) { final Map<String, Object> namedParameters = new HashMap<String, Object>(); namedParameters.put(DAYS, days); namedParameters.put(STATUS, StatusEnum.CANCELLED.toString()); int updated = this.namedParameterJdbcTemplate.update(Query.QUERIES.DELETE_CANCELLED, namedParameters

Can @ContextConfiguration in a custom annotation be merged?

余生颓废 提交于 2019-12-23 04:58:19
问题 I am working on custom Spring Boot starters. In a test starter what I wanted do to is to implement a composed annotation, which would add additional @Configuration classes to the ApplicationContext (and possibly use this annotation in a TestExecutionListener ). ex: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @ContextConfiguration(classes = AdditionalTestConfiguration.class) public @interface ComposedAnnotation { } And use that in my Spring Boot integration test: @RunWith