jhipster java tests failing - application properties - java

I have generated a jhipster monolithic app. I have created a class to connect with the AWS S3 and upload a file there. I defined the properties in .yml file. Here everything works fine.
When I am trying to run the provided tests, most of them are failing with the following error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 's3AutoConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'aws.endpoint.url' in value "${aws.endpoint.url}"
S3AutoConfig is the class which uses the properties.
I checked jhipster's documenation and several posts, like the one below:
Adding applicationproperties in Jhipster
which mention that you should provide the properties in the ApplicationProperties class (seems a bit redundant).
I defined the properties also in the java class, but the tests are still failing with the same error above.
How should I define the properties, so they are picked up by the tests? Is it necessary to provide them also in the java class as some posts suggest?

Your implementation cannot work because you are defining Aws class within ApplicationProperties which means that your AWS properties will be prefixed by application, so for example application.aws.endpoint.url which does not match your application*.yml structure and this is why you get this error.
You should extract Aws class and its inner classes to its own file (Aws.java) and use prefix "aws".
Also, it would probably better named as AwsProperties.
#ConfigurationProperties(prefix = "aws", ignoreUnknownFields = false)
public class Aws {
Then the second point about tests is that they are using a different classpath than main class so you should ensure that you define these properties also in src/test/resources/config/application.yml

Related

Holders.config is null during grails run-war

Recently did a log4j upgrade on grails 2.5.4 application. Following dependencies pertaining to v2.17.1 have been added:
log4j-api
log4j-core
log4j-1.2-api
log4j-slf4j-impl
I've excluded the following dependencies from the global level:
log4j
grails-plugin-log4j
jcl-over-slf4j
The application boots up without any issues on doing grails run-app. But when I try to run the war by grails run-war or deploy the war using tomcat, I keep getting:
Error creating bean with name 'grailsApplication' defined in ServletContext
...
Caused by: java.lang.ExceptionInInitializerError
...
Caused by: java.lang.NullPointerException: Cannot get property 'xyz' on null object
This null object is Holders.config
This issue occurs during static property initialization. The class looks like:
class Test {
public static Logger log = LoggerFactory.getLogger(this)
private static final String SOURCE_URL = Holders.config.xyz.abc
...
}
Another class had the same issue during the initialization of variable using Holders.config in static block.
Strangely, this issue is only for classes under grails-app/utils folder because I've another class under src/groovy where its variables are defined in a similar way but there's no issue with it during the war file execution.
There's no change in code except for the dependencies added/removed as mentioned above.
I also tried using applicationContext.getBean('grailsApplication).config.xyz.abc but it said Could not find ApplicationContext, configure Grails correctly first.
Can someone please suggest what could be the root cause or guide how I can fix or maybe initialize these static values.

Spring boot integration test error: "Could not resolve placeholder 'wiremock.server.port'" in tests not requiring Wiremock

I have a #SpringBootTest which tests the loading of a #ConfigurationProperties and its methods. It is in test source set.
And, in itest, I have an integration test which uses Wiremock(sending requests and using stubs as response, etc.)
Now, when I run gradle test, the first test fails, saying:
[ENV=local] [productName=app-gateway-api] [2019-10-22T16:18:30.994Z] [ERROR] [MSG=[Test worker] o.s.boot.SpringApplication - Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myController' defined in file [E:\coding\code\app\build\classes\java\main\com\app\controller\MyController> .class]: Unsatisfied dependency expressed through constructor
parameter 0; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'proxyService': Injection of autowired
dependencies failed; nested exception is
java.lang.IllegalArgumentException: Could not resolve placeholder
'wiremock.server.port' in value
"http://localhost:${wiremock.server.port}/send"
Which is used in some external properties. The value will be used in itest, but not in test. But the context loads it always.
In other projects using it like this, there is no problem. But, it seems that it is loading all the classes and cannot find Wiremock and create the server.
The test in problem:
#SpringBootTest
public class MapperLookupTest {
...
}
Wiremock dependency already has compile scope:
compile('com.github.tomakehurst:wiremock-jre8-standalone:2.21.0')
compile("org.springframework.cloud:spring-cloud-starter-contract-stub-runner")
I have tried to load only the necessary classes with #SpringBootTest(classes = {...}), but it is too verbose.
So, is there any easy way to tell the context to load Wiremock?
Just add a default value to your placeholder:
${wiremock.server.port:defaultValue}
At last I decide not to honor #Cwrwhaf, sorry.. because I don't want to have a possible negative impact on the production code; I just want to pass the test and any changes should be limited to the mere scope of the test itself.
So I did:
#SpringBootTest(property = "wiremock.server.port=8080")
And I am aware of the fact that it is not the perfect answer, but has less impact than changing global settings of wiremock.

Spring Boot 2 Upgrade not able to resolve class name as property

We have an application where one of our internal libraries has defined a bean like this
<bean id="myBean" class="${myBean.type}"/>
We have a Spring Cloud Config Server which feeds properties to this application on startup, which also contains the property myBean.type. This setup is currently working fine with no issues. I then made the following change to my pom
Earlier
<spring.boot.version>1.5.16.RELEASE</spring.boot.version>
<spring.cloud.version>Edgware.RELEASE</spring.cloud.version>
<spring.version>4.3.19.RELEASE</spring.version>
Now
<spring.boot.version>2.0.9.RELEASE</spring.boot.version>
<spring.version>5.0.13.RELEASE</spring.version>
<spring.cloud.version>Finchley.SR2</spring.cloud.version>
Then I started getting this error on startup
An attempt was made to call the method org.springframework.beans.factory.support.BeanDefinitionBuilder.addConstructorArg(Ljava/lang/Object;)Lorg/springframework/beans/factory/support/BeanDefinitionBuilder; but it does not exist. Its class, org.springframework.beans.factory.support.BeanDefinitionBuilder, is available from the following locations:
jar:file:/I:/Library/MavenRepository/org/springframework/spring-beans/5.0.13.RELEASE/spring-beans-5.0.13.RELEASE.jar!/org/springframework/beans/factory/support/BeanDefinitionBuilder.class
It was loaded from the following location:
file:/I:/Library/MavenRepository/org/springframework/spring-beans/5.0.13.RELEASE/spring-beans-5.0.13.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.beans.factory.support.BeanDefinitionBuilder
Looking at this, I excluded the artifact org.apache.cxf:cxf-api:jar:2.7.18:compile from all the jars that depended on it & upgraded cxf jars versions to 3.2.5. Now the startup is going ahead but it is giving me the following error:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [${myBean.type}] for bean with name 'myBean' defined in class path resource [xyz.xml]; nested exception is java.lang.ClassNotFoundException: ${myBean.type}
I am not sure if removal of cxf-api is causing this issue, or upgrade of Spring Boot 2, or is it something else that is going wrong here!

Unable to read application.properties in spring boot application

I am unable to read application.properties value in my project in classes that are injected as a bean from another project. My project is using another project which has classes which needs to read configuration from application.properties.
Mine is a maven project and a spring boot application having application.properties in src/main/resources folder and those properties values are defined in it.
What is it that I am missing that it is unable to read file values? Or is there is any other mechanism for setting these properties for classes loaded via component-scan
Below line of code works fine, it is able to read value from the application.properties:
#PostConstruct
void init() throws ClassNotFoundException, IOException {
System.out.println(context.getEnvironment().getProperty("env.host",
"default value"));
}
Now there is another project which I am using in mine. When loading the classes those beans are getting initialized as dependency of another class, there also it tries to read same value in constants file as
static final String HOST_PROPERTY = "${env.host}";
There this value is not getting initialized to value from applictaion.properties
If you want to get application.properties values,you have to two option.
1) you can autowire Environment class and use its getProperty() method as
#Autowired
Environment env;
env.getProperty("${env.host}");
2)#Value annotation
#Value("${env.host}")
String HOST_PROPERTY;

ConflictingBeanDefinitionException even after deleting a class

I am developing a simple maven + spring application and i am getting the following error. It says two of my classes have a conflict. so i deleted the second class but i am still getting the same error. I tried restarting the server but it still says my class exists. Can somebody help?
Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.nibm.config.RootConfig]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'employeeController' for bean class [com.nibm.hibernate.controller.EmployeeController] conflicts with existing, non-compatible bean definition of same name and class [com.nibm.controller.EmployeeController]
I was able to solve a similar problem by using IntelliJ's function "Rebuild".
The reason was an orphan .class file after the corresponding .java file had already been deleted.
You get the exception because you have two spring beans of the same class.
This exception is thrown by
org.springframework.context.annotation.ClassPathBeanDefinitionScanner#isCompatible
And looking at that implementation it looks as if you create a bean of type EmployeeController in your RootConfig and additionally by ComponentScan.
To fix the problem remove the bean from the RootConfig or change your ComponentScan, so this bean is not found by it.
You can set a breakpoint in the constructor of EmployeeController. From the stack you can get more information about how and why the bean is created.
I faced the same problem and it was because class with same name exist at two locations as mentioned in the Exception itself which are conflicting and after removing one issue got fixed.
I was getting this same ConflictingBeanDefinitionException..."conflicts with existing, non-compatible bean definition of same name and class" when running JUnit tests with #RunWith(SpringRunner.class)/ #SpringBootTest from inside Intellij.
Execution via gradle:build of the same tests were running fine.
This began to happen after I had refactored the packaging of several #Components which lead me to believe something was holding a reference to the class under it's previous package name.
No amount of gradle build/clean would seem to clear it.
Doing a Build -> Rebuild Project in IntelliJ was what cleared this issue for me.

Categories

Resources