Load test resources within other module? - java

I'm using a abstract class in another module for reading and input for my testdata with:
package src/main/java/path/to/my/base/testclass;
InputStream stream = getClass().getResourceAsStream(filename);
filename is eg "test.txt", located in src/main/resources/path/to/my/base/testclass
As long as I put this abstract class into the same module as my testclasses are in, everything works fine.
Then i extract the acstract class (as well as the resources) to other module, compile, add to pom etc.
Result: My test implementation runs fine, but: I'm getting IO exception as the file could not be found.
What am I missing here? Why does the abstract class work within the same module, but not within another?

Test resources are for this artifact's tests only, they don't get deployed.
There are two possible ways around this:
Dirty: Make your app deploy a test jar along with the main jar,
and add that as a dependency with scope TEST to the second artifact.
Clean: Create a separate test artifact for base test classes and
common test resources. Important: in this artifact, nothing goes in src/test and everything goes in src/main. Reference this test artifact from both other
artifacts with scope TEST.

Related

Micronaut Test Resources: How to extend AbstractTestContainersProvider class

Micronaut documentation says:
For test resources which make use of Testcontainers, you may extend
the base AbstractTestContainersProvider class.
My question is: how to add this class properly to the classpath of the test resources sourceset (I am using Gradle)
You will need to add the following dependencies to your build.gradle file:
dependencies {
testResourcesImplementation platform("io.micronaut:micronaut-bom:3.6.1")
testResourcesImplementation "io.micronaut.testresources:micronaut-test-resources-testcontainers"
}
(note that I'm importing the Micronaut BOM so that you don't have to specify the test resources version, but you could use it directly)

Nested modules with Spring

I am developing project with Spring Framework.
I have created about 5 modules, sometimes one depend on other, but they are all on top level, and up to this point everything works fine.
Example:
Database module has only external dependencies
Identity module depends on database module
Facebook stuff module depends on identity module
Now, I have created directory in root of project called modules, and moved all modules into it (so they all were, and still are on same relative distance to each other).
All tests passes and I can build/compile and inspect classes without any problem.
However, now when I try to run only identity module (that does not require facebook stuff) spring throws me an exception, that it cannot find facebook beans. Of course it cannot, because there is no dependency, but I do not want to add this dependency. #Configuration is #Lazy so there is no point creating such #Bean anyway.
Code:
new AnnotationConfigApplicationContext(Application.class);
Application class is #Lazy #Configuration and does #ComponentScan from whole application, and as I understand it finds also #Configuration's from other modules and then - I do not know why - tried to create those #Bean's from other modules but fails as expected.
I have verified with git, that the only between working and not working states are moving those modules into new folder.
So to clarify, working/default structure is:
/.gradle
/.idea
/DatabaseModule
/IdentityModule
/FacebookModule
/.out
/.gitignore
and not working one is:
/.gradle
/.idea
/modules/DatabaseModule
/modules/IdentityModule
/modules/FacebookModule
/.out
/.gitignore
Code stays the same.
I think, that if I will add all dependencies to all modules then it will work but for obvious reasons I do not want to do this.
Am I doing something wrong?
Is there any convention, that I am breaking?
Bonus question: how are nested modules different, from ordinary folder containing modules?
EDIT:
I should also note, that all tests pass in both scenarios, however I am not using spring in tests (no dependency injection) - just new or Mock() everything

JUnit test failed to read property file from another module class

I am creating a project which have multiple module.I am using the gradle build tool and IntelliJ IDEA.I have two module webservice and utilities.
Project Structure as-
I am reading the config.properties file in my utilities module.In which I am defining the server port and other values. When I am calling the method of utilities module (which reads the property file and return the values) from my webservice module classes that work fine and return proper values.
But when trying to call same method from test classes of webservice module then utility class method failed to read property file.
Now I am not getting what is going wrong.
Thanks.
Be sure that the property file exists in /src/test/resources as well as /src/main/resources as the classpath used when executing tests differs to your regular application classpath.

Spring Maven unitTest applicationContext loading wrong file

.I have a project that has a spring-config.xml file in src/main/webapp/WEB-INF and an applicationContext.xml file in src/test/resources. I also have an abstract test base class for my unit tests in src/test/java looks something like:
#ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
public abstract class AbstractTestBase extends AbstractTransactionalJUnit4SpringContextTests {
//Common code and fields
}
All my unit tests extends this AbstractTestBase which points to the context within the src/test/resources or should. The problem arises when running my unit tests it is pulling in the spring-config.xml file.
There are other projects my team is working on that have the same file structure, same app context setup, and run as intended, but even when I have each file in the project side by side I don't see where their file runs and this one doesn't.
I am new to spring so I don't know what it is I should be looking for.
Are there any situations where Spring or Maven would not take the app context I'm handing it given all files exist? Is there anything I might be missing?
EDIT: corrected to reflect that one file is a spring-config file.
"classpath:/applicationContext.xml" should look under src/test/resources.
But it should be noticed that using that syntax will load the first one it finds and then stop as mentioned by '#chrylis'.
I once had similar problem.
You must have been using an IDE. There must have been applicationContext.xml file in your target/test-classes/ (in Eclipse IDE) in your project directory that is a copy of your xml file under src/main/webapp/WEB-INF or xml file like it.

Any ability to mark a REST endpoint to be ignored during build process?

I have a Jackson services project in Eclipse that uses Maven for the build process, and I have several "test only" endpoints that are necessary for the unit tests, but should be removed for the public build. Is there an annotation, or other configuration property I can set so that in the build process (after the unit tests pass), the endpoint will not be deployed?
For example, I have a method like:
#GET
#Path("/{user}/addresses")
#Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> getUserAddresses(...){ ... }
And when this is published, user addresses will come from a 3rd party system, but for testing, we need a way to add an address, so i have
#POST
#Path("/{user}/address")
#Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> createAddress(
but I want the 2nd method to never exist on the live server.
I would just put the "test only" classes in src/test/java. There is no rule that the only things that can appear in src/test/java are unit tests only.
This way you have nothing extra to configure with Maven to exclude classes.
Alternatively, you could tell the maven-jar-plugin or maven-war-plugin to exclude specific classes from being packaged in the final artifact, but this has the downside of not being able to catch a case where you accidentally import one of the "test only" classes in a real class and thus you ship something in your jar/war that will cause a NoClassDefFoundError. If you place the "test-only" classes in src/test/java instead, and therefore ensure they are only on the test classpath, this accidental import problem will not be possible (compilation of the main code will fail).

Categories