0

I am a beginner in programming in Java. I am facing a problem in testing a private method against a unit testing tool. There are no errors when I compile and I am getting the expected results when I enter the values in the fields.

Here is the part of the code I wrote:

public class Title {
     private String getNewString() {
        if (newName == null || "".equals(newName)) {
            return newName;
        } else {
            return (newName.substring(0,1).toUpperCase() + newName.substring(1).toLowerCase());
        }
    }
}

The following is the testing tool which is giving me the error: <method> does not exist, or is spelled wrong

@Test
public void TestGetNewString() {

    //String parameter
    Class[] paramString = new Class[1]; 
    paramString[0] = String.class;

    try {
        //load the Title at runtime
        Class cls = Class.forName("Title");
        Object obj = cls.newInstance();

        //call the printItString method, pass a String param 
        Method method = cls.getDeclaredMethod("getNewString", paramString);
        method.setAccessible(true);
        String returnVal1 = (String) method.invoke(obj, new String("Peter"));
        String returnVal2 = (String) method.invoke(obj, new String("PETER"));
        String returnVal3 = (String) method.invoke(obj, new String("PeTEr"));
        int modifiers = method.getModifiers();

        assertEquals("getNewString does not format the name correctly. \n", "Peter", returnVal1);
        assertEquals("getNewString does not format the name correctly.  \n", "Peter", returnVal2);
        assertEquals("getNewString does not format the name correctly.  \n", "Peter", returnVal3);
        assertEquals("Method \"getNewString()\" is not marked private.  \n", true, Modifier.isPrivate(modifiers));
    } catch (NoSuchMethodException e) { // Field does not exist
        throw new AssertionError("Method \"getNewString()\" does not exist, or is spelled wrong.  \n");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Any suggestions as to where I am making a mistake?

Jagger
  • 10,350
  • 9
  • 51
  • 93
sguha
  • 1
  • 1
  • 5
    http://stackoverflow.com/questions/105007/should-i-test-private-methods-or-only-public-ones – byrnedo Oct 13 '14 at 18:16
  • 1
    If you need to test a method like this, make it default visible and annotate it with @VisibleForTesting or something instead of using reflection. – Louis Wasserman Oct 13 '14 at 18:17
  • @LouisWasserman Or create a separate class that contains the same functionality without the state (which is probably what you want to keep "private" in the first place). Using assertions and/or code coverage could also alleviate issues. – Maarten Bodewes Oct 13 '14 at 18:35

1 Answers1

1

Your method is calling:

 Method method = cls.getDeclaredMethod("getNewString", paramString);

and you define paramString as:

 Class[] paramString = new Class[1]; 

This would mean that it looks for a method signature that uses one parameter. getNewString however doesn't have any parameters.

Removing paramString should do the trick.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263