In Eclipse, it is very easy to run Java classes and Junit tests with a click, Eclipse does all the hard work of figuring out the class path.
But if I want to create an Ant task that will run a specific Java class or Junit test - I have to figure it out myself.
Is there a way to tell Eclipse to automatically create an ant build file for these tasks?
Good afternoon,
I am running some JUnit tests on my application using ant. In doing so I am following the instructions in the step-by-step Spring-MVC tutorial. [*]
The instructions never mention a call to org.junit.runner.JUnitCore.main() in running a test. My question is, is it necessary to call org.junit.runner.JUnitCore.main to run a test if you are running the tests through command-line ant (as opposed to an IDE)? Or is ant smart enough to locate all the methods in a TestCase subclass and run all of them without an explicit call to JUnitCore.main()?
[*] http://static.springsource.org/docs/Spring-MVC-step-by-step/part3.html
Thanks,
ktm
Ant knows what to do. As long as you're using the right ant-task for that (like jUnit task: http://ant.apache.org/manual/Tasks/junit.html).
I need to run a java class (actually a test case) from ant script. Is it possible to do so?
At the most basic level you could use the ant java task to do this.
But you tagged with junit4 - can you not use the ant junit task?
To execute java class, you can use java task http://ant.apache.org/manual/Tasks/java.html
To execute junit test cases: http://ant.apache.org/manual/tasksoverview.html#testing
Try the java task. If you want to run tests, you might want to take a look at JUnit
I'm currently using ANT for building my Java project on a Windows XP machine.
In my build.xml file I've defined 3 task and I would like that,in case of fail,a default task be executed before closing the building and exiting (like a recovery procedure). I would like to know if it's possible.
thanks
Googled and found this. It's basically a try/catch for Ant. Might be worth a look: http://ant-contrib.sourceforge.net/tasks/tasks/trycatch.html
Never heard of such a property/task, but the follwing just comes to my mind: you could use an additional 'Master' ant script.
The master script (a new one) includes all public targets from the original one and delegates the work to the corresponding task in your build script (ant calls)
If the delegate fails, the master should be able to recognize the failure and could call the 'clean-up' task (either on the 'master' or on the original build file)
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.