What do you need to do Unit testing in Java with Eclipse? - java

I've recently been lifted out of the .Net world into the Java world and I miss my unit tests.
Using Visual Studio I used NUnit and TestDriven.net to run my unit tests.
What is a comparable system for Java Using Eclipse?
I'm looking specifically for the plugins that will get me going, or a guide on how to do it.
I'm aware that JUnit is what NUnit was initially based on, but I want to know the best way to integrate it into Eclipse as there seem to be a few plugins that do this and I don't have the time to play around with them all.
UPDATE
Okay I didn't know that JUnit was built into the IDE. Are there any plugins that make using JUnit any easier?

Using JUnit with eclipse is actually very easy. Just go to File->New... and select JUnit Test Case. Eclipse will handle adding the JUnit library and all of the imports.

Which version of Eclipse are you using?
For as long as I remember (I've been using Eclipse since early 3.xs), Eclipse supports JUnit out of the box. You just:
Right-click on a project -> Run As -> JUnit Test
Does this not work for you?

I've been using moreUnit for a few years and can't live without its Ctrl+J shortcut to switch between the class and its test case.
I've also found EclEmma useful for finding untested code.

Easier than "Right-click on a project -> Run As -> JUnit Test"? Like you want it bound to a keypress (because it probably is). Lemme check--Yeah, alt-shift-X, then "T". Easy enough?
There is also window/show view/other/java/JUnit that will give you a junit run bar in a window. You can then just hit the run tests button and it will run all the tests in your project/section.
Ctrl-shift-L is great for figuring out keybindings if you are getting to know eclipse.
Also, get VERY familiar wtih ctrl-space, just press it whenever you're in the middle of typing something (seriously, try it with everything!) Also type "sysout[ctrl-space]"

JUnit 4 is actually really easy to use, as long as you're using a project that targets Java 5 or newer, and have added it to the project.
I mean, how much easier can you get than
#Test
public myTest() {
// Test code here
}
There are also #Before, #After, #BeforeClass, #AfterClass, and #Ignore. The *Class methods need to be static methods. #Before runs before each test, #BeforeClass runs before the first test... but keep in mind that JUnit tests can run in any order.
If you're doing database tests, you may want to look into the DBUnit addon, although you need to take some special steps to use it with JUnit 4's native mode.

What do you mean with "make using JUnit any easier"?
I use Ant for runnings tests as a task. Output will be stored into a flat file or a html file. It depends on the TestRunner.
Please specify your question and you'll get answers! :)

fit (http://www.theserverside.com/news/thread.tss?thread_id=39417)
dbunit (http://www.dbunit.org/)
many others
in eclipse, you can right click a package and select run as a junit test.
be careful of http://xunitpatterns.com/test%20fixture%20-%20ambiguous.html. iirc, this boils down to junit creating an instance of each test case before calling setup and nunit just creating one instance.

I've used the testNG which has a plug in for eclipse.

Related

Is it possible to generate Cucumber HTML Reports with Karate's JUnit5 fluent API?

Our team is starting a JUnit 5 project with karate tests.
Currently we are using this as a template for our Karate test runner https://github.com/intuit/karate#junit-5-parallel-execution.
It allows us to pass in the "target/surefire-reports" and then before the test finishes we call ReportBuilder.generateReports(). It is basically identical to this code https://github.com/intuit/karate/blob/b50202b3c8a8916a7db0f3d5196d42086ab80a04/karate-junit4/src/test/java/com/intuit/karate/mock/MockServerTest.java.
This works well, but while I was looking at how to set up JUnit 5 I noticed this very slick fluent api https://github.com/intuit/karate#junit-5.
It would be nice to use that syntax, but I can't get the Cucumber report generated like I can with Runner.parallel. I made sure the maven-surefire-plugin was in build.gradle(although I could have messed that up) but it didn't seem to help.
I also tried doing ReportBuilder.generateReports() and the related logic from the parallel execution example in the #AfterAll function, but couldn't get that working either. The errors suggested that the target/surefire-reports folder didn't exist.
Is the cucumber report supported in the second example? If so, is there a trick to getting it setup?
Great question. The reason we de-couple the JUnit execution and the parallel-runner - is JUnit is more useful in development mode, and you expect detailed pass/fail stats in the IDE for example. But this will be an un-necessary overhead in "CI mode".
That said, we have put in some work on making the Parallel runner a fluent interface, so great timing :) You can find an example on line 57 here.
May I request you to try the develop branch and see if you are missing anything ? Building is easy, here are some instructions: https://github.com/intuit/karate/wiki/Developer-Guide

Can I get a Cucumber feature and its steps from a variable?

I'm new to BDD and particularly Cucumber.
Can I get a features and its steps from a variable? Also, I want to get a feature and its steps from a test tracker (TestRail) before running tests by the special selection of this tests, and put it in a list, then one by one get a scenario and run it.
Is there such a possibility? Should I use Cucumber or another framework for this?
No, you can't define a Cucumber scenario in code (or at least not in a supported way). But if you were going to write code to get a scenario and its steps from your test tracker and run it, you could equally well write code to put the scenario and its steps in files and run the scenario with the cucumber executable.
I don't know of a Java testing framework in which you can define tests dynamically. You could do that in Ruby with RSpec or (less cleanly) minitest. But I don't know whether a Ruby test framework would be acceptable, or whether it would be OK for the people writing entries in your test tracker to have to read and/or write RSpec examples. (It seems strange to have Cucumber step definitions in a test tracker, too; having features in a test tracker seems more reasonable, aside from the question of how to run them.)

What's the usage of JUnitCore.runClasses?

I've seen some examples showing that running a JUnit class with the JUnitCore.runClasses(Test.class). However, we can easily run the JUnit test classes by right clicking the class file and "Run With->JUnit" in most IDEs. Then, my question is: with the IDEs, what's the usage of JUnitCore.runClasses? Is it still necessary to write JUnit classes using JUnitCore.runClasses?
JUnitCore#runClasses is usually used when you want to write a program to tests (i.e., a runner).
Since you're running from inside an IDE, there's probably no reason for you to use it in this scenario.

Run JUnit automatically when building Eclipse project

I want to run my unit tests automatically when I save my Eclipse project. The project is built automatically whenever I save a file, so I think this should be possible in some way.
How do I do it? Is the only option really to get an ant script and change the project build to use the ant script with targets build and compile?
Update I will try 2 different approaches now:
Running an additional builder for my project that executes the ant target test (I have an ant script anyway)
ct-eclipse, recommended by Thorbjørn
For sure it it unwise to run all tests, because we can have for example 20.000 tests whereas our change could affect only, let's say 50 of them, among which are tests for the class we have changed and tests for classes that collaborate with our class.
There is an unseful plugin called infinitetest http://improvingworks.com/products/infinitest/ which runs only some tests ( related to class we've changed ) just after we save changes. It also integrate quite nicely with editor ( using annotations ) and problem view - displaying not-passing tests like errors.
Right click on your project > Properties > Builders > New, and there add your ant ant builder.
But, in my opinion, it is unwise to run the unit tests on each save.
See if Eclipse has a plugin for Infinitest.
I'd also consider TestNG as an alternative to JUnit. It has a lot of features that might be helpful in partitioning your unit test classes into shorter and longer running groups.
I believe you are looking for http://ct-eclipse.tigris.org/
I've experimented with the concept earlier, and my personal conclusion was that in order for this to be useful you need a lot of tests which take time. Personally I save very frequently so this would happen frequently, and I didn't find it to be an advantage. It might be different for you.
Instead we bit the bullet and set up a "build server" which watches our CVS repository and builds projects as they change. If the compilation fails or the tests fail we are notified quickly so we can remedy it.
It is as always a matter of taste what works for you. This is what I've found.
I would recommend Inifinitest for the described situation. Infinitest is nowadays a GPL v3 licensed product. Eclipse update site: http://infinitest.github.com
Then you must use INFINITEST. INFINITEST helps you to do Continuous Testing.
Whenever you make a change, Infinitest runs tests for you.
It selects tests intelligently, and only runs the ones you need. It reports unit test failures like compiler errors, and provides additional information that helps you write better tests.

testNG tests extending BaseTest

We noticed that when testNG test cases extend TestCase (JUnit) those tests start executing as Junit tests. Also, I should probably mention, the tests are run through Maven.
Is this a bug or a feature? Is it possible to override this behavior and still run those types of tests as TestNG tests? Do you know a link where TestNG talks about this?
thanks.
I didn't think either TestNG or JUnit required any base classes now that both use annotations to specify test methods. Why do you think you need to extend a class? And why on earth would a TestNG class extend the JUnit base class TestCase? Is it any surprise that they run as JUnit tests?
It sounds like neither bug nor feature but user error on your part. I'm not sure what you're getting at here. Why would you do this?
UPDATE: Your question is confusing me. Did you have JUnit tests running successfully that you're not trying to convert to TestNG, or visa versa? I'm having a very hard time understanding what you're trying to achieve here. Leave Maven out of it. It's immaterial whether they're run by you, Ant, or Maven.
Looking at the maven surefire plugin info I can't see any way to select a test for TestNG processing only if it also extends a jUnit 3 class.
IFAIK your best bet is to just work on each class seperately, removing the jUnit references and then retesting. That way you never have the mixture in one class and you should avoid problems. To make the work manageable I would be inclined to do this only when I was changing a test case for some other reason.

Categories

Resources