Testing private members using Junit

Testing private members using Junit

Post by Boby Georg » Thu, 07 Feb 2002 08:32:54



Hey all
 i am trying to figure out how to test private methods.
lets say main method inside a class mathie calls a private method pm_reader
which reads in the input from command line
how do i check whether the input is properly accepted or not. the skeleton
code is given below.

public class Mathie {

 private static int pm_reader(BufferedReader br) {
     iTemp = Integer.parseInt(br.readLine());
    return iTemp;

Quote:}

public void static main (String[] args) {
    int a = obj.pm_reader(br);

Quote:}
}

How do i check main or reader method? or is this style of coding non
testable?
Thanks in advance
 
 
 

Testing private members using Junit

Post by Ron Jeffrie » Thu, 07 Feb 2002 10:56:53




Quote:>How do i check main or reader method? or is this style of coding non
>testable?

If you want to test them, the simplest thing is to make them public.

Sometimes the private methods are tested well-enough by the public
method tests.

You get to decide ...

Ronald E Jeffries
http://www.XProgramming.com
http://www.objectmentor.com
I'm giving the best advice I have. You get to decide whether it's true for you.

 
 
 

Testing private members using Junit

Post by Kelly Morriso » Thu, 07 Feb 2002 17:08:53



Quote:>  i am trying to figure out how to test private methods.

An alternative is to make the methods package protected. You do this by
leaving the visibility qualifier off your methods. For example, your class
would look like this:

public class Mathie {

static int pm_reader(BufferedReader br) {
     iTemp = Integer.parseInt(br.readLine());
    return iTemp;

Quote:}

The "package protected" visibility lies somewhere between "protected" and
"private". A "package protected" method is visible only within classes
within that package. This means that you can write a test for the method as
long as you put the test inside the very same package. So, assuming the
above class is in "package com.java.mathie", your pm_reader method would be
visible to classes "com.java.mathie.Mathie" and
"com.java.mathie.TestMathie", but it wouldn't be visible to
"com.java.mathie.gui.MathieApp", or "com.java.mathie.lib.CommonRoutines", or
any other class outside of the "com.java.mathie" package. It's not quite as
good as "private" protection, but it's close.

   -----> kell

P.S. Need a great programmer in Atlanta? Hire me!!!

 
 
 

Testing private members using Junit

Post by Rolv Inge Seehu » Thu, 07 Feb 2002 19:00:25





>>  i am trying to figure out how to test private methods.

>An alternative is to make the methods package protected. You do this by
>leaving the visibility qualifier off your methods. For example, your class
>would look like this:

I disagree with that. I get this uggy gut feeling that this will lead
(organically) to a tighter coupling between my testcode and production
code. Which for me is an "Avoid at all cost" thing. (Once I really
shrudded when seeing a person sugesting that making C++ test classes a
"friend" of the testee.. *shrug* )

I rather prefer to have functionality I need to test public. When
doing OOP I often become over-eager to encapsulate as much as
possible... But too heavy encapsulation tends to make things... hard
to test.. John Lakos suggests[1] inserting 'probes' in classes to
makes them easy to test, methods that you wouldn't need unless you
were testing, and I find that aproach to work out fine. If having that
method public really is dangerous ( and when is that, when we have a
large testsuite, collective code ownership and programmers that think
for them selves anyway..), it can be encapsulated by other means (by
extracting an interface, creating a factory, etc.. But your code is
testable, so you've probably allready done that... ;-)

But that's only my o[pi]nion though...

reg.
Rolv

[1] John Lakos, Large-Scale C++ Software Design | Chapter: Design for
testability

 
 
 

Testing private members using Junit

Post by Robert C. Marti » Sat, 09 Feb 2002 03:33:08




Quote:>Hey all
> i am trying to figure out how to test private methods.
>lets say main method inside a class mathie calls a private method pm_reader
>which reads in the input from command line
>how do i check whether the input is properly accepted or not. the skeleton
>code is given below.

>public class Mathie {

> private static int pm_reader(BufferedReader br) {
>     iTemp = Integer.parseInt(br.readLine());
>    return iTemp;
>}

>public void static main (String[] args) {
>    int a = obj.pm_reader(br);
>}

>}

>How do i check main or reader method? or is this style of coding non
>testable?
>Thanks in advance

Here's one way that I use:

public class Mathie {

 /*private*/ public static int pm_reader(BufferedReader br) {
     iTemp = Integer.parseInt(br.readLine());
    return iTemp;

Quote:}

/*private*/ public void static main (String[] args) {
    int a = obj.pm_reader(br);

Quote:}
}

This makes it clear to the readers of my code that these functions are
private.

Unfortunately, this doesn't prevent well-meaning tools from listing my
/*private*/ methods in their dropdown lists.  You might try a prefix
like prv_, but that's pretty ugly.

Other folks I know have used reflection to get the method and call it.
I think this is overkill, but it works.

The most common approach I know of is to make the function 'protected'
and put the tests in the same package.

Robert C. Martin    | "Uncle Bob"              | Software Consultants

PO Box 5757         | Tel: (800) 338-6716      | your projects done.
565 Lakeview Pkwy   | Fax: (847) 573-1658      | www.objectmentor.com
Suite 135           |                          | www.XProgramming.com
Vernon Hills, IL,   | Training and Mentoring   | www.junit.org
60061               | OO, XP, Java, C++, Python|

"One of the great commandments of science is:
    'Mistrust arguments from authority.'" -- Carl Sagan

 
 
 

Testing private members using Junit

Post by Kelly Morriso » Sat, 09 Feb 2002 04:53:55





> > i am trying to figure out how to test private methods.
> The most common approach I know of is to make the function 'protected'
> and put the tests in the same package.

You could also use make the function "package protected." This is enough to
make it visible to a test function in the same package, but, like a private
method, it prevents the original class from being subclassed. It's probably
a little closer to what the original poster needed. I've always wondered why
Sun didn't provide a keyword for this level of protection...

   -----> kell

P.S. Need a great programmer in Atlanta? Hire me!

 
 
 

Testing private members using Junit

Post by Ilia Preus » Sat, 09 Feb 2002 16:24:21





> >Hey all
> > i am trying to figure out how to test private methods.
> >lets say main method inside a class mathie calls a private method pm_reader
> >which reads in the input from command line
> >how do i check whether the input is properly accepted or not. the skeleton
> >code is given below.

> >public class Mathie {

> > private static int pm_reader(BufferedReader br) {
> >     iTemp = Integer.parseInt(br.readLine());
> >    return iTemp;
> >}

> >public void static main (String[] args) {
> >    int a = obj.pm_reader(br);
> >}

> >}

> >How do i check main or reader method? or is this style of coding non
> >testable?
> >Thanks in advance

> Here's one way that I use:

> public class Mathie {

>  /*private*/ public static int pm_reader(BufferedReader br) {
>      iTemp = Integer.parseInt(br.readLine());
>     return iTemp;
> }

> /*private*/ public void static main (String[] args) {
>     int a = obj.pm_reader(br);
> }

> }

> This makes it clear to the readers of my code that these functions are
> private.

> Unfortunately, this doesn't prevent well-meaning tools from listing my
> /*private*/ methods in their dropdown lists.  You might try a prefix
> like prv_, but that's pretty ugly.

A workaround I sometimes use is a nested class (or inner class for
instance methods):

public class Mathie {
  public static class TestProbe {
    public static int pm_reader(BufferedReader br) {
      return Mathie.pm_reader(br);
    }
  }

  private static int pm_reader(BufferedReader br) {
      iTemp = Integer.parseInt(br.readLine());
     return iTemp;
 }

Quote:}

I see some advantages:
- it communicates intention very well, imho - even using "well-meaning
tools"
- it segregates the test-interface, so that it is easier to change (you
can even choose to not include the TestProbe in the distribution)
- it's an obvious code smell, so that I am remembered to think about
changing my design in a way to make testing private members obsolete

YMMV

Kind Regards, Ilja

--
++ Besuchen Sie unseren neuen Web-Auftritt mit aktuellen Informationen
++ ber unsere Produkte, Services und das Unternehmen:
++ http://www.disy.net


 disy Informationssysteme GmbH                  http://www.disy.net
 Stephanienstr. 30                            Tel: +49 721 1600-600
 D-76133 Karlsruhe, Germany                   Fax: +49 721 1600-605

++disy - wir optimieren Ihre Informations- und Kommunikationsprozesse++

 
 
 

1. Testing JNI-ridden Java code using JUnit!

The Problem:

  We have used JNI to give Java developers a chance to use our C++
messaging system.  However, these JNI methods are not available to us
at test-time, since we load our shared libraries dynamically.  When we
write JUnit tests, or even try to run the software outside of the
system, we get UnsatisfiedLinkErrors, because the JNI methods cannot
be found because they exist in shared libraries that have not been
loaded.

Is there any tricks, short of overhauling the system, to get around
this?
Perhaps maybe even a pre-processor directive like #define or #pragma
in C++?

Thanks.

AJ Mieskolainen

2. Frame Relay / PVC restriction per serial interface ??

3. testing main method using junit

4. Is there NVL in SQL?

5. Unit tests for JUnit (Was: Unit tests for GUI programming?)

6. Answer these VPN Q's for me??

7. C++ White Box Testing: Private member access?

8. OOPS with CJK 4.0.0

9. private member is not private ? why

10. Are private members really "private"?

11. Return of private types from private member functions

12. private member function returning a private type

13. Can I access private members of a class using its instance