JUnit Tests - Is a call to JUnitCore.main() necessary? - java

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).

Related

How to enable processfork for unit tests in Gradle Android plugin

I have configured unit testing for AndroidStudio as described on the Android documentation (http://tools.android.com/tech-docs/unit-testing-support).
I would like to run every test method in its own JVM, so all static properties in my project are set back to their default values.
I couldn't find anything about this in the documentation and I'm afraid this is not possible yet.
I'm running my tests from the command line (gradlew --daemon test) as I didn't get the testrunner in Android Studio to work.
Does anyone know how to fork every testmethod in its own jvm process, so they run 'standalone'? Please let me know if this is possible or if there are alternative ways to run every testmethod in it's own process using gradle.
Thats easy. In your test task set
forkEvery 1
That will cause a new jvm to be forked for every single test.

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 subclass an ant task?

I wrote an ant task to run my project in NetBeans (I'm using a freeform java project). Now I want to write a debug task. The debug task is nearly identical to the run task except for a few added properties. Can I subclass the run task and add in the extra properties?
Since an Ant task implementation is a Java class, Ant tasks may be subclassed just like any other Java class. However, the usual method for testing tasks is to write both JUnit and AntUnit tests.
For examples, take a look at the Apache Ant source code under tests.

how to run playframework FunctionalTest through Eclipse?

and the second question:
is it possible to run them all in a bunch through Eclipse?
One approach could be, Install 'antify' module and run 'play antify' on your application. It will create a build.xml for your app (which would import the needed targets from application-build.xml file) and run 'auto-test' from eclipse.
FunctionalTest and UnitTest extend BaseTest, which is annotated with the PlayJunit4TestRunner, so you should find that you can just run tests as you would with any other test (i.e. Run As > Junit Test). You'll see the Play environment being initialised on the console before the tests actually run.
One problem I've found is that running a whole package of Play tests in Eclipse is buggy, so I tend to just run one at a time in Eclipse and then use Play's own testrunner to verify the whole suite.

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