15

I'd like to run my Java Card applications in some kind of emulated/simulated environment to be able to run JUnit (or any other unit test framework) tests on them. Does anyone know of such a tool? I'd prefer some open source project.

Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
AO.
  • 151
  • 1
  • 3
  • 1
    In case anyone doesn't know what "Java Card" refers to: http://java.sun.com/javacard/ – Tyler Feb 27 '10 at 20:15

4 Answers4

12

Special for this purpose our team developed jCardSim: open-source JavaCard Simulator - http://code.google.com/p/jcardsim/.

//1. create simulator
Simulator simulator = new Simulator();
//2. install applet
simulator.installApplet(appletAID, HelloWorldApplet.class);
//3. select applet
simulator.selectApplet(appletAID);
//4. send apdu
ResponseAPDU response = simulator.transmitCommand(new CommandAPDU(0x01, 0x01, 0x00,   0x00));
//5. check response
assertEquals(0x9000, response.getSW());

Unit-test example : http://code.google.com/p/jcardsim/source/browse/trunk/src/test/java/com/licel/jcardsim/base/SimulatorTest.java

It's fully emulate the real NXP-chip JavaCard. Try use it. Any comments and ideas are welcome! We will be grateful if you share link to the project!

MikhailDudarev
  • 461
  • 4
  • 8
  • 1
    I think that jCardSim is a very interesting project (and I'll try to contribute) but to say that it already is able to fully emulate "the real NXP-chip" is taking a bit too far. Memory management, transaction support, proprietary libraries, multiple protocol support, extended length etc. are all still missing. – Maarten Bodewes Aug 23 '13 at 11:02
1

Note that one method of doing this is to create JUnit tests in Java SE, and use APDU's to communicate with the classes in Java Card. Obviously this is not the same as directly testing classes in Java Card, but beggars cannot be choosers. In general you would need to create an Applet that does some conversions for you. This is the method that is used in the jCardSim answer from Mikhail - it could of course be used on a real card too, but you would lose any code coverage and - possibly - debugging options.

Another method (that I've got a proprietary, working solution for) is to implement unit tests on the card and call them from a JUnit test framework from Java SE. This is a lot of work; I've had to generate my own List implementation for this to work. It was worth it though; it let me use Java Card defined objects and types from within Java Card. There is no open source alternative for this as far as I know.

It could be an option to add a JUnit test framework to jCardSim and test classes directly, as the complete JRE is available during compilation of the Applet. Note that jCardSim is not a fully functioning Java Card yet (e.g. no reset of memory as specified in the API, no transaction support etc. etc.) so be aware of this when testing Java Card specific code.

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
0

Don't know the Java Card, however, at first glance, it seems not much different than any other JavaME platforms (by different I mean comparing them to e.g. J2SE). As such you may use any ordinary e.g. JUnit test environment with Eclipse/Netbeans to make development little bit easier. You may need powermock to mock some platform specific stuff. Hope this helps.

One tip for Netbeans: you may consider separate project for tests (that depends on your application project). In that case you can have your UTs coded with the power of latest Java toys.

Przemek Kryger
  • 687
  • 4
  • 11
0

Java card applets are simple pieces of Java code. So, JUnit is fine. Test them like you test regular java classes.

But the problem is that I never saw java implementations of classes in javacard.* and javacardx.* packages. If you will get them - the problem is solved.

If you want to test how applet itself works (APDU send/receive), then there is cref.exe in the Java Card SDK. It's a javacard emulator.

Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
  • 2
    This may be slightly possible now with jCardSim, but generally you need a Java Card Runtime Environment (including Java Card VM) to run Java Card code. The Java Card SDK only has a very minimal set of implemented API (no crypto for instance) so it is of minimal use, and you cannot directly run Unit tests on them. – Maarten Bodewes Aug 23 '13 at 11:00