spring-test

Exclude a particular class from Spring Component scan while writing Spring Integration Test

好久不见. 提交于 2019-12-23 03:06:40
问题 My classes are.. lies in src/intregation-test/java @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = StoreOrderFulfillmentApplication.class) @ActiveProfiles("Test") public class OrderCreationIntregationTest { @Autowired private TestRestTemplate restTemplate; @MockBean private OrderRepository orderRepository; @MockBean private OrderLineItemRepository orderLineItemRepository; @MockBean private InternalEventPublisher internalEventPublisher;

how to run/turn off selective tests based on profiles in spring boot

烂漫一生 提交于 2019-12-22 04:27:07
问题 I have a spring-boot application for which I am writing IT tests. The data for the tests comes from application-dev.properties when I activate dev profile Here is what I have for tests: @RunWith(SpringRunner.class) @SpringBootTest @WebAppConfiguration public class ApplicationTests { @Autowired Environment env; @Test public void contextLoads() { System.out.println(Arrays.toString((env.getActiveProfiles()))); } } ServiceITTest public class ServiceITTest extends ApplicationTests { @value String

Cannot JUnit test using Spring

三世轮回 提交于 2019-12-22 01:44:55
问题 My test is defined as follows: package com.mytest; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class SpringTestCase { @Test public void testSave_success() { fail("Not implemented"); } } My intention is to just run this test and fail! My test's spring configuration file is located in: /com/mytest/SpringTestCase-context.xml The contents of my spring configuration file are as follows: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org

How can I initialize a Spring applicationContext just once for all tests

自古美人都是妖i 提交于 2019-12-21 22:47:33
问题 I have a set of tests based which need a spring context. For fast test execution I want to make sure that the Spring context is initialized just once, then all the tests should be run against this context, then it should shut down. I already tried the following approaches: Use @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration(MyAnnotatedConfig.class) to initialize the spring context Use a @RunWith(SpringJUnit4ClassRunner.class) and @TestExecutionListeners(

pass remoteUser value in HttpServletRequest to mockmvc perform test

a 夏天 提交于 2019-12-21 20:23:58
问题 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( "

Testing Spring asyncResult() and jsonPath() together

会有一股神秘感。 提交于 2019-12-21 03:45:32
问题 I'm using a restful url to kick off a long-running backend process (it is normally on a cron schedule, but we want the ability to kick it off manually). The code below works and I see the result in the browser when I test manually. @ResponseBody @RequestMapping(value = "/trigger/{jobName}", method = RequestMethod.GET) public Callable<TriggerResult> triggerJob(@PathVariable final String jobName) { return new Callable<TriggerResult>() { @Override public TriggerResult call() throws Exception { /

Is there a way to use AssertJ assertions with Spring MVC Test?

﹥>﹥吖頭↗ 提交于 2019-12-21 03:26:13
问题 I have been using AssertJ for some time in my projects. Recently I started using Spring MVC Test for testing Spring MVC controllers. But I am not getting how to use AssertJ with it. All examples I see online all use Hamcrest with Spring MVC Test. Below is an example using the Hamcrest API. mockMvc .perform(get("/user?operation=userList")) .andExpect(status().isOk()) .andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, userList)) .andExpect(view().name(UserController.VIEW

@DataJpaTest needing a class outside the test

喜你入骨 提交于 2019-12-20 18:32:59
问题 In a SpringBoot application, I would like to do some test about the repository layer. @RunWith(SpringRunner.class) @DataJpaTest public class VisitRepositoryTest { @Autowired private TestEntityManager entityManager; @Autowired private VisitRepository visitRepository; ... } When I try to run test from VisitRepositoryTest , I get an error about DefaultConfigService Field defaultConfigService in com.norc.Application required a bean of type 'com.norc.service.DefaultConfigService' that could not be

Spring-Autowiring happens after @BeforeClass when running test with maven-surefire

会有一股神秘感。 提交于 2019-12-20 09:36:02
问题 I have some problems with dependency injection (Spring autowiring) and maven-surefire. The following test works without problems when run in eclipse with TestNG: The service-object is injected, then the @BeforeClass -method is called. @TransactionConfiguration(defaultRollback=false) @ContextConfiguration(locations={"/testContext.xml"}) public class MyServiceTest extends AbstractTransactionalTestNGSpringContextTests { @Autowired private MyService service; @BeforeTest public void setup() {

Spring Boot properties in 'application.yml' not loading from JUnit Test

≡放荡痞女 提交于 2019-12-20 09:10:03
问题 What am I doing wrong? I'm using this little standalone App which runs and finds my src/main/resources/config/application.yml . The same configuration doesn't work from JUnit, see below: @Configuration @ComponentScan @EnableConfigurationProperties public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class); } } @Component @ConfigurationProperties public class Bean{ ... } The following doesn't work, the same properties in application.yml