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

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,

Related

Rename references at the same time in Eclipse

I have the following situation:
Consider that I have a class nested in a folder structure like for example /fol/der/exam/ple.java . Of course, the class ple.java contains package or import instuctions referencing to some sources. Now, when I choose Use as a source folder for /fol/der/exam/ple.java then, of course, the references used in package, import or function parameters are not the same anymore. I need to adjust them. Is there an elegant how to do that automatically in eclipse?
So, let´s assume that I have the following line in ple.java:
package fol.der.exam;
and that I have this line also in other files like a.java, b.java, etc. and I want to change the line, let´s say to package der.exam;
How can I do that automatically for all java files in eclipse?
best regards,

Newbie IDE (IntelliJ IDEA) issue: .class files not all usable

I'm working on a school project right now, and every time I have in the past, I always make a new Project in IntelliJ IDEA. However, this time she gave us some .class files that have methods in them that we can't see (she described what they do so we know how to use them) but need to use, so they have to be in the same folder, obviously.
SIDENOTE: I'm also new to using Macs, which is what I'm on at the moment.
Anyways, I put the .class files in my src folder that I found in the Project8 folder. I just made an array of the Book objects, which was one of the .class files I moved, and now I need to use a method from the other .class file, named BookInventory.class. I put that file in the src folder just like the other, but it won't let me use the only method in that class, which is LoadBooks.
Here is the LoadBooks signature:
public static void LoadBooks(Book[] b)
And here's the description of it that she gave to us:
"For each element in the array, accepts user input for the book, creates the Book object, and stores the object into the array."
So, when I made the array of Book objects, IDEA made an import statement up top all by itself, I didn't type it:
import java.awt.print.Book;
So why does IDEA recognize the Book.class file and allow me to use it in this .java file for my project, but it doesn't seem to notice the BookInventory.class file?
Any help appreciated, thanks ahead of time.
What is happening is when you first typed the line with LoadBooks(Book[] b), IntelliJ could not "see" your class files (you have subsequently loaded them in "class files" and added that as a project library, I presume).
IntelliJ however searched for and found a Book class in the internal java libraries, java.awt.print.Book. Note that this is a different class to the one your teacher gave you, which might have been e.g. edu.myschool.homework.Book.
Firstly, try to delete the line including the import statement, or manually change it to the correct package (your teacher can inform you what it is).
If the same import comes back automatically, you can go into Settings -> Editor -> General -> Auto Import and untick Add unambiguous imports on the fly - this will cause intellij to prompt you before adding imports.
Also, I would ask your teacher to give you the class files in a jar file, since that's the usual approach.
Good luck.

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.

Keep Test classes in sync with Source classes

In Eclipse, I have two source folders, one called src the other called test. They have the exact same package structure and each src Class has a test equivalent with "Test" appended to its name. Sometimes, I move my src classes during refactoring. Is there a setting where Eclipse automatically moves the test classes as well? Also, the same with renaming source classes.
No, because eclipse doesn't know the convention you are writing about.

Generating JUnit stubs for new methods in existing class in 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.