unit testing private methods

Put your problem here if it does not fit any of the other categories.

unit testing private methods

Postby jonneymendoza » Fri Sep 04, 2009 10:34 am

Hi, is their a way to unit test a private method/class in android? i have my Junit tests setup in a separate android project with the main project included in the build/library path.

i can access public class's and methods but i can do so for any private ones. in C++ i heard you can use "stubs" written above a specific method/function and it will only be assigned as a public class/method when you run/call that class/method but i cant seem to find any examples of using "stubs" for android unit testing.

cheers in advance
jonneymendoza
Developer
Developer
 
Posts: 36
Joined: Fri Sep 04, 2009 10:30 am

Top

Postby jonneymendoza » Mon Sep 07, 2009 11:36 am

bump: any suggestions?
jonneymendoza
Developer
Developer
 
Posts: 36
Joined: Fri Sep 04, 2009 10:30 am

Postby jonneymendoza » Tue Sep 08, 2009 9:13 am

Hi, i am having two issues when doing Junit testing on my code. the first issue is i cant seem to unit test a private method or class and was wondering if its possible to do so.

the second issue is that i have setup a seperate android project for doing all the testing and have just added the main project into its build path so i can call methods from the main android project i am testing on. Whenever i call something from the main project ie Myclass myclass = new Myclass(); it fails saying that the Test Run failed with a java.lang.Verify error

i am simply creating a new class object from my main project into my AndroidTestCase class so im not quite sure what the issue is.

i am basicly having the exact same issue as someone in this link http://dtmilano.blogspot.com/2009/07/an ... _4442.html who has posted a comment about it

Here below is the original comment made by someone with the same issue i have.


hey diego, i'm trying to create a unit test by following your tutorial. I've arrived in which i created a test method for one of my class (called MyClass for example) which resides in my main package (say, com.package.main). And yes, I've put my main project in the test build path configuration.

And then, I tried to create the class in one of my test case in a usual manner of:

MyClass myClass = new MyClass()

And then I tried to run the test project and it always gives Test run failed messages. This happens eveytime I wanted to instantiate any class that resides in my main project. Can you help me on this? Thanks in advance :).



Anyways, thanks for your time in reading this and im hoping someone here can help me as i really want to create a seperate project with all my Testcase's and TestSuits instead of including them in the main project. I also do not want to make my private class and methods public just for Unit testing.

Thanks again and looking forward to some of your advice.
jonneymendoza
Developer
Developer
 
Posts: 36
Joined: Fri Sep 04, 2009 10:30 am

Postby wiederke » Tue Sep 08, 2009 12:17 pm

Hi,

I also read Diegos Blog and run my tests in a separate project and it runs fine. You should try with a simple example first to find your bug.

It is not simple to test private methods. You can do so using reflection, but I don't know if this is available on Android. Why don't you change your access modifier from private to package private (default). If you organize your tests in the same package structure as your classes to tests you can easily write tests on package private methods.


cu,

Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Postby jonneymendoza » Tue Sep 08, 2009 12:25 pm

i have tried basic Test cases. all i have written from my test case is something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4.  
  5. private ServiceManager mServiceManager;
  6.  
  7.  
  8.  
  9. public class ServiceManagerTest extends AndroidTestCase {
  10.  
  11.  
  12.  
  13. public void testStartServices() throws IOException {
  14.  
  15.         Log.d( LOG_TAG, "testStartServices" );
  16.  
  17.        
  18.  
  19.         mServiceManager = new ServiceManager();
  20.  
  21.        // mServiceManager.startServices();
  22.  
  23.         boolean c1 = true;
  24.  
  25.         boolean c2 = true;
  26.  
  27.         assertEquals(c1, c2);
  28.  
  29.  
  30.  
  31.     }
  32.  
  33.  
  34.  
  35. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


it fails with a verify error
jonneymendoza
Developer
Developer
 
Posts: 36
Joined: Fri Sep 04, 2009 10:30 am

Postby wiederke » Wed Sep 09, 2009 9:35 am

Hi,

I added a project with a separate test project. The test runs on my system. You should look at the AndroidManifest.xml of the ServiceManagementTest application.

I hope this helps.


bye
Attachments
ServiceManagement.zip
(55.97 KiB) Downloaded 130 times
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Top

Postby jimshowalter » Sun Oct 04, 2009 11:38 pm

For an example of how to reproduce this problem as well as fix it, see http://jimshowalter.blogspot.com/2009/1 ... tiple.html.
jimshowalter
Freshman
Freshman
 
Posts: 2
Joined: Sun Oct 04, 2009 11:36 pm
Location: Los Gatos, CA

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 15 guests