⬆️ ⬇️

Mobile JUnit exists

It so happened that our office wrote on J2ME a lot and for a long time, but never used unit-tests. And I, having read several incendiary articles on Habré, imbued with the bright ideas of unit-tests and wanted to at least try. My colleagues shrugged their shoulders and said that JUnit would not work on J2ME anyway, and in general, how can we run tests away? On the mobile phone? As a result, I did it, although I had to tinker.



Choosing a framework



The selection was small. Google told me that there are such options:



I, I repent, chose, without looking, option SE. For reasons in the spirit of "well, JUnit from the SE should probably work with SE emulators without any problems." Perhaps it was the most unfortunate option ... 8)



Installation



The downloaded archive contained libraries, sample code, and an example of ant ant.



I will quote one of the tasks:

< target name ="run-with-coverage" >

< java

classname ="com.sonyericsson.sdkme.junit.OnDeviceTest"

fork ="true" failonerror ="true" >

< classpath refid ="test-classpath" />

< arg value ="--javac:${javac}" />

< arg value ="--project-dir:${project-dir}" />

< arg value ="--device:SonyEricsson_K750_Emu" />

< arg value ="--compile:true" />

< arg value ="--compile-midlet:true" />

< arg value ="--coverage:L" />

< arg value ="--coverage-report:${project-dir}/coverage.html" />

< arg value ="--name:Mobile JUnit Sample Project" />

</ java >



< echo message ="A coverage HTML report has been written to ${project-dir}/coverage.html" />

</ target >



* This source code was highlighted with Source Code Highlighter .


It is seen that a certain class is called with different parameters. Nowhere, by the way, is not documented. Kopipast results did not give. The problem was in the bootclasspath. Thoughtful reading of the decompiled code showed that the library eats parameters of the form - <name>: <value>; <name>: <value> ... In this case, it is unrealistic to write in the bootclasspath a list of libs, the path to each of which begins with C :. And, by the way, it takes spaces as separators along with a semicolon. So, no “Program Files”. All this was decided by unpacking the necessary libs into one directory and indicating the path to it in the bootclasspath.

After all the corrections, the java call looks like this:

< java classname ="com.sonyericsson.sdkme.junit.OnDeviceTest" fork ="true" failonerror ="true" >

< classpath >

< pathelement path ="${env.MJUNIT_HOME}/junit.jar" />

< pathelement path ="${env.MJUNIT_HOME}/mobile-ju-1.0.jar" />

< pathelement path ="${java.home}/lib" />

</ classpath >

< arg value ="--project-dir:${basedir}" />

< arg value ="--device:SonyEricsson_K750_Emu" />

< arg value ="--compile:false" />

< arg value ="--compile-midlet:false" />

< arg value ="--coverage:L" />

< arg value ="--runmode:${tests.runmode}" />

< arg value ="--coverage-report:${basedir}/coverage.html" />

< arg value ="--name:Stiter Client" />

< arg value =""--wtk:${env.WTK_HOME}"" />

< arg value ="--print-config:on" />

< arg value ="--midlet-under-test:${jarpath}" />

< arg value ="--bootclasspath:"${toString:tests.bcp}"" />

</ java >




* This source code was highlighted with Source Code Highlighter .


')

Using



After the described actions, the test midlet was assembled and launched. But tests fell with errors. The treatment was reduced to the fact that it is not necessary to create an instance of your midlet in the tests. That is, so wrong:

public void setUp() throws Throwable {

super.setUp();



midlet = new OurMIDlet();

}




* This source code was highlighted with Source Code Highlighter .


And rightly like this:

public void setUp(MIDlet miDlet) throws Throwable {

super.setUp(miDlet);



testMidlet = miDlet;

OurApplication.setMidletInstance(testMidlet);

}




* This source code was highlighted with Source Code Highlighter .


At the same time, testMidlet is an instance of the test midlet, and not the working one.



That's all. I hope someone will be useful.

Do you use any alternatives to the SE library? What do you recommend?

Source: https://habr.com/ru/post/51519/



All Articles