why application context cannot be loaded? - spring-mvc

can any body explain why should i getting the following error message "Failed to load ApplicationContext" while testing my test cases
mvn test

If your using the Spring framework, you can specify the location of your applicationContext.xml file using the #ContextConfiguration annotation:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "/path/to/your/applicationContext.xml" })
public class MyTest {
}

Check your #ContextConfiguration. Quote from the Spring documentation:
A plain or relative path — for example "context.xml" — will be treated
as a classpath resource that is relative to the package in which the
test class is defined. A path starting with a slash is treated as an
absolute classpath location, for example "/org/example/config.xml". A
path which represents a resource URL (i.e., a path prefixed with
classpath:, file:, http:, etc.) will be used as is.
So I suppose you have to use "/applicationContext.xml" instead of "/src/test/resources/applicationContext.xml" as the file will be placed at the root level in your classpath.

Since I can't see your configuration you could just change your Test to use the following class level annotations. This should help find the application context as long as it is in the classpath.
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration({ "classpath:applicationContext.xml" })

Related

Import custom library from a path using java in robot framework test cases

I have created a custom library consisting of robot keywords. In order to use these keywords i have to specify
Library abc.xyz.<Class_name>
This however does not look clean. I want to just have
Library <class_name>
which seems the standard way. How do I get this to work?
you can specify the path till your custom library in PYTHONPATH environment variables
and then use it like
*** Settings ***
Library abc.java
For more options and information you can refer to below answer as well
Import custom library from a different path in Robot Framework
The name of the library in Robot Framwork consists out of two parts:
Library <Package Path>.<Class_name>
In many cases this means something like:
Library org.company.application.<Class_name>
This is often reflected in the Java Code as:
package org.company.application;
public class SampleKeywordLibrary {
In order to only use the class name in Robot Framework it will be necessary to have no Package Path defined in your Class. Depending upon your editor it may require some changes in your project settings as well.
Java:
public class SampleKeywordLibrary {
public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";
public void MyCustomJavaKeyword() {
}
}
Then the Robot File looks like:
*** Settings ***
Library SampleKeywordLibrary
*** Test Cases ***
TC
My Custom Java Keyword
When you have exported/compiled it to a Jar file, place it where you want to store it and start Robot Framework from within the Jython context similar to this:
C:\Python\jython2.7.0\bin\jython.exe -
J-Dpython.path=C:\Python\jython2.7.0\Lib\site-packages
-J-cp .;C:\TA\Workspace\StackOverflowJython\SampleKeywordLibrary.jar
-m robot.run
-s StackOverflowJython.Test
C:\TA\Workspace\StackOverflowJython

Spring Boot - Loading a .properties file from WEB-INF/ folder

I have a Spring Boot 1.3 application (deployed as .war) that needs to be able read a .properties file from the following location:
WEB-INF/application.properties (outside the classpath, but relative to the app root folder)
...as opposed to:
WEB-INF/classes/application.properties(inside the classpath, gets loaded automatically)
What worked in Spring Boot 1.3 was the following #PropertySource annotation:
#SpringBootApplication
#PropertySource(value = {"WEB-INF/application.properties"})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
It correctly fetched the .properties file relative to the app root. However that stops working after an update to Spring Boot 1.4.0.RC1.
Since then I've tried the following:
#PropertySource("classpath:../application.properties")
#PropertySource("file:WEB-INF/application.properties")
and also
spring.config.location=classpath:../
spring.config.location=file:src/main/webapp/WEB-INF/
spring.config.location=WEB-INF/application.properties
But haven't had any luck loading the .properties.
I'd normally put the .properties file inside the classpath, but in this case this is NOT an option due to the way our deployment works on a remote location.
I'd also prefer to not use an absolute path, as that'll be a nightmare to support with multiple customers.
Edit: Just to be clear - the .properties I'd like to read aren't located outside the JAR (in my case - WAR) file, but inside - just not on the classpath, but directly in the WEB-INF/ folder where normally other resources (pages, images) would be.
As I mentioned in duplicate SO question:
Put this line into your application.properties:
logging.config=file:log4j.xml
Second option is to pass system variable to -Dlogging.config=file:log4j.xml
In this case it is expected to be located in current directory outside of the JAR file.
REACTION ON COMMENT:
If you are using WAR file, your main class is not used at all. So PropertySource annotation doesn't have any effect there.
If the .properties is packed in the .war file. Then you can try the following (assuming that the WEB-INF directory is located in the root of the .war file.
#PropertySource("classpath:/WEB-INF/conf/application.properties")
Turns out this issue was caused by a bug with the SpringBootTestContextBootstrapper in Spring Boot 1.4.0.RC1: https://github.com/spring-projects/spring-boot/issues/6371

Wrap resources inside custom class and invoke class?

I am using Puppet 3.0. I tried to use the existing NGINX module but encountered one issue which no answer seems to exist. I have moved to implementing NGINX module myself which handles my exact requirements.
Each package/service/file resource works as expected and now I am moving the code out of init.pp into a /manifest/nginx.pp fileas a class:
class company_nginx {
... Various resources
}
Now I am trying to include this class inside the init.pp
include company_nginx
An it returns an error:
Error: Could not find class nginx for localhost on node localhost
What step or concept am I missing? How do I invoke this "class" in the init.pp file to get the resources invoked and configuring a system???
Manifest file names and the classes defined within them must exactly match. If you want to have a class names company_nginx then it must be in a file names company_nginx.pp.
This required comes from the puppet autoloader.
Your class names must be qualified for Puppet to be able to pick them up.
class nginx::company_nginx { ... }
This class will be loaded from module nginx file manifests/company_nginx.pp.
Doing include company_nginx will make Puppet assume that there is an actual module names company_nginx with the class defined in init.pp.

Robolectric #Config path value for manifest

I am trying to run robolectric unit test but I am getting error as AndroidManifest.xml not found on path. Can anyone give me an example path of #Config manifest value. Is it relative path or absolute one?
Thanks in advance
If you are using maven to run your tests you can set as follows:
#Config(manifest = "../app/AndroidManifest.xml")
#RunWith(RobolectricTestRunner.class)
public class SomeTestCase { ... }
Please note that if you are using Android Studio/Intellij and want to run your tests within the IDE you will have to make a change in the Run configuration. In Run->Edit configuration->Defaults->JUnit->Working directory set the value $MODULE_DIR$ and Android Studio will set the relative path in all junits just like Maven.
This worked for me however if the annotation fails, you can also create a file called "org.robolectric.Config.properties" and place it on your classpath.
The file should contain something like the following:
"manifest: ./app/AndroidManifest.xml"
More information can be found here: Configuring Robolectric 2.0
A simple configuration using maven can be found here: Simple Robolectric

trying to use log4j.xml file within WinRun4j

has anyone tried to use a log4j.xml reference within a WinRun4j service configuration. here is a copy of my service.ini file. I have tried many configuration combinations. this is just my latest attempt
service.class=org.boris.winrun4j.MainService
service.id=SimpleBacnetIpDataTransfer
service.name=Simple Backnet IP DataTransfer Service
service.description=This is the service for the Simple Backnet IP DataTransfer.
service.startup=auto
classpath.1=C:\Inbox\DataTransferClient-1.0-SNAPSHOT-jar-with-dependencies.jar
classpath.2=WinRun4J.jar
classpath.3=C:\Inbox\log4j-1.2.16.jar
arg.1=C:\Inbox\DataTransferClient.xml
log=C:\WinRun4J-Service\SimpleBacnetIpDataTransfer\NBP-DT-service.log
log.overwrite=true
log.roll.size=10MB
[MainService]
class=com.shiftenergy.ws.App
vmarg.1=-Xdebug
vmarg.2=-Xnoagent
vmarg.3=-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n
vmarg.4=-Dlog4j.configuration=file:C:\Inbox\log4j.xml
within the log4j.xml file, there is reference to a log file for when the application runs. if I run the java -jar -Dlog4j.configuration=file:C:\Inbox\log4j.xml ...., the log file is created accordingly. if I register my service and start the service, the log file does not get created.
has anyone had success using the -D log4j configuration, using winrun4j?
thanks
I think that you provided the vmarg.4 parameter incorrectly. In your case it has to be like:
vmarg.4=-Dlog4j.configurationFile=[Path for log4j.xml]
I am also using the same and in my case, it is working perfectly fine. Please see below example:
vmarg.1=-Dlog4j.configurationFile=.\log4j2.xml
Have you tried setting the path in your code instead:
System.setProperty("log4j.configurationFile", "config/log4j.xml");
I'm using a relative path to a folder named config that contains log4j.xml. An absolute path is not recommended, but may work as well.
Just be sure to set this before making any calls to log4j, including any log4j config settings or static method calls!
System.setProperty("log4j.configurationFile", "config/log4j.xml");
final Logger log = Logger.getLogger(Main.class);
log.info("Starting up");
I didn't specify the log4j path in the ini file, only placed log4j.xml file at the same place the jar was placed.
Also without specify the
System.setProperty("log4j.configurationFile", "config/log4j.xml");
In the Java project it was stored in (src/main/resources) and will be included in the jar, but it will not be that one used if placed outside the jar.

Resources