Installing Ant on Device to Run JUnit
Home › Forums › MultiConnect OCG › Installing Ant on Device to Run JUnit
- This topic has 5 replies, 2 voices, and was last updated 11 years, 5 months ago by Ian McCoy.
-
AuthorPosts
-
May 16, 2013 at 8:07 pm #2937Ian McCoyParticipant
Hi,
I would to run JUnit on the device to test my software which I think would be helpful for unit testing and/or integration type tests on the device (Apparently one needs ANT to call JUnit?). I’m using the following guide to install Ant: http://ant.apache.org/manual/index.html
Under the section labelled “Linux/Unix (bash)” an export variable named “JAVA_HOME” needs to point to the JVM. Where is it located in the file structure? I have tried pointing it to /usr/bin/java, /usr/lib/jvm/java-6-openjdk/jre/bin/java, but I’m getting an error stating the environment variable is not set properly.
I have tried grep and find to no avail.
Any help would be appreciate.
Ian
May 16, 2013 at 9:05 pm #4545Jonathan EngbrechtBlockedIan,
I actually used JUnit for developing the unit tests for the SDK. That said, I did not use ANT in this process at all. If you want to perform Unit or functional testing on the device, the easiest way to do it is using your existing development and build system. I did all of mine in NetBeans, but it should be the same for Eclipse or whatever you are using. All you need to do is create a new project called, AppTest or whatever you want to call it. Then add your application and the JUnit jar to this project as libraries. Next write all of your test classes and suites in the JUnit format using its built in classes and methods, etc… From the main method within your test application you will use something like the following:
Result results = JUnitCore.runClasses(tests);
printResults(results);
Where tests is an array of JUnit classes or test suites, see the JUnit javadocs for more detailed usage information. I have also included a sample of what printResults might look like below.
public static void printResults(Result results) {
System.out.println();
System.out.println(”
Test Results
“);if (results.wasSuccessful()) {
System.out.println(“All Tests Passed!!!”);
System.out.println();
} else {
System.out.println(“Total Failures: ” + results.getFailureCount());
System.out.println();
}
List<Failure> list = results.getFailures();
for (int i = 0; i < list.size(); i++) {
Failure failure = list.get(i);
System.out.println(“Failure: ” + (i + 1));
System.out.print(failure.getTestHeader());
System.out.println();
switch (printLevel) {
case NONE:
break;
case MESSAGE:
System.out.println(failure.getMessage());
break;
case EXCEPTION:
System.out.println(failure.getException());
break;
case TRACE:
System.out.println(failure.getTrace());
break;
}
System.out.println();
}
}
Here printLevel is an Enum that I set as part of a command line parameter to detemerine the level of debug I want on the test output.
Once you have developed and compiled your test application using your existing development environment, you can copy it to an OCG and run it just like any other Java application. Let me know if this was helpful or what you were looking for! Thanks!!!
– Jonathan
May 17, 2013 at 1:41 pm #4546Ian McCoyParticipantJonathan,
Thanks, This was really helpful. I’ll try this approach. If I get stuck, I may ask another question.
Regards,
Ian
May 17, 2013 at 3:53 pm #4547Ian McCoyParticipantJonathan,
Can you please add some more details on how you declared and added tests for the ‘tests’ variable you mentioned above.
Most of the documentation I have read mentioned using annotations to declare tests suites, like as follows:
// specify a runner class: Suite.class
@RunWith(Suite.class)
// specify an array of test classes
@Suite.SuiteClasses({
HelloTest.class,
ExpectedExceptionTest1.class,
ExpectedExceptionTest2.class}
)
I’m struggling how to get an array of classes using this approach to pass into runClasses(tests) method.
Regards,
Ian
May 17, 2013 at 4:30 pm #4548Jonathan EngbrechtBlockedIan,
Here is an example of how to declare an array of these test classes:
Class[] tests = new Class[4];
tests[0] = MeasurementsSuite.class;
tests[1] = ProcessSuite.class;
tests[2] = TypesSuite.class;
tests[3] = UtilSuite.class;
Here is an example stubbed out suite class:
@RunWith(Suite.class)
@Suite.SuiteClasses({test.util.JSONObjectTest.class, test.util.ByteQueueTest.class, test.util.JSONArrayTest.class, test.util.CommandLineTest.class})
public class UtilSuite {
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
}
In this case there is no need to setup or tear down anything as this suite class simply calls a number of other JUnit test classes.
Here is an example JUnit test class:
public class CommandLineTest {
private static CommandLine instance;
public CommandLineTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
String[] args = {“Arg1”, “Arg2=Value2”, “Arg3=test=test=test”};
instance = new CommandLine(args);
}
/**
* Test of argumentPresent method, of class CommandLine.
*/
@Test
public void testArgumentPresent() {
System.out.println(“argumentPresent”);
String arg = “Arg1”;
boolean expResult1 = true;
boolean result1 = instance.argumentPresent(arg);
assertEquals(expResult1, result1);
boolean expResult2 = false;
boolean result2 = instance.argumentPresent(“FooBar”);
assertEquals(expResult2, result2);
}
/**
* Test of getValue method, of class CommandLine.
*/
@Test
public void testGetValue() {
System.out.println(“getValue”);
String arg1 = “Arg2”;
String expResult1 = “Value2”;
String result1 = instance.getValue(arg1);
assertEquals(expResult1, result1);
String arg2 = “Arg3”;
String expResult2 = “test=test=test”;
String result2 = instance.getValue(arg2);
assertEquals(expResult2, result2);
}
/**
* Test of size method, of class CommandLine.
*/
@Test
public void testGetNumberArgs() {
System.out.println(“getNumberArgs”);
int expResult = 3;
int result = instance.size();
assertEquals(expResult, result);
}
}
So the suite classes typically run the individual test classes or higher level integration testing. Then the suites or individual test classes are run in the main application, as demonstrated above. Also, if you are not doing this already I would try running a very basic test locally on your development platform. Let me know if this gets you any further! Thanks!!!
– Jonathan
June 18, 2013 at 5:26 pm #4549Ian McCoyParticipantJonathan,
Thanks for the help getting JUnit set-up that worked great.
Ian
-
AuthorPosts
- You must be logged in to reply to this topic.