Build failing in intellij - java

Current scenario : Two file in same package but in different module one in main and other in Test.
Reason : Because when I run my unit test it automatically access file in test module,where as when I run the main module it access the file in main module.
Problem : Now when I am running build task it is failing. As now my unit test are failing as they are not able to pick the right file of the two.Due to which my build is failing.
Is there anyway I can overcome this issue.

Related

IntelliJ: Setting different working directories per class in multiple class tests

I want to run a suite of tests in IntelliJ within a single run configuration,
and I want each class of Junit tests to have it's own work directory.
What I tried to do was to set the Working directory to $MODULE_WORKING_DIR$\$FileClass$\
However, I'm getting the error "Cannot start process, the working directory 'c:/...' does not exist".
I would even settle on having the same workdir per class and long as there's a way to make sure it's cleaned after each class.
Any suggestions?

Scan and render junit tests from external files

There are 2 files outside of the application (A.class and ATest.class).
How can we scan ATest to find all junit tests and run them (assuming the dependencies of ATest are loaded in classpath except A)? I'm looking for doing this programmatically in Java and as a result having the list of the tests of ATest, which failed and which succeeded.
The command line must not be used (in case of we need a more complex output).
Thanks for your help.

Java/Eclipse: How to configure Run Configuration's Classpath for JUnit test?

I have an Eclipse project with the following directory structure:
MyProj/
src/main/java/
com.me.myproject.widgets
Widget.java
src/main/config
widget-config.xml
src/test/java
com.me.myproject.widgets
WidgetTest.java
src/test/config
widget-test-config.xml
The Widget class reads its config (XML) file in from anywhere on the classpath and uses it to configure its properties.
I am trying to just get WidgetTest's test cases (all written with JUnit) to run inside Eclipse when I right-click the file and go to Run As >> JUnit Test. I assume I'll have to actually run it as a customized Run Configuration with its own configured classpath, but I'm not sure about that as I've never done this before.
Does anybody know how I can get a custom Run Configuration to run WidgetTest.java as a JUnit test, and successfully place src/test/config/widget-test-config.xml on the classpath? Thanks in advance!
Please note, this question is not about how to read a resource from the runtime classpath, its about how to get it on Eclipse's JUnit Run Config classpath in the first place!
I was under the impression that as long as you have src/test/config/widget-test-config.xml inside what Eclipse considers to be a source folder, it should already be on the classpath.
Is src/test a source folder for Eclipse ? If it is and you still get the problem, try out the following experiment :
If you copy widget-test-config.xml to the src root can Widget class read it ?
If Yes
then it's a problem of the test folder not being on the classpath and you may wanna try adding it manually like so.
Right click WidgetTest and select Run As -> Junit Test. This should automatically create a Junit Run Configuration accessible at Run -> Run Configurations. You modify it's Classpath entry to add the project containing the .xml file like so :
If No
If, even after moving the .xml file to the src root (i.e. default package), your widget class cannot read it then there is something else wrong. In that case, it would be great if you could furnish the snippet of code in WidgetTest which is trying to read the .xml file.
Working Code
Here is a bit of working code :
public class A {
#Test
public void test() {
InputStream stream = A.class.getResourceAsStream("/SomeTextFile.txt");
System.out.println(stream != null);
stream = Test.class.getClassLoader()
.getResourceAsStream("SomeTextFile.txt");
System.out.println(stream != null);
}
}
The above works for me in a simple JAVA project and runs fine. (Running fine means getting
'true' printed on the console)
I am in the process of creating a GITHub repo for you to try out this code painlessly.
GIT Hub Repo with Test project
You should be able to import the project in this zip and see the code working. Right click on the Test class A and click Run As -> Junit Test, and you should see two true in the Console.
If your WidgetTest class is written as a JUnit test, eclipse will try to run it as a Junit test automatically. If it doesn't, you should right click on the class in the package explorer, choose Run As >> Run Configuration >> choose Junit
To run a Junit test:
in JUnit3, the class should implement TestCase and all the method names should start "test"
in JUnit4, all the methods should be preceded by a #Test annotation
To place that config file in the classpath: when setting the Run Configuration as above, go to the Arguments tab in the upper right pane and in the VM arguments specify the classpath:
-cp .:/path/to/the/config/file
However, if that file is in a package in the source directory, it should automatically be included in the classpath.

Troubles with Jenkins: unable to run jUnit tests.

All tests, that creates or removes files and dirs fails. I try to use env var to point root directory from surefire plugin(${WORKSPACE}), manually at pom.xml (e.g. ${env.WORKSPACE}). I even set those workspace dir directly in test code (String WORKSPACE="/ud/rpt_trunk_b"). But tests fails anyway. Does anyone uses jUnit with Jenkins?
Thanks.

JPA Hibernate Maven testing --> unknown class

I'm trying to run a simple dataImport class which is using JPA and Hibernate.
If I run my class, i always have the following error:
Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: ch.itartis.relman.entities.code.ReferenceCode
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:675)
at ch.itartis.relman.service.test.dataimport.DataImport.doSave(DataImport.java:111)
at ch.itartis.relman.service.test.dataimport.DataImport.main(DataImport.java:43)
My Class is located in the src/test/java/ folder, I have a service-config.xml in src/test/resources/ and I also have a persistence.xml in src/test/resources/META-INF/.
If I run the class in in the src/main/java/... folder, it works. But if I want to have the class in src/test/java/, it doesn't.
What am I doing wrong?
Thanks a lot!
You are running the code from your own main method, which I am guessing means it isn't being run by maven. The code in test is not included as part of the artifact generated by maven, it is only included during mavens test phase for running unit tests.
If you are using maven, why not simply create JUnit tests that maven will run as part of the build process instead of rolling your own.
To complete Robin's answer: if you are running the class using exec:java set classpathScope to test and everything will be fine.

Categories