Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringRunner ignores application properties

I am trying to create Spring integration tests as follow:

@RunWith(SpringRunner::class)
@ActiveProfiles(profiles = ["Test"])
@ContextConfiguration(locations = ["classpath:**/applicationContext.xml"])
open class SimpleEntityIT {...}

applicationContact.xml contains:

    <context:annotation-config/>
    <context:spring-configured/>
    <context:property-placeholder
            ignore-resource-not-found="false"
            location="classpath:application${spring.profiles.active}.properties,classpath:application.properties"/>

    <context:component-scan base-package="net.goout"/>

The applicationContext is loaded, but it seems it is mostly ignored. Beans are not constructed via component-scan and the application.properties are completely ignored, no mention in logs:

 2018-01-26 20:09:26,131 DEBUG Resolved location pattern [classpath:**/applicationContext.xml] to resources []
 2018-01-26 20:09:26,132 DEBUG Loaded 0 bean definitions from location pattern [classpath:**/applicationContext.xml]
 2018-01-26 20:09:26,167  INFO Refreshing org.springframework.context.support.GenericApplicationContext@aecb35a: startup date [Fri Jan 26 20:09:26 CET 2018]; root of context hierarchy
 2018-01-26 20:09:26,167 DEBUG Bean factory for org.springframework.context.support.GenericApplicationContext@aecb35a: org.springframework.beans.factory.support.DefaultListableBeanFactory@20d3d15a: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory]; root of factory hierarchy
 2018-01-26 20:09:26,198 DEBUG Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
 2018-01-26 20:09:26,198 DEBUG Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
 2018-01-26 20:09:26,225 DEBUG Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
 2018-01-26 20:09:26,231 DEBUG Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'

What am I not getting?

EDIT: Not event component-scan beans are not constructed, but really no beans are constructed – even those defined by <bean> in applicationContext.xml. It seems its content is just ignored even though it is correctly found.

like image 471
Vojtěch Avatar asked Oct 16 '22 23:10

Vojtěch


2 Answers

As per the latest comments i believe this issue is solved, but even then couple of quick observations and alternatives :

a) You did not get an error on missing resource is because of below attr,i would suggest making it true to identify issues early on :

ignore-resource-not-found="false"

Also, you could use below :

@PropertySource(value = "xyz.properties", ignoreResourceNotFound = true)

b) Completely unrelated but :

@ActiveProfiles(profiles = ["Test"])

Could be written as :

@ActiveProfiles({"Test","QA"}) 

or in your application-test.properties

spring.profiles.active=Test,QA

c)For placing applicationContext.xml ,an alternative can also be placed in web.xml :

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/spring/*.xml</param-value>
</context-param>

(Although, annotation with specifying the exact location with @ContextConfiguration is completely fine as well! )

And above all , if you are using Annotation based configs, i would advice Spring boot :-)

Hope it helps!!

like image 76
Ashwini Rao Avatar answered Nov 15 '22 13:11

Ashwini Rao


You should not copy applicationContext.xml to /test/resources yourself. Allow this to maven

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
        </testResource>
    </testResources>
</build>
like image 33
Andriy Slobodyanyk Avatar answered Nov 15 '22 11:11

Andriy Slobodyanyk