spring-test

How can I use @IfProfileValue to test if a Profile is active?

谁说胖子不能爱 提交于 2019-11-29 14:20:42
问题 So confusingly @IfProfileValue has nothing to do with @Profile or @ActiveProfiles . @Profile tests to see if a profile is active, @ActiveProfiles sets them as active, and @IfProfileValue allows you to check things in Spring Environment . Wut? I'd deprecate all of them and add new ones @IfEnvironment , @IfProfile , and @ActivateProfiles . Commentary aside, how can I use @IfProfileValue to detect whether i have a profile active? I am not using Spring Boot on this project, at this time. Answers

Spring Test returning 401 for unsecured URLs

孤街醉人 提交于 2019-11-29 13:13:53
I am using Spring for MVC tests Here is my test class @RunWith(SpringRunner.class) @WebMvcTest public class ITIndexController { @Autowired WebApplicationContext context; MockMvc mockMvc; @MockBean UserRegistrationApplicationService userRegistrationApplicationService; @Before public void setUp() { this.mockMvc = MockMvcBuilders .webAppContextSetup(context) .apply(springSecurity()) .build(); } @Test public void should_render_index() throws Exception { mockMvc.perform(get("/")) .andExpect(status().isOk()) .andExpect(view().name("index")) .andExpect(content().string(containsString("Login"))); } }

Fails to load Spring application context in tests with SpringJUnit4ClassRunner

試著忘記壹切 提交于 2019-11-29 12:43:49
I can't figure out why Spring can't load the application context in a test class defined as follows: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/springMVC-config/mainController-servlet.xml"}) public class DiagnosticsControllerTest { @Mock DiagnosticsService diagnosticsService; private DiagnosticsController diagnosticsController; private MockMvc mockMvc; @Before public void setUp() { MockitoAnnotations.initMocks(this); diagnosticsController = new DiagnosticsController(); diagnosticsController.setService(diagnosticsService); this.mockMvc = MockMvcBuilders

How can I mock db connection in Spring Boot for testing purpose?

谁说我不能喝 提交于 2019-11-29 11:59:29
问题 Situation: I am using Spring Cloud with Spring Boot in a microservice, that microservice is loading a DB config information to configure a connection. I created a test to get the rest interfaces using Swagger for documentation. I want to disable the loading of DB configuration because is not necessary. Here is the code: @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {Application.class, Swagger2MarkupTest.class}, loader =

How to integration test auto configuration for a custom Spring Boot style starter library?

心已入冬 提交于 2019-11-29 10:59:16
I am writing a library to provide some functionality that is shared between multiple different Spring Boot applications that I work with. I would like to do something similar to the auto-configuration that is provided by the many Spring Boot starter libraries exist. That, or some other simple declarative way to integrate my library with the ApplicationContext of the apps using it. I have found some resources explaining how auto configuration works. I can figure out the above problem. However, I have not been able to find any good examples of how I can test as part of my library's test suite

How to get @WebMvcTest work with OAuth?

ぃ、小莉子 提交于 2019-11-29 07:59:14
I just had hard time to get my controllers unit tests working because, IMO, what is in Spring doc is not enough if using OAuth. In my case, it's Oauth2 with JWT. I tried to use @WithMockUser , @WithUserDetails and even define my own annotation with @WithSecurityContext and a custom UserSecurityContextFactory but always got anonymous user in the UserSecurityContext when security expression where evaluated, whatever I set the test context to in my factory... I propose the solution I came to just under, but as I'm not sure mocking the TokenService is the most efficient / clean way to go, please

Mockito Exception - when() requires an argument which has to be a method call on a mock

谁说我不能喝 提交于 2019-11-29 05:25:56
I have a very simple test case that is using Mockito and Spring Test framework. When I do when(pcUserService.read("1")).thenReturn(pcUser); I get this exception. org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles); Also, this error might show up because: 1. you stub either of: final/private/equals()/hashCode() methods. Those methods *cannot* be stubbed/verified. 2. inside when() you don't call method on mock but on some other object. at com.project

@TestPropertySource with dynamic properties

老子叫甜甜 提交于 2019-11-29 02:15:31
I am using @TestPropertySource to overwrite application.yml properties in my integration test for a spring boot app. @TestPropertySource(properties = { "repository.file.path=src/test/resources/x" }) I was wondering if there was some way to make the property VALUE dynamic. Something like this: @TestPropertySource(properties = { "repository.file.path=PropertyValueProvider.class" }) Your feedback is appreciated. In my case the property value is system specific that should be generated upon the test run. Sam Brannen @TestPropertySource only provides declarative mechanisms for configuring

Error: Unable to find @SpringBootConfiguration when doing @WebMvcTest for Spring Controller

久未见 提交于 2019-11-29 00:26:35
问题 I am testing my controller given below @Controller public class MasterController { @GetMapping("/") public String goLoginPage(){ return "index"; } } I am following this Spring documentation to test my controller. Now, I want to test my controller by just instantiating the web layer and not the whole Spring context as given in the documentation. Below is my code for the same. @RunWith(SpringRunner.class) @WebMvcTest public class MasterControllerTestWithWebLayer { @Autowired MockMvc mockMvc;

How to set environment variable or system property in spring tests?

蹲街弑〆低调 提交于 2019-11-28 19:59:30
I'd like to write some tests that check the XML Spring configuration of a deployed WAR. Unfortunately some beans require that some environment variables or system properties are set. How can I set an environment variable before the spring beans are initialized when using the convenient test style with @ContextConfiguration? @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:whereever/context.xml") public class TestWarSpringContext { ... } If I configure the application context with annotations, I don't see a hook where I can do something before the spring