Running a class WITHOUT jUnit as a running option - java

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.

Related

can't import class src/test/java into src/main/java in eclipse

I am using gradle in eclipse.
I want to use class in src/test/java/testing inside src/main/java/program.
I tried doing :
// in program.java
public static void main(String[] args) {
final testing t = new testing();
// in testing.java
public class testing {
... some code ...
}
but the execution failed because it couldn't find symbol "class testing".
I do not see any problem in my program, so I am assuming that I did not properly set the environment up.
I've seen a post saying I have to build path but it says there's no action available.
Can someone help me set the environment up on mac?
I want to use class in src/test/java/testing inside src/main/java/program.
No, that's not how it's supposed to go. Gradle, eclipse, or for that matter any build system and any IDE will fight you.
Test code depends on main code, but main code does not depend on test code. It wouldn't work - test code is not shipped when you deploy your application.
If you want to write some tests that you run from main, put that code in src/main itself. src/test is for automated testing purposes.
If you are making a class for some data tracking that the actual test code will use, and currently this tracking class lives in src/test because you feel that is where it should go, given that it is test related - move that class to main. Anything that src/main/... uses, is by definition 'main' code.

Running a code with JUnit Test Cases

I have checked out a code from CVS and need to make changes to it. The code has 2 folders
Java
Test
The later has JUnit test cases. I'm not very familiar with JUnit but as far as my understanding is, the classes are duplicated in JUnit as class names. That's why I get the error in the test folder.
Class "xxxxx" already exists
I'm not sure how do I run this project without removing the folder test. Is there a way I can make eclipse ignore the JUnit test cases for now?
Go into the properties of the Eclipse project, open Java Build Path / Source and remove folder Test. Eclipse will then ignore the sources in that folder.
Test and normal java classes are merged together during build time, your error happens because the test classes have the exact same name as the normal classes. You should rename your test cases with some kind of prefix like Test to prevent them conflicting.
Doing things to work around the problem will only conflict later when you are changing the build platform, maybe your current build platform accepts it, but your future platform/editor may not, and then you have the real problems.

JUnit test, Unrooted Tests: initializationError

So I edited the name of a JUnit test and now it wont work. Instead I get Unrooted Tests: initializationError.
This is a simple test. Infact it is a test for JUnit tests as I am just starting to use it.
#Test
public void testRun()
String s = null;
assertNull(s);
}
and all i did was change it to testRun2(). Also when I run the file not the individual test, it still runs the old testRun(), not testRun2().
My project has Maven not sure if that is a factor. And I have updated the project
So it turned out that I needed to rebuild using Maven to update the classes. Now it works fine and I can add/modify test cases.
In my case, i changed the method name and it didn't update it automatically, so the above solution of Project> Clean worked well for me.
Another way this error would occur is forgetting the Test annotation. Encountered when right click method name in Eclipse and Run As -> Junit Test.

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.

Can't run JUnit in Eclipse Indigo

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....
}

Categories