spring-test

Should Mockito be used with MockMvc's webAppContextSetup in Spring 4?

二次信任 提交于 2019-12-04 15:23:28
I'm having difficulties getting Mockito and MockMvc working together when I use the webAppContextSetup together. I'm curious if it's because I'm mixing the two in a way they were never intended. Source: https://github.com/zgardner/spring-boot-intro/blob/master/src/test/java/com/zgardner/springBootIntro/controller/PersonControllerTest.java Here is the test I'm running: package com.zgardner.springBootIntro.controller; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.springframework.beans.factory

How to test POST spring mvc

我的未来我决定 提交于 2019-12-04 13:53:15
问题 My problem is to how to call this. I could do MyObject o = new MyObject(); myController.save(o, "value"); but this is not what I would like to do. I would like the MyObject to be in the request post body? How can this be done? @Requestmapping(value="/save/{value}", method=RequestMethod.POST) public void post(@Valid MyObject o, @PathVariable String value{ objectService.save(o); } Just to be clear I am talking about unit testing. Edit: @RequestMapping(value = "/", method = RequestMethod.POST)

pass remoteUser value in HttpServletRequest to mockmvc perform test

前提是你 提交于 2019-12-04 13:01:05
I have an api call as: @RequestMapping(value = "/course", method = RequestMethod.GET) ResponseEntity<Object> getCourse(HttpServletRequest request, HttpServletResponse response) throwsException { User user = userDao.getByUsername(request.getRemoteUser()); } I'm getting the user null when I call this from the test class like: HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getRemoteUser()).thenReturn("test1"); MvcResult result = mockMvc.perform( get( "/course") .contentType(MediaType.APPLICATION_JSON) .andExpect( status().isOk() ) .andExpect( 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 )

Spring-test and ServletContextListener in web.xml

[亡魂溺海] 提交于 2019-12-04 10:59:35
i try to use spring-test(3.2.10) and integration tests with TestNG by this link . I created RootTest.java @WebAppConfiguration @ContextConfiguration("file:src/test/resources/root-context2.xml") public class ReferenceServiceTest extends AbstractTestNGSpringContextTests { ... spring context loaded success. But my global variables not instantiated because web.xml ignored. In web.xml i have my own "listener-class"(implementation of ServletContextListener) and "context-param". How i can load web.xml context(and calls all application startup listeners) with spring integration test context? As stated

What are TestExecutionListeners, and what do they do?

青春壹個敷衍的年華 提交于 2019-12-04 10:00:48
问题 As far as I understand, TestExecutionListeners act like @BeforeClass methods in JUnit. What I don't understand is why I need to use DependencyInjectionTestExecutionListener , TransactionalTestExecutionListener and DirtiesContextTestExecutionListener to use DbUnitTestExecutionListener . Normally without DbUnit, I can create and populate the database. Why suddenly do I need to use these listeners to do some CRUD for my database? 回答1: TestExecutionListeners provide various types of functionality

@RunWith(SpringRunner.class) vs @RunWith(MockitoJUnitRunner.class)

给你一囗甜甜゛ 提交于 2019-12-04 08:56:49
问题 I was using @RunWith(MockitoJUnitRunner.class) for my junit test with mockito. But now i am working with spring boot app and trying to use @RunWith(SpringRunner.class) . Does using @RunWith(SpringRunner.class) has any advantages over using @RunWith(MockitoJUnitRunner.class) ? Can i still use feature like @Injectmock , @Mock , @Spy with @RunWith(SpringRunner.class) 回答1: The SpringRunner provides support for loading a Spring ApplicationContext and having beans @Autowired into your test instance

executeSqlScript fails with Spring for PL/SQL block

最后都变了- 提交于 2019-12-04 03:54:52
问题 Im trying to populate my database using the builtin function executeSqlScript from AbstractTransactionalJUnit4SpringContextTests using the following external SQL file. declare id number; begin insert into table1 (field1) values ('V1') returning account__id into id; insert into table2 (my_id, field2) VALUES (id, 'Value3'); end; However im getting the following error. Im not sure what im allowed to do in a SQL file I would like to execute using executeSqlScript . org.springframework.jdbc

How to mock Eureka when doing Integration Tests in Spring?

妖精的绣舞 提交于 2019-12-04 03:46:56
I am running a simple Junit Testing a Controller in Spring Boot. The test code looks like this: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {FrontControllerApplication.class}) @WebAppConfiguration @ComponentScan @IntegrationTest({"server.port:0", "eureka.client.registerWithEureka:false", "eureka.client.fetchRegistry:false"}) @ActiveProfiles("integrationTest") public class MyControllerIT { In the application-integrationTest.properties I have the following Eureka Settings: ####### Eureka eureka.serviceUrl.default=http://localhost:8767/eureka/ eureka

Spring Boot. @DataJpaTest H2 embedded database create schema

末鹿安然 提交于 2019-12-04 02:48:49
问题 I have couple of entities in my data layer stored in particular schema. For example: @Entity @Table(name = "FOO", schema = "DUMMY") public class Foo {} I'm trying to setup H2 embedded database for integration testing of my data layer. I'm using @DataJpaTest annotation for my tests to get H2 embedded database configured automatically. However, the creation of tables fails because schema DUMMY is not created at DB initialization. Any ideas on how to create schema before creation of tables in