Spring JUnit ContextConfiguration class path resource cannot be opened - spring

I am using the latest version of Spring-Boot (spring boot starter web and test), in a gradle project. And this older unit test I use to have is no longer working.
The XML context file is located under src/test/resources/myTestContext.xml. But I get the error below when I try to run this as a Junit test in Eclipse (mars 4.5)
#ContextConfiguration(locations={"/myTestContext.xml"})
public class MyTest extends AbstractJUnit4SpringContextTests{
....
Caused by: java.io.FileNotFoundException: class path resource [myTestContext.xml]
cannot be opened because it does not exist at
org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) ~[spring-core-4.1.7.RELEASE.jar:4.1.7.RELEASE]

Related

Spring boot testing : NoClassDefFound exception

I'm using the latest version of Spring boot and an old test fails.
I'm having
Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class
Caused by: java.lang.NoClassDefFoundError: MyClass
My config is
spring boot 2.7.2
gradle 7.5
a structure using idiomatic gradle (multiproject and composite build)
The subproject failing is the spring-boot-application. The class not found exists inside the src/main/java
The test failing is under src/test/java, annotated with
#SpringJUnitConfig(classes = ApplicationServiceAspectTestConfigSuccessListener.class) because it is using #Inject fields.
I understand it is not perfect (unit testing depending on injection ; prefer integration test)
When I debug this test, the classloader tries to use a JarLoader and searches inside the spring-boot-application-default.jar. The jar structure is BOOT-INF / META-INF / org... The class not found is inside the BOOT-INF/classes/org/xxxx. The jar loader failed :/
My question is : why searching inside the jar and not the build/classes/org/xxx ? and why is the JarLoader not finding the class ?
Update 1
I think I have a lead. I created a gradle precompiles quality plugins which uses java-text-fixtures. When enable the java-text-fixtures produces two jars and the problem comes from here.
I don't really understand why for the moment (why 2 jars and not only the text-fixtures one).
Update 2
Ok seems I ran across https://github.com/gradle/gradle/issues/11696

Spring Boot scans Test configuration classes on start up

I have a Spring Boot App with the main class as below.
#SpringBootApplication
#EnableAspectJAutoProxy(proxyTargetClass = true)
#EnableAutoConfiguration
public class Application extends SpringBootServletInitializer
The project structure is
App/
src/main/java
src/test/java
In the test package , I have a configuration class used for Integration testing and the class is marked as #TestConfiguration. I am noticing that this class is getting picked up on the server startup. I am using profile as dev on start up. Any idea why the test configuration classes are getting loaded on startup of the application. I need these classes to be scanned only during test phase.
This works well when i run java -jar command from command line, but fails during start up from STS eclipse.

How to include a standalone jar that uses spring and beans as a dependency in maven project for jenkins plugin?

I want to include a standalone jar file (which uses spring), inside my java code to develop a jenkins plugin. When I add this as a dependency and I run the plugin ,I get errors like: Caused: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/XXX/YYY/jmsclient3/XXXclient-jms-config.xml]
It says cant find the class path resource [com/XXX/YYY/jmsclient3/XXXclient-jms-config.xml], because it doesnt exist.
But this file is clearly there in the jar. Its a bean file(I dont know much about it).

how to #autowire dao class created in project A to the rest controller class in Project B

I created a Springboot application with dao class, model and rest controller. it works fine.
now i want to create rest controller in separate project.
Project A : model + dao class
Project B: rest controller
in Project B pom.xml I included the Project A jar file under dependency
Now i m trying to autowire dao class from project A to the restcontroller class in Project B.
In the rest controller class (project B) i used #ComponentScan(basePackages="package where my daoclass is there")
But i am getting compilation error saying cannot resolve the symbol for the line daoclass dao;
I am new to spring and springboot. Not sure what is wrong.. please help me to understand it better
I am trying to do mvn clean package (project B)
You need to register the class as a bean in the spring context with the applicationContext.xml for projectB, otherwise projectB can't find the bean created by projectA.
For example, you should write following applicationContext.xml
<context:component-scan base-package="name.space.for.projectA"/>
You can also configure component-scan with annotations:
18. Using the #SpringBootApplication annotation
(a) Specify target packages as scanBasePackages
#SpringBootApplication(scanBasePackages={"name.space.for.projectA"})
or
(b) Specify target packages as #ComponentScan
#SpringBootApplication
#ComponentScan("name.space.for.projectA")
Check your jar file exists in local repository.
Open your local repository folder in setting files
<localRepository>Your Directory</localRepository>
And check your jar file is in there.
If file is not exist, try copy and paste it first.
And if Project B is Okay, then check your maven setting file.
Maven will always look in your local repository (usually found in ~/.m2/repository) for dependent jars.
Therefore you must execute:
mvn clean install
in projectA. This will copy your projectA-version.jar into your local repository, where projectB will be able to find it.

IncompatibleClassChangeError - Uprade to Spring 4.1.7

We have a tomcat application that has a spring container running on 3.0.0. Upgraded to spring 4.1.7, and modified certain classes appropriately. One of the beans that we modified used CronTriggerBean. That was changed to use CronTriggerFactoryBean, and also upgraded to org.quartz-scheduler to 2.2.1. When deploying and starting tomcat, encountered the following exception.
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'appsecScheduler' defined in class path resource [application-
context.xml]: Initialization of bean failed; nested exception is
java.lang.IncompatibleClassChangeError: Implementing class at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean
(AbstractAutowireCapableBeanFactory.java:547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean
Attached a debugger and found the offending class to be CronTriggerFactoryBean. But this issue happens only when deploying in a Windows environment. WHen the same war is deployed in Ubuntu, the application starts without any errors. For the record, I did a dependency:tree, dependency:build-classpath and ensured that 3.0.0 spring was not in use. The CronTriggerFactoryBean is part of spring-context-support.
Any suggestions on how to debug further or determine which jar is being used?

Resources