Generating JUnit stubs for new methods in existing class in Eclipse - eclipse

This question is tangentially related to How can I create JUnit stub methods in eclipse? but it doesn't answer my specific question.
Assuming you have an existing JUnit test case class, if you add a method to the target class (or choose to test a previously untested method), is there a way to tell Eclipse to generate the stub for the "new" method(s) in the existing TestCase without creating a new test case class?

Install the MoreUnit extension from the MarketPlace.
Open the package hierarchy panel.
Navigate down to the class that
you've modified.
Right click on the new method.
Select "Generate Test". The generated stub will appear in your ClassTest file.

My solution.
I simply go through the standard 'create JUnit test case'
Select file to test. -> New Junit test case
Go through the normal process in creating the test case, but only select those that you want new stubs for.
The file is created with the stubs, which I now copy into the existing test case file.
Delete the newly created test file class.
It's not the most efficient, but quicker than 'thick fingering' when you create them manually.
David

The usual working cycle with unit tests is to create the test case first
public void test_new_method() {
ClassUnderTest x = new ClassUnderTest();
x.NewMethod();
}
At that point Eclipse will give you a warning that the method "NewMethod" does not exist. Than you simply select a "Quick Fix" (Ctrl-1 or Ctrl-2 i'm not sure) to create the missing method in the class.

Related

Eclipse can not find JUnit test case folder - how to configure it?

I want to test methods of Java classes using JUnit. Therefore, I use Eclipse and create a source folder "TestCases" which contains a package with the name "test" that should be the place where I want to put the JUnit test cases.
The whole directory set up is shown in the following pic:
My problem is, when I right-click on "test" and go to "JUnit Test Case", and write for example "ASTAnyExprTest"(see (1) in the following pic), and go to "Browse" (see (2)) and type "ASTAnyExpr" (see (3) ) to set the name of the class under test, then I see only a blank space (no class to choose).
So, I must have a path-problem, I thought. My first idea was to copy all classes to the package "test". But then I need to modify and adjust all dependencies in the classes.
So is there a better solution for that ?
best regards,

JUnit: How to bundle #Test methods to a suite?

I have several classes each containing several methods annotated with #Test. The classes are not extending TestCase. Now I want to
write a main that executes all these methods (for command line use)
create a class that can be "Run as -> JUnit Test" in Eclipse executing all these methods
Since the classes are no TestCases I can't just add them to a suite. Also extending TestCase is not an option because the test methods are just annotated and their names don't start with 'test'.
How can I do this?
Try this:
#RunWith(Suite.class)
#Suite.SuiteClasses({
Test1.class,
Test2.class,
Test3.class,
Test4.class
})
public class YourTestSuite {
}
You can set up run configurations for a project. Right click on your project. Then selecte 'Run as' -> 'Run configurations'. Within that you can select run all tests
What Eugene posted works if you have a small number of classes. If you want all the tests in a certain package pattern, Classpath Suite lets you specify this with patterns instead of listing them all out.

Executing Scala objects in Eclipse without a main method

I have an assignment to code several methods in Scala. The methods will be encapsulated in an object that has no main method. The professor gave us a JAR file that contains an interface (my object implements this interface) as well as a sort of pseudo test object that performs various assert statements against each of my functions. This object also does not contain a main method.
Now in Intellij I simply had to declare the dependency on the JAR in the classpath, and it runs fine. Eclipse is giving me trouble though because when I go to define a Scala application run configuration it specifically asks me to name the class that contains a main method, and there is no main method.
I am assuming that I might be choosing the wrong project type for this type of set up, but I am inexperienced with this and I would appreciate any advice you might have for running something like this in eclipse.
Thanks.
I would either:
just write an object with a main method which calls the test object, or
start a Scala interpreter in your project (from context menu, under Scala).
Preferring the first approach, because it's faster to repeat tests after a modification.

How to have several templates for Java files in Eclipse?

I'd like to have several different templates for new java files in Eclipse. Is this possible?
Basically when I am creating a new Java class file in Package Explorer I'd like to use a template X as a base for new file, when I am creating a new unit test class I'd like to use template Y, when I am creating a Wicket component class I'd like to use template Z, etc.
Also is it possible to configure Eclipse so that when I create a new Java class, Eclipse will generate corresponding new unit test class automatically?
I am not sure if this is what you want, but you can add new wizards to Eclipse using the org.eclipse.ui.newWizards extension point.
Here is the corresponding documentation in Eclipse help.
Another (less elegant) way of doing this is to add your own templates (e.g. DAO) under the Java editor (Window->Preferences->Java->Editor->Templates) so when you type DAO and Ctrl + Enter it, it expands to the full template you want. This way creating a new empty file, typing DAO and autocompleting would transform into a full file snippet.
Hope it helps!

Netbeans - class does not have a main method

My program is just a simple System.out.println("");
But netbeans cannot find the main method.
Is netbeans 6.7.1 conflict with WIN7?
Any possible error?
This happens when you move your main class location manually because Netbeans doesn't refresh one of its property files.
Open nbproject/project.properties and change the value of main.class to the correct package location.
Sometimes passing parameters in the main method causes this problem eg. public static void main(String[] args,int a). If you declare the variable outside the main method, it might help :)
Exceute the program by pressing SHIFT+F6, instead of clicking the RUN button on the window.
This might be silly, bt the error main class not found is not occurring, the project is executing well...
It was most likely that you capitalized 'm' in 'main' to 'Main'
This happened to me this instant but I fixed it thanks to the various source code examples given by all those that responded. Thank you.
This destroyed me for a while.... I knew that there HAD to be an easier way with a world class IDE like Netbeans.
The easiest method is to press Shift+F11 (Clean and Build Project), then hit F6 to run it.
It refreshes Netbeans appropriately and finds your main without all the manual labor; and if you have multiple mains, it will give you the option to select the correct one.
Make sure it is
public static void main(String[] argv)
No other signature will do.
in Project window right click on your project and select properties go to Run and set Main Class ( you can brows it) . this manual work if you have static main in some class :
public class Someclass
{
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
//your code
}
}
Netbeans doesn't have any conflict with W7 and you can use version 6.8 .
If you named your class with the keyword in Java, your program wouldn't be recognized that it had the main method.
I had this issue as well (Error: Could not find or load main class test.Test). I'll explain this relatively basically since I know I would have appreciated someone doing that when I was looking for my answer.
When I right-clicked on the project (left hand side of the screen unless you got rid of the projects tab) and went to properties and then run, the main class had the projectname.classname, which is what confused me.
For example, I created a project "test" to test this out, and when I went to
(right-click) test or Source Packages -> properties -> run -> main class
had Test.test in that field, which is what the problem was. the class is test, not Test.test, so I clicked browse to its right, and the only thing in the list to select from was test, and when I selected that and tried rerunning it, it finally worked.
Additionally, I found that when you create a new project in Netbeans, one of the things it originally gives you (in my case of the project named test) is package test;. If you are having this problem, then like me, you probably originally got rid of that line seeing it as just another line of code you didn't need. That line of code is what enabled your main class which in my case was Test.test to find the main class *test from that.
My situation was different I believe because non of the above solutions di work for me. Let me share my situation.
I am importing an existing project (NewProject->Java->Import Existing Projects)
I name the project to xyz. The 'main' function exists in Main.class.
I try to run the code I modified in the main function but the error pops out. I tried the shift_f6, specifically rebuild. Nothing works.
Solution: I took the project properties and saw the 'Source Package Folder' mappings in the Sources branch was blank. I mapped it and voila it worked.
Now anyone might think that was very silly of me. Although I am new to Java and Netbeans this is not the first time I am importing sample projects and I saw all of them had the properties similar. The only difference I saw was that the main class was not having the name as the project which I believe is a java convention. I am using JDK7u51 (latest till date), is it causing the issue? I have no idea. But I am happy the project is running fine now.
Check for correct method declaration
public static void main(String [ ] args)
Check netbeans project properties in Run > main Class
While this may be an old question, the problem is still occurring these days, and the exact question is still not answered properly.
It is important to note that some projects have multiple classes with a main method.
In my case, I could run the project via the main class, but I could not run a particular other class that had a main method.
The only thing that helped me was refactoring the class and renaming it.
I've tried:
restart NetBeans
re-open the project
clear NetBeans cache
delete the file and create a new one with same name and contents
delete the file and create a new one with same name but very simple contents with only main method and print out message
rename the class (refactor) so a temp name and back
delete the project and create a new one with the same sources
The only thing that let me run this class is renaming it permanently. I think this must be some kind of a NetBeans bug.
Edit: Another thing that did help was completely uninstall Netbeans, wipe cache and any configuration files. It so happened that a newer Netbeans version was available so I installed it. But the old one would have probably worked too.
public class TestConnection {
public static void main(String args[]) {
//make sure when you create a new class, it has the above line, and then
//when you click shift+F6, it works fine for NetBeans IDE 12.4
myConnection my = new myConnection();
my.getConnection();
}
}