Eclipse function/plugin that finds corresponding junit class? - java

I've been searching high and low for an Eclipse feature that lets you right-click on a main source class and find the corresponding JUnit class(es), without me having to navigate through the test classes of my project. I am using Mylyn which helps reduce the clutter but it would be much easier if there was a feature that performs a find automagically.
I am following the Maven standard directory layout (/src/main/java, /src/test/java, etc.). And all of my test classes are named *Test. I'd imagine this can be feasibly implemented and probably already exists.
Is there a function or plugin in Eclipse that finds the corresponding JUnit test classes for a given main class?

The moreUnit plugin probably works for you.
Capabilities (from its site):
Decorate classes which have a testcase.
Mark methods in the editor which are under test.
Jump to a testcase/testmethod in the editor via the menu or a shortcut.
Rename classes/methods and moreUnit will rename the corresponding testcode too.
Move classes and moreUnit will move the corresponding tests.
Generate a testmethod stub for the method under cursor-position in the editor via the menu or a shortcut.

This plugin claims to be able to do this, as well as other stuff.
An useful feature of this plugin is
the ability to jump between similar
class, e.g FooDAO to FooService,
FooService to FooAction, etc. To use
this feature, one needs to configure
this first. To configure, please go to
Windows -> Preferences ->Fast Code
Preferences -> Mapping Btween Similar
Classes. This is very similar to the
configuration for create similar
classes.

As a partial answer to your question, there is no requirement that tests have a one to one correspondence with main classes, or any standard naming convention (even with maven). What you would want is a plugin that (for example based on a regex) matches source classNames to dest ClassNames, and then loads that. Such a plugin would allow you to do what you want (and also for other uses not related to junit), but I'm not aware of one.

Infinitest plugin runs your JUnits for those classes that you're changing, as you're changing them. There is no need to right-click on the updated class to find the relevant JUnit class, and then to run it - it will get run automatically. Test errors (if any) will show up the same way as Eclipse shows syntax errors.

Related

Eclipse TDD - How to join test and main source folders?

When we are using Test driven devleopment, I always wonder how you easily reach or create the test class in Eclipse.
We have a maven setup with two source folders src/main/java and src/test/java
When I am opening a class in src/main/java looking for a bug, I want my test class side-by-side. But I have to search the tree for it. Of course you can use a search box like "Open Type", but it is still annoying and takes a lot of time. And when i go to the test my package explorer to the left shows the test package, so I can't see the other classes in the package and cannot easily open them.
Do I miss something to support my TDD workflow?
What is the best setup to have the test near by?
The title of your question speaks about a bad practice: keeping production and test code together. This is a bad practice because there's the risk of deploying also tests in the final package(s).
But reading your request (if you are using JUnit) it looks like you want an Eclipse plugin such as MoreUnit which creates a shortcut and makes easier working with JUnit files by decorating source classes etc.
This plugin has also been cited in other SO responses such as:
Eclipse function/plugin that finds corresponding junit class?
Furthermore sometimes a production class is used in more than one test class you may also search for all occurrences of that source class and limit your result scope to test packages. If you don't use JUnit but instead you use another framework such as TestNG you will have to search for a different plugin because MoreUnit is JUnit specific.
Hope it helps.

Determining the test classes in a project

I wonder is there any id or specified label to distinguish a selected class whether it is one of the standard class declarations or test class in run-time?
EDIT: I collect the entire classes from the project. I separate abstract classes, interfaces, subclasses by looking the collection, whereas I also want to know how many classes have test behavior. In other words, how many classes are actually test classes. One more thing: I don't know these classes in advance, these are not mine!
Let me share with you how I like to organize my tests in eclipse :-) maybe you may find useful.
First, I create two projects, one for the app and another for the test.
The test project, of course, has a dependency on the app project
Now, let's suppose you want to add some test case, you just point to the right src dir.
So you want to create your test code without mixing app code and test code (for example, utils), just leave what's specific to the right project.
The only name convention I use is the eclipse junit default, appending the word "Test" in the end of the test class.
No need for ant scripts to deploy only the app code.
Even JUNIT dependency is restricted to the test project.
I hope it helps.

How to create JUnit test cases for Guava's ArrayListMultiMap class in Eclipse?

I have to create some JUnit test cases for Guava's ArrayListMultimap class, but so far, I'm clueless as to how I should proceed. I have successfully been able to create a normal testing class, ArrayListMultimapTest (which I've used to test the methods of the class), in which I added Guava's 15 release JAR file as an external library in the build path. But I've been specifically asked to create JUnit test cases for ArrayListMultimap, and I'm not sure as to how I should proceed. Any help would be appreciated, thanks!
Although the library is extensively tested by Google, you can do this easily yourself, if you want.
right-click your project in Eclipse
select Properties
select Java Build Path
click Add Library...
Select JUnit, pick JUnit4, confirm
Now you're basically done and you can write your testcases - simply annotate any public void someName() method with the #Test annotation. If you won't have a public static main(String... args) method in your code, then when you'll try to run the class, Eclipse will automatically run it with JUnit.
#Test
public void multimapAcceptsMultipleEqualValuesForOneKey() {
ListMultimap<String, String> listMultimap = ArrayListMultimap.create();
listMultimap.put("aha", "eh");
listMultimap.put("aha", "eh");
Assert.assertEquals(2, listMultimap.get("aha").size());
}
After that, try to dig into JUnit to learn all the goodness it offers you.
There's already such a test. No idea about maven, but just do
git clone https://code.google.com/p/guava-libraries
Then do everything you need to get in Eclipse running (which needs tons of JARs, but you get them all using maven, add them to the build path), open the file and click "run as junit test". Done.
__
Unless you suspect a specific problem, there's no need to write your own tests for Guava as it gets tested and used heavily. Anyway, even if feel like writing it, you should look at the existing ones first (they're sometimes a bit hard to understand as there's a whole testing framework, which is necessary to test that many classes thoroughly).
git is a version control system free to download here. It uses command line, but the only one you need to know is written above. maven is an incomprehensible "software project management and comprehension tool" also free to download and use.
As a programmer, you'll need them (or other such tools). Even more, you'll need to utfg a lot.

Can eclipse automatically rename the unit test class when the class under test is renamed?

I often use the refactor -> rename functionality in eclipse and I also have the habit of naming the associated unit test TestedClassNameTest. But when I rename my tested class I must not forget to rename my unitTest. It would be extremely useful to rename my unit test automatically when the tested class is renamed.
I guess it wouldn't be that difficult to create a plugin that does the job but maybe that isn't even necessary?
I've found a plugin that does the trick http://moreunit.sourceforge.net/
After several googling and eclipse searches, it seems such feature is not yet available.
Today there is no notion of "class being unit tested" in Eclipse. What I mean here, is that you can create a Unit test classes testing anything you want: a full package, a single class, a single method, a full plugin ....
To get more accurate, there is "NO relation in Eclipse's model" between your tested class and the associated unit test.
I totally agree with you that it would be nice to such a feature in Eclipse. To go further it would be really cool to be able to generate Unit tests skeletons and thus have these tests classes linked to the tested ones.
May be you can laucnh the discussion on Eclipse Buzilla, maybe in the PDE category.
Manu
eclipse would not figure this out to change: It only changes the references of the method used in other classes or in the same class.
If you really want to make this functionality work, you could extend eclipse's refactoring API as I did for my project and give it this new functionality.
If you like to have any references on this just ask me ;-)

Impact Analysis using Eclipse for Java application with framework code

Can impact analysis be done in Eclipse? If there are a few classes and methods that need to be changed, finding the impact of that change on rest of the application code (other classes and methods)
The core issue is when there is code apart from core java that is XML, JSP, framework code etc
One of the most advanced project on this topic might be XRay.
You can try it and check if that does provide some of the answer you are looking for (note: I have not yet tested it)
X-Ray is an open-source software visualization plug-in for the Eclipse framework. It provides System Complexity View, Class and Package Dependency View for a given Java project.
Other advanced tools exists (but are not free) for exploring code dependencies:
nWire for SO contributor extraordinaire Zviki Cohen (zvikico)
XDepend, now part of JArchitect (lets you extract, visualize, seek and control the structure of your applications and frameworks)
The most simple way (and still free) to make a quick dependency analysis remains for me:
CDA - Class Dependency Analyzer
(not directly integrated to eclipse, but very simple to use)
Simplest method is: right-click the class or method you would like to change, select "refactor" (or press alt-shift-T) and then the refactoring you propose to do (rename, move, change method signature, etc ). Then select "preview" (or next as the case may be). You'll then see the impact of the proposed change. For rename and move class, you'll also get the option to apply the changes to non-java files. Next to that, you can use the search function.
Try JRipple eclipse plugin. Its good one.
There is a plugin for jQAssisant available, which brings Test Impact Analysis to the Java world. The plugin is called jQAssistat Test Impact Analysis and available via https://github.com/jqassistant-contrib/jqassistant-test-impact-analysis-plugin.

Categories