# Unit testing J/Link projects in Workbench

I am having some problems setting up the MUnit testing framework for a J/Link project and would be grateful for advice. I do have J/Link working when running or debugging the .nb notebook in workbench, just can't get it going when trying to run or debug the .mt unit test file.

I did the following steps, following the documentation as closely as i can.

I created a fresh J/Link project, right-click the inner project folder and choose New / TestFile, and I get a tree like the following in the package explorer of workbench:

fooProject
+-- javaSource
+-- JRE System Library [1.1]
+-- fooProject
|   +-- Java
|   +-- Kernel
|   |   +-- init.m
|   +-- fooProject.m
|   +-- fooProject.mt <---- this is my unit test file; can't get it going
+-- fooProject.nb     <---- this one works well


I put a couple of functions in my .m package file:

(* Mathematica Package *)
(* Created by the Wolfram Workbench Feb 3, 2012 *)
BeginPackage["fooProject", {"JLink"}]
(* Exported symbols added here with SymbolName::usage *)
KvpQ::usage = "Tests whether its argument is a KVPair or KVP, that is, a list of two values, the first of which is a string or symbol Key.";
Begin["Private"]
(* Implementation of the package *)
SymbolQ[x_Symbol] := True;
SymbolQ[___] := False (* all other cases produce False *);
KvpQ[kvp_List] := (Length@kvp === 2 &&
(StringQ[kvp[[1]]] || SymbolQ[kvp[[1]]]));
KvpQ[___] := False;
End[]
EndPackage[]


Then I open the MUnit Test file, fooProject.mt, do Ctrl+K and choose the test template, and then fill it out:

(* Mathematica Test File *)
Test[
KvpQ[{}]
,
False
,
TestID->"fooProject-20120203-Q0O8P9"
]


Now, following the directions at the wolfram site on J/Link launching, I create the new configuration as directed accepting all defaults. Then when I try to run the unit tests, I get a dialog with the unsettling message:

The selected file is not a Mathematica notebook or scrapbook file.
Please make sure that a file with extension .nb or .mscrapbook
is selected, then try the run again.


This leads me to suspect that MUnit testing just isn't supported for J/Link projects, which would make me sad :( But it's more likely I've just done something silly wrong.

Again, I'd be grateful for any advice or guidance. Thanks.

-
Upgrading to Workbench 2.0 solved this problem. –  Reb.Cabin Feb 14 '12 at 21:33

MUnit testing is surely supported on JLink projects (I used it in JLink projects which also contained Java classes, without problems). In fact, MUnit is all about Mathematica, so you just follow the usual procedure. I actually never bothered to create configurations etc.

I just took your code, created a J/Link project, and run the unit test file as Run As -> Mathematica test (position mouse on any place within a unit test file in the WorkBench editor, then right-click and choose Run-As). Everything was fine.

-
I'll reboot and give it another try and get back –  Reb.Cabin Feb 4 '12 at 21:27
Ok, I can get the unit tests to run, but only after a very painful pair of modal dialogs that say "Could not find the main class: Files\Wolfram. Program will exit." This is telling me that the J/Link launcher configuration wasn't used, which suggest that once I actually start using Java, I will have total failure. If I do use the J/Link launcher, according to the documentation, I get the message above ("The selected file is not a ...") So I am still blocked, but will now try a completely fresh project and get back. –  Reb.Cabin Feb 5 '12 at 8:13
same after cold start of the machine; same after fresh project and copying source. I'm stumped :( –  Reb.Cabin Feb 5 '12 at 8:25
to clarify my comment above the last, where I say "I can get the unit tests to run..." I can get them to run only by right-clicking in the Test.mt file and choosing "Debug As.../1 Mathematica Test" instead of choosing "Debug As.../Debug..." and then choosing the J/Link launcher. I have no other problems with Java or J/Link on this machine, so I'm really stumped. –  Reb.Cabin Feb 5 '12 at 8:29
@Reb.Cabin This is rather strange. Did you create your project as a J/Link project in WorkBench? And do you have a real WorkBench, or you installed a plugin for the existing Eclipse installation? I was using the WorkBench proper, and created a J/Link project (when you create a project, choose Create project -> Mathematica -> J/Link project). I suppose it does automate some things, may be this is why I never had any problem with that. –  Leonid Shifrin Feb 5 '12 at 8:33

MUnit will absolutely work for testing Mathematica code in J/Link projects.

The J/Link project does indeed setup some paths and things, and you should be able to go ahead and write MUnit tests that use Mathematica code that calls Java without any trouble.

When I setup your project as you stated here, and then right click on the test and either run or debug as Mathematica test, then it works just fine, no errors dialogs or others.

I think the confusion here is on the role of the launch configuration where you setup the Mathematica and Java pieces. You only need this if you wish to debug (ie set breakpoints and suspend) both Mathematica and the running JVM at the same time, and currently as the message states only scrapbook and notebooks are supported file types for launching in this way.

-
Also, you shouldn't really put test files in the application folder. Projects allow you to wrap up both deliverable files and files you need in development. Tests are not deliverables, and so should get a slightly different location, I normally add a Tests folder at the root of the project and add test files in there. –  Adam Berry Feb 6 '12 at 15:48
I understand your last paragraph; thanks! That saves me one branch in the debugging adventure. I am now consistently getting "Could not find the main class: Files\Wolfram. Program will exit" when right-clicking and saying "Run" or "Debug" as Mathematica test. Still blocked. –  Reb.Cabin Feb 7 '12 at 21:37
I think whats happening is that because you already created a launch configuration, that is being used when you click Run or Debug. So open the configurations dialog (Run > Run configurations), select the config for the test file, make sure that you have nothing selected in the J/Link launch drop, then try. If that doesn't work try deleting all Mathematica configurations and try right clicking, Run As... again. –  Adam Berry Feb 8 '12 at 15:11