Java 卡单元测试
我想在某种模拟/模拟环境中运行我的 Java Card 应用程序,以便能够对它们运行 JUnit(或任何其他单元测试框架)测试。有谁知道这样的工具吗?我更喜欢一些开源项目。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为此,我们的团队专门开发了 jCardSim:开源 JavaCard 模拟器 - http://code.google.com /p/jcardsim/。
单元测试示例:http://code.google.com/p/jcardsim/source/browse/trunk/src/test/java/com/licel/jcardsim/base/SimulatorTest.java
它完全模拟真实NXP 芯片 JavaCard。尝试使用它。
欢迎任何意见和想法!如果您分享该项目的链接,我们将不胜感激!
Special for this purpose our team developed jCardSim: open-source JavaCard Simulator - http://code.google.com/p/jcardsim/.
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!
请注意,执行此操作的一种方法是在 Java SE 中创建 JUnit 测试,并使用 APDU 与 Java Card 中的类进行通信。显然这与直接在 Java Card 中测试类不同,但乞丐不能是选择者。一般来说,您需要创建一个小程序来为您执行一些转换。这是 Mikhail 的 jCardSim 答案 中使用的方法 - 它当然也可以在真实的卡上使用,但是您将失去所有代码覆盖率以及可能的调试选项。
另一种方法(我有一个专有的、可行的解决方案)是在卡上实现单元测试,并从 Java SE 的 JUnit 测试框架中调用它们。这是一项繁重的工作;我必须生成自己的
List
实现才能使其正常工作。不过这是值得的;它允许我使用 Java Card 中定义的对象和类型。据我所知,没有开源替代方案。可以选择将 JUnit 测试框架添加到 jCardSim 并直接测试类,因为完整的 JRE 在 Applet 编译期间可用。请注意,jCardSim 还不是功能齐全的 Java 卡(例如,没有按照 API 中指定的方式重置内存、没有事务支持等),因此在测试 Java 卡特定代码时请注意这一点。
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.
不了解 Java Card,但是,乍一看,它似乎与任何其他 JavaME 平台没有太大区别(我所说的不同是指将它们与 J2SE 等进行比较)。因此,您可以使用任何普通的测试环境,例如 JUnit 测试环境 Eclipse/Netbeans 使开发变得更容易。您可能需要 powermock 来模拟一些特定于平台的内容。希望这有帮助。
Netbeans 的一个提示:您可以考虑单独的项目进行测试(这取决于您的应用程序项目)。在这种情况下,您可以使用最新 Java 玩具的功能对您的 UT 进行编码。
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 卡小程序是简单的 Java 代码片段。所以,JUnit 没问题。像测试常规 Java 类一样测试它们。
但问题是我从未见过 javacard.* 和 javacardx.* 包中类的 java 实现。如果你能得到它们——问题就解决了。
如果您想测试小程序本身如何工作(APDU 发送/接收),那么 Java Card SDK 中有 cref.exe。这是一个javacard模拟器。
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.