Configuring base package for component scan in Spring boot test

Good to add the test config above I have the following in test config and any test case. I am new to spring boot test but it work. Let me know, if I am wrong. @Configuration @ComponentScan(“au.some.spring.package”) public class TestConfig { } @RunWith(SpringRunner.class) @EnableAutoConfiguration @SpringBootTest(classes= TestConfig.class) @TestPropertySource({“classpath:application.yml”, “classpath:env-${testing.env}.properties”}) public class DBDmoTest { @Autowired PartyRepository partyRepository; … Read more

Spring Boot. @DataJpaTest H2 embedded database create schema

I had the same issue, I managed to resolve by creating schema.sql (in resources folder) with the content CREATE SCHEMA IF NOT EXISTS <yourschema> Documentation can be found here but imho the lack of real examples make it very complex. Warning: this script is also executed within the normal (not test) environment. Not mandatory, but … Read more

Spring Test returning 401 for unsecured URLs

I found the answer Spring docs says that: @WebMvcTest will auto-configure the Spring MVC infrastructure and limit scanned beans to @Controller, @ControllerAdvice, @JsonComponent, Filter, WebMvcConfigurer and HandlerMethodArgumentResolver. Regular @Component beans will not be scanned when using this annotation. And according to this issue in github: https://github.com/spring-projects/spring-boot/issues/5476 The @WebMvcTest by default auto configure spring security if … Read more

request scoped beans in spring testing

Solution for Spring 3.2 or newer Spring starting with version 3.2 provides support for session/request scoped beans for integration testing. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestConfig.class) @WebAppConfiguration public class SampleTest { @Autowired WebApplicationContext wac; @Autowired MockHttpServletRequest request; @Autowired MockHttpSession session; @Autowired MySessionBean mySessionBean; @Autowired MyRequestBean myRequestBean; @Test public void requestScope() throws Exception { assertThat(myRequestBean) .isSameAs(request.getAttribute(“myRequestBean”)); assertThat(myRequestBean) .isSameAs(wac.getBean(“myRequestBean”, … Read more

Spring Test session scope bean using Junit

I came across this simpler approach, thought I might as well post here in case others need it. <bean class=”org.springframework.beans.factory.config.CustomScopeConfigurer”> <property name=”scopes”> <map> <entry key=”session”> <bean class=”org.springframework.context.support.SimpleThreadScope”/> </entry> </map> </property> </bean> With this approach, you don’t have to mock any request/session objects. Source: http://tarunsapra.wordpress.com/2011/06/28/junit-spring-session-and-request-scope-beans/ Update for spring 5.x (works in spring 5.3.22, in a JUnit5 … Read more

Using Spring mockMvc to test optional path variables

Using an array of @RequestMapping values like this … @RequestMapping( value = {“/some/uri/{foo}”, “/some/uri/{foo}/{bar}”}, method = RequestMethod.PUT) public ResponseEntity<String> someMethod( @PathVariable(“foo”) String foo, @PathVariable(value = “bar”, required = false) String bar ) { return new ResponseEntity<>(foo + ” and ” + (bar == null ? “<null>” : bar), HttpStatus.OK); } … will enable this test … Read more

@Import vs @ContextConfiguration in Spring

@Import and @ContextConfiguration are for different use cases and cannot be used interchangeability. The @Import is only useful for importing other @Configuration files and is only useful (and afaik) and functional on @Configuration classes. When putting the @Import on a test class it will be no good as it won’t be processed. @Configuration @Import(PersistenceConfig.class) public … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)