I have a problem with maven and JUnit testing.
I have some files in src/main/resources and when running junit tests I want to these files. Only one test specific file with slightly different settings should overrule a corresponding file.
So my idea was to give this test-file the same name like the main-file and put it file under src/test/resources at the same (corresponding) place like the main-file.
But now I have the problem, that I cant use all the other files from src/main/resources.
I thought they junit test would coppy them default into target/test-classes/ when running ernriced by the files from src/test/resources, but it doesnt. There is only the file from src/test/resources and not other.
Thanks for any ideas, how I could solf this problem.
here an example how I try to access the files
'
#BeforeClass
public static void globalSetUp() throws NamingException, SQLException {
System.setProperty("solr.solr.home", "/solr/");
cores = new CoreContainer(
"/home/foo/workspace/reporting/target/test-classes/solr");
cores.load();
server = new EmbeddedSolrServer(cores, "reporting");
loadDriver();
connection = createAndConnectToDB();
createDBSchema();
}
'
The problem was in pom.xml.
/main/resources was not as testResource.
Here's the snippet that solved my problem:
<build>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
<testResource>
<directory>${project.basedir}/src/main/resources</directory>
</testResource>
</testResources>
</build>
Actually both are there / available. The files in src/main/resources and src/test/resources. However when loading files from the classpath, any file in src/test/resources takes precedence over the files in src/main/resources.
It it difficult to fully answer your question since you did not provide a lot of details (are you using ContextConfiguration? If not how are you loading the files) but I would suggest that you use a different name and load that file explicitly in the one test that needs it.
You could also have the same name but then all other tests would need to explicitly use the src/main/resources path to load the default file.
Related
I have a following project structure
bin
start.sh
db
liquibase_scripts
...
schema.json
main
java
...
test
resources
liquibase_scripts
...
schema.json
So than I build my project, folder db with liquibase scripts added to distributive.
In unit tests I use H2 database and want to load schema from db/liquibase. I create bean
#Bean
public SpringLiquibase springLiquibase() {
SpringLiquibase springLiquibase = new SpringLiquibase();
springLiquibase.setDataSource(dataSource());
springLiquibase.setChangeLog("classpath:/liquibase/sam.json");
return springLiquibase;
}
The problem is that method setChangeLog look at resource folder in test folder.
So to solve the problem I copied all liquibase scripts to the test/resources directory.
But this is not ok becouse now I have 2 copies of scripts in different folders.
Is there a way to force springLiquibase.setChangeLog find scripts in any folder not only in test/resources?
In Maven build configuration you can define testResources, which may be directories and files. It looks like this:
<build>
<testResources>
<testResource>
<directory>${project.basedir}/db/liquibase_scripts</directory>
</testResource>
</testResources>
</build>
With such configuration Maven copies the files into the target/test-classes directory. Thanks to that those files can be used as test resources in the code, but there's no duplication in the project files.
Im using such configuration (Liquibase + testResources) in one of my projects. To better reproduce your problem, I've created a separate branch, where you can find the configuration described above - see: this commit diff. All test pass after the change.
I am trying to write a Dropwizard application and its doc tells me that I need to ship everything as an uber jar.
However, in my application I need to support multiple databases and this requires multiple database JDBC driver jars in my classpath, all of which are not expected to be shipped together with my application. Users are expected to place the corresponding JDBC jar like mysql-connector-java-5.1.39.jar in a particular folder by their own.
After reading Dropwizard's documentation I am not sure if this kind of usage is supported. Does anyone have experience making it to work this way?
Since java 6, you can wildcard classpaths.
Using the application plugin, the generated bin folder will have a start script that contains the classpath. What we want to do, is to instead of listing every possible jar in the bin folder, we simply include all of them.
Note: You can also do the same thing with different folders if you want the classpath in a different location.
This can be achieved (in a workaround manner since there are problems with this plugin in my version) in the easiest way as follows. In build.gradle you do:
startScripts {
doLast {
def windowsScriptFile = file getWindowsScript()
def unixScriptFile = file getUnixScript()
windowsScriptFile.text = windowsScriptFile.text.replaceAll('CLASSPATH=.*', 'CLASSPATH=\\$APP_HOME/lib/*')
unixScriptFile.text = unixScriptFile.text.replaceAll('CLASSPATH=.*', 'CLASSPATH=\\$APP_HOME/lib/*')
}
}
This will wildcard your lib folder in the start scripts. When starting up, your classpath will simply be
lib/*
When you drop jars into that folder, they will automatically be picked up (on startup, not on runtime).
I hope this helps,
Artur
During a pytest fixture, what is the best way to robustly get the location of a text file for users that may specify different working directories at runtime e.g. I want a person using the cmd line in the test fixture directory find the file as well as an integration server which may work in the project's root. Can I somehow include the text file in a module? What are best practices for including and getting access to non .py files?
I am aware of BASE_DIR = os.path.dirname(os.path.dirname(__file__)), but I am not sure if this will always refer to the same directory given a particular way of running the test suite.
os.path.dirname(os.path.abspath(__file__)) (which is what I think you meant above) worked fine for me so far - it should work as long as Python can figure the path of the file, and with pytest I can't imagine a scenario where that wouldn't be true.
I have a solution divided into two projects, one for a Class Library and another one for Unit Tests (using NUnit 2.5). Now, on the App.config file of the Class Library project I added several lines like
<add key="KeyName" value="KeyValue"/>
which I am reading in the class library code with
ConfigurationManager.AppSettings["KeyName"].
The problem is that when I run the unit tests the class library can't access those values because even though that code is used from within the library, it goes looking for them in the App.config of the Unit Tests project. If I add those line to this file everything goes smooth, but that's of course not what I want. How can I tell ConfigurationManager to look for the keys in the right application path?
use ConfigurationManager.OpenExeConfiguration("file path") to open correct path
I'd like to create an application using the embedded version of Jetty. Unfortunately, I can't find any information on what jar files I would need to do that. There are several in the maven repository (http://repo2.maven.org/maven2/org/eclipse/jetty/aggregate/). But what's the difference between jetty-server, jetty-server-all, and jetty-webapp? Are any of these what I want for the embedded use case?
I stopped getting compile errors against the Eclipse embedded code minimal example combining SimplestServer and HelloWorldHandler...
http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
To achieve this I had to include the following from the lib directory in the unzipped distribution from eclipse's jetty mirror...
jetty-server-7.1.4xxxx.jar
jetty-util-7.1.4xxxx.jar
servlet-api.2.5.jar
This document lists the JAR files required for embedding and is pretty complete.
http://docs.codehaus.org/display/JETTY/Embedding+Jetty
I believe you won't need the Ant jar file unless you're invoking Jetty from Ant, even though it says you need it.
Some of the JSP jar files are named differently in the binary bundle than that document calls for, but this document helps figure out which Jetty JSP jars to use:
http://docs.codehaus.org/display/JETTY/JSP+2.0+v+JSP+2.1
I used jetty-webapp.
All the dependencies are very best explained in this diagram : http://wiki.eclipse.org/Jetty/Reference/Dependencies
Based on the diagram,for embedded use case, a minimum of 6 jars are required.
E.g for Jetty 8, try:
jetty-continuation-8..jar
jetty-http-8..jar
jetty-io-8..jar
jetty-server-8..jar
jetty-util-8.*.jar
servlet-api-3.0.jar
For completeness, the xml for jetty-webapp is;
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.1.2.v20120308</version>
</dependency>