开发者

Java Card Unit Testing

开发者 https://www.devze.com 2022-12-21 22:36 出处:网络
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开发者_JAVA百科 s

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开发者_JAVA百科 such a tool? I'd prefer some open source project.


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!


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消