Can't run JUnit in Eclipse Indigo - java

I create simple Java Application project with src and test folders. src folder consists of the class which I test and test folder consists of the class where I describe test. When I start testing, using JUnit 4 I get: Could not find the main class: org.eclipse.jdt.internal.junit.runner.RemoteTestRunner. Program will exit. error. All JAR files are presented in my project.
What did I miss in my project?

That is a class that is part of the Eclipse plugin, it sounds like you have a corrupted installation of Eclipse. Try with a fresh installation.

Far shot: is the test-folder containing the .java-files for your tests also declared as a source-folder in the project's Build Path settings?

All JUnit tests should be written as follows:
#Test //This is very very important
public void testAbc()
{
//Test Abc() here....
}

Related

Running a class WITHOUT jUnit as a running option

So I just ran a jUnit test with Eclipse. I have a test source folder and I have a general source folder for the normal classes that are not tests. Everytime I run my project, the tests (and Test.java) is the only thing that runs. The only time I am able to access the normal classes in my general source folder, is if I throw an #Test annotation in front of the main.
Is there someway to go back to running from the normal source, without deleting the test source? Do I have to change the Run As configuration?
Thanks!
As cricket_007 mentioned you need a public static void main(String[] args){...} method in one class to run it.

Unable to run JUnit test in IntelliJ

I wanted to start project using TDD. Created the test directory and later changed it to package that is integrated with src direcotry. In both cases I get the same error:
Class not found: "tests.objectsTest"
I tried different techniques of importing JUnit jar and none solved problem. Also I tried to rename my test class but it gives no solutons whatsoever.
It seems that IntelliJ or JUnit changes name of the test class. Shouldn't it be objectsTest.tests?
I am using JUnit version 4.12 and latest IntelliJ EAP.
This is my project structure:
Project:
-.idea
-src
-logic
-objects
-tests
-test
-test.java
src and tests are directories marked as Source and Test. Every package except test is empty. On my other PC with IntelliJ Community Edition everything works fine but on EAP there is this bug. Unfortunatelly I have to use EAP.
test.java code:
package test;
import org.junit.Test;
public class test {
#Test
public void canCreateInhabitant(){
}
}
Have you checked if you have the JUnit plugin enabled? I (foolishly, shame!) disabled it at some point and was unable to get IDEA to run my tests until I remembered to turn the plugin back on...
Check the root directory of your classes. It must be marked as source (for java classes) or test (for java test classes).
It seems that your directory is not well marked in IntelliJ.
I did a simple test and put it on github.
It's the absolute simplest of tests but it works great, standing inside the test class pressing shift+ctrl+t will run the test.
Go ahead and clone it and try it out.
Easiest way is:
Open Class in Intellij and press Ctrl+Shift+T
Select "Create New Test"
Now, a new pop up will be opened where you can select Unit Test Library (For your case its Junit4)
Select the methods which you want to include in test
And there you go !
Sometimes I find that this happens when I try and launch "All tests" from the project folder in the structure view. Launching all tests by right clicking on the test root folder and selecting "all tests" from there seems to solve it.
Just had this happen to me. When I built via Maven it had a problem. When I fixed the problem, it would run the junits again. Goofy.
I had the same problem, I solved it by clicking File-> Invalidate chaces and Invalidate an Restart

Running testing class from jar package in NetBeans

I have like 30 Java classes and 1 class for testing in a jar package.
To run the testing class, I need to create new NetBeans project , import all those 30 classes into /src , then import 1 testing class into /test and maybe add some libraries to the project also...
So that, after all I will be able to run the testing class...
Is there some other way to do it?
I open the jar package in NetBeans and see all the classes but it doesn't let me run the testing class since there is no main method in there..
A bunch of ways. If you're in NetBeans, and the class has a public static void main() method you can just right-click it and choose Run File.
But you're far better off writing your test as a JUnit or TestNG test - that way it is not included in your production bits, and continuous build tools and other things will be able to run your tests automatically because they look the way those tools expect.

How to run java file exported from selenium IDE through command prompt in windows

Can some one tell me how can i run the java file which was exported from selenium IDE through command prompt.
I have used the following command: "java -jar selenium-server.jar -htmlSuite "*firefox" "http://www.google.com" "C:\mytestsuite\mytestsuite.html" "C:\mytestsuite\results.html"
Able to launch selenium functional test runner but nothing is executed there.
The converted tests are JUnit tests. So you should have two processes:
Your selenium server process:
java -jar lib/selenium-server-standalone-2.28.0.jar
Your JUnit test runner
"java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name] "
If you have several test classes, it might be better to create a TestSuite with the Suite annotation:
#RunWith(Suite.class)
#SuiteClasses({
MyTestClass1.class,
MyTestClass2.class})
public class TestSuite {
...
If you are using Spring, you can setup config containing selenium server address, browser, ...
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:my/package/seleniumConfig.xml"})
public abstract class SeleniumTestSuite {
The java test cases exported don't compile because they require at least the Selenium library to compile and may need junit or TestNG as well to actually run. I really suggest that you do this from within eclipse.
You can get eclipse 32 or 64 bit here. Then create a new Java project by going File-> New -> Java Project. You can get the Selenium client zip here. You need to get the client jar (called selenium-java-2.31.0.jar) out of this zip and put it in the lib directory of your new Java project in eclipse. You may have to create the lib directory and then right-click the jar file in the lib directory and "add to build path".
Put the java code that was generated by the Selenium IDE into the src directory of the new Java project in eclipse. (you may need to create the appropriate packages under src etc). Then right-click the test case java file that you want and select "Run As ... Junit". This should run it for you. If you get compile or run errors you can update your question above.
This is the solution:
Test is your Class mande by selenium
import org.junit.runner.JUnitCore;
import com.example.tests;
public static void main(String[] args) {
Result result = JUnitCore.runClasses(Test.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}

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.

Categories