Automatically create testcode from class (jars - java

We have thousands of classes without any true unit test coverage. As special project we aim to add reasonable coverage to all. One option is to create tests from sourced code and use GenerateTestCases plugin with IJ.
I am exploring other options to create tests massively..
Is it possible to create tests (okay, just test-templates) from class files? TestGen4J does this, but the project seems to be inactive. Is there any tool that could help in creating the tests for thousands of classes.
Once such tests are generated, I hope we have more momentum to add more unit tests for existing classes.

Google CodePro Analytix eclipse plugin can generate the unit tests automatically for you and it is actively maintained.

you can try netbeans tools too, select the class and right click tools/create Junit Test but its manual and ugly task to thousands files

Related

How to organize different java source folders in netbeans (for testing)?

I am developing a project in Netbeans (8.0.2) that uses JUnit (4.x) for testing application code (in a Netbeans module). In the same module I also have some integration tests. I found a way to separate application code, unit tests and integration tests by putting the latter into a separate folder under MODULE_PATH\test\qa-functional\src. By that, the Netbeans IDE puts those three semantical different kinds of classes into individual folders:
The problem is, my functional tests tend to grow complex, so I would prefer to have them, well, unit tested. However if create a unit test (in the Unit Test Package) for a class of the functional test package, the test does not compile. Seems to be some class path issues.
I know I can put the unit test of the functional test in the functional test package as well (and I will do so if this question does not provide me with some solution), but anyway, is there a way to solve this in Netbeans?
I found a bad solution, changing the order of ant targets in common.xml (in line 610 of NB 8.0.2). But that is obiously not portable, changes with a new version of Netbeans,.. So not really a solution. See details in Netbeans Forum: Classpath problem when unit testing functional tests
In Eclipse, I would simply add a new source folder on the Java build path. But that mechanism does not exist in Netbeans?
For clearification, I added a screenshot.
Thanks in advance for your help.
You are are trying to do a wrong thing:
unit tests should verify the smallest possible functionality, e.g. that a method is producing the expected result
functional tests in NetBeans represent integration tests, tests when you treat the application as a black box
It does not make sense to "unit test" the functionality in the "functional tests". NetBeans treats the tests folders independent so that they are not on each other's classpath, that's why it does not work in NetBeans.

In IntelliJ, how do I do Run with Coverage on a custom scope of tests?

I have a custom Scope with a bunch of tests included and excluded, and I want to perform Run with Coverage on just these tests. Is there a way to do that?
My current (poor) workaround is to expand all in the test sources folder, and manually shift-click a range of tests and control click off the ones I don't want to run.

Is it possible to run Eclipse application and Junit plug-in test simultaneously?

I have an eclipse application, where in i can create projects and perform some operations. I have written a test cases using Junit for some functions. To run these test functions, i am doing Right click on test class and Run as Junit Plug-in test and it is working properly. I am unable to do both the things at the same time. What i need is to run my eclipse application and Junit plug-in test simultaneously without human intervention. Junit plug-in test has to be done at run time. If there is a way to do that, then please suggest me the solution.
When you run it as Junit plugin test, then it already launches your eclipse plugins (and application), so there is absolutely no need to try to launch an additional application.
What probably confuses you, is that the test run and the "normal" manual run use two different workspaces. So if you try to access some files in your test which you created during normal operation, they will not exist. But you should never rely on such things, instead you have to create the necessary artifacts in the test setup method.

Is it possible to run incremental/automated JUnit testing in Eclipse?

Eclipse support incremental compiling. If I save a source file then it will compile the modified files.
Is it possible after such incremental compile also to run the JUnit tests of the same package and show the fail in the error view. Then I can see the JUnit test failing and compiling errors in the same view without extra action. Are there any plugins that can do it?
You have to look at these plugins:
JUnit Max: Not free, developed by Kent Benk (one of the men behind the TDD practice);
MoreUnit: Free, but essentially dedicated to help you write the tests;
Infinitest: Now free, this plugin is dedicated to run the tests related to the files you have just modified.
So regarding your needs, I suggest that you install MoreUnit and Infinitest plugins.
Use ExternalToolBuilder.
It can be triggered by source modify.
There’s Eclipse customized feature(integrate external tool builder) which may meet your need. But it needs extra effort to write the scripts I never used. Automatic test cases is not a convenient way, at least single click to see green bar in Eclipse is enough for me:)
You can run all tests in a project using Alt+Shift+X,T. I think that making it any more automated than this could take a serious performance toll. Incremental compilation is compiling at most 1 file at a time, but you're talking about running potentially hundreds of tests.

What GUI should I run with JUnit(similar to NUnit gui)

What GUI should use to run my JUnit tests, and how exactly do I do that? My entire background is in .NET, so I'm used to just firing up my NUnit gui and running my unit tests. If the lights are green, I'm clean.
Now, I have to write some Java code and want to run something similar using JUnit. The JUnit documentation is nice and clear about adding the attributes necessary to create tests, but its pretty lean on how to fire up a runner and see the results of those tests.
JUnit stopped having graphical runners following the release of JUnit 4.
If you do have an earlier version of JUnit you can use a graphical test runner by entering on the command line[1]:
java junit.swingui.TestRunner [optional TestClass]
With the optional test class the specified tests will run straight away. Without it you can enter the class into the GUI.
The benefits of running your tests this way is that you don't have the overhead of an entire IDE (if you're not already running one). However, if you're already working in an IDE such as Eclipse, the integration is excellent and is a lot less hassle to get the test running.
If you do have JUnit 4, and really don't want to use an IDE to run the tests, or want textual feedback, you can run the text UI test runner. In a similar vein as earlier, this can be done by entering on the command line[1]:
java junit.textui.TestRunner [TestClass]
Though in this case the TestClass is not optional, for obvious reasons.
[1] assuming you're in the correct working directory and the classpath has been setup, which may be out of scope for this answer
Eclipse is by-far the best I've used. Couple JUnit with a code coverage plug-in and Eclipse will probably be the best unit-tester.
There's a standalone JUnit runner that has a UI, but I recommend using one of the builtin test runners in the Java IDEs (Eclipse, Netbeans, and IntelliJ all have good ones). They all support JUnit, and most support TestNG as well.
If you want a standalone test runner (not the build-in IDE one), then for Junit3 you can use
junit.textui.TestRunner %your_class% - command line based runner
junit.swingui.TestRunner [%your_class%] - runner with user interface (swing-powered)
For Junit4, the UI-powered runners were removed and so far I haven't found a convenient solution to run new Junit4 tests on old swing-powered runner without additional libraries. But you can use JUnit 4 Extensions that provides a workaround to use junit.swingui.TestRunner. More here
Why you need a GUI runner? Can't you just run the tests from the IDE itself?
In .Net we have TestDriven.net, in Java there must be something equivalent. You can check out IntelliJ IDEA, it has the unit testing support built-in.

Categories

Resources