See which of your colleagues or former colleagues are already on Java Link: Check out the Contact Finder
News »Browse Articles » How to use JUnit to test your JavaFX applications with NetBeans IDE
0
Vote Vote

How to use JUnit to test your JavaFX applications with NetBeans IDE

Views 1 Views    Comments 0 Comments    Share Share    Posted 07-06-2009  
So, we have JavaFX 1.2 and upgraded support for NetBeans IDE 6.5.1. After a few hours of use, I can confirm that the programmer`s experience is now really improved, with the editor much more supportive than before. What`s missing of importance still? Simple answer: JUnit support.

In fact, strangely enough, there`s no support for tests; not even a specific folder to put them in. A few people blogged about some tricks to run JUnit in a JavaFX project, treating JUnit as a simple library; but this approach didn`t satisfy me, also because I want something that produces the regular reports (e.g., to be used by Hudson) and doesn`t require to write too much extra code (such as explicitly creating TestSuites); in a word, something that will allow me to have all the things working just as they are when JUnit tests will be available in NetBeans IDE.

I was able to do that, at the price of applying two small patches to JUnit. Here it is how to do in six steps.
1. Create a new JavaFX project and put a dependency on your primary project

You need to do that because you don`t want to put test sources together with regular sources. You have to create a Dummy.fx class with a void run() function and declare it as the main class in the project properties: NetBeans treats every project as it was a complete application and wants an entry point.
view source
print?
1.public function run(): Void
2. {
3. }

You need to put the JAR of JUnit as a library of your test project - but a patched version of JUnit (see below).
2. Create the tests

Keep the usual conventions, such as putting a test in the same package as the fixture; name the test with the ****Test pattern and test methods test***(). Since in JavaFX there are no annotations, you have to use the JUnit 3.x approach, that is you have to extend TestCase. I`m including a sample of a real test from blueBill Mobile (which just a few not relevant stuff omitted for brevity). Note that to assert the equality of two sequences I had to write a small ad hoc function, as sequences aren`t array and aren`t known to JUnit. Such code (and other similar) should probably packed in a specific, small library.
view source
print?
01.package it.tidalwave.bluebillmfx.taxon.controller;
02.
03.import java.lang.System;
04.import it.tidalwave.bluebillmfx.taxon.model.Taxon;
05.import it.tidalwave.bluebillmfx.taxon.model.TaxonomyMock;
06.import it.tidalwave.bluebillmfx.taxon.model.TaxonomyImpl;
07.import junit.framework.TestCase;
08.import org.junit.Assert;
09.
10.public class TaxonSearchControllerTest extends TestCase
11. {
12. def mockTaxonomy = TaxonomyMock{};
13. def fullTaxonomy = TaxonomyImpl{};
14.
15. postinit
16. {
17. def is = getClass().getResourceAsStream("EBNItalia2003.json");
18. fullTaxonomy.load(is);
19. is.close();
20. }
21.
22. def fixture = InstrumentedTaxonSearchController
23. {
24. taxons: mockTaxonomy.species;
25. }
26.
27. def performanceFixture = InstrumentedTaxonSearchController
28. {
29. taxons: fullTaxonomy.species;
30. }
31.
32. public function testFunction(): Void
33. {
34. assertFilter("", ["Airone cinerino", "Airone bianco maggiore", "Airone rosso", "Piro piro", "Piro piro boschereccio"], -1, "");
35. assertFilter("A", ["Airone cinerino", "Airone bianco maggiore", "Airone rosso"], -1, "Airone ");
36. ...
37. }
38.
39. function assertFilter (filter : String, expected : String[], expectedIndex : Integer, expectedLeading : String)
40. {
41. fixture.filter = filter;
42. assertEquals(expected, displayNames(fixture.filteredTaxons));
43. assertEquals("fixture.selectedTaxonIndex", expectedIndex, fixture.selectedTaxonIndex);
44. assertEquals("fixture.leading", expectedLeading, fixture.leading);
45. }
46.
47. function assertEquals (expected : String[], actual : String[]) : Void
48. {
49. Assert.assertTrue("{actual}", expected == actual);
50. }
51. }
3. Create a specific Ant target for testing

It is pretty much copied from similar stuff in NetBeans, just patched for getting the right stuff in the classpath (this could be done in a better way, but it presently works).
.............

Source:
http://java.dzone.com/articles/how-use-junit-test-your-javafx
0
Vote  Vote
Enter your comment:
No Comments For This News

Search News

What's the News?

Post a link to something interesting from another site, or submit your own original writing for the Java community to read.

Most Popular News

Most Recent User Submitted News