Heim  >  Artikel  >  Java  >  Java testet private Methoden

Java testet private Methoden

WBOY
WBOYOriginal
2024-08-30 16:23:08510Durchsuche

The java testing private methods are defined as the methods having private access modifier and are restricted to be accessed in the defining class only and are not visible in their child class due to which are not eligible for overridden, however, we can define a method with the same name in the child class and could access in parent class, also can test the private methods by making private methods package access which allows we to test them directly with JUnit from the test classes in the same package but the testing of private methods in SuiteRunner is easier than to test in JUnit.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

List of Four basic Approaches of Testing Private Methods

Whether you are using JUnit or SuiteRunner we have four basic approaches to testing private methods,

1. Do not test private methods (Indirect testing)

Sometimes the private method is doing a very complicated task and the test should be tested very well so we do not want that the users will have access to these methods for that the private methods need to be protected.
The private methods could not test indirectly but only their effect on the public methods that call them. Testing private methods may be an indication that those methods are moved into another class that assisting/supporting the reusability.

If we extract the private method from which are already working and has good unit test coverage then which already exists in a unit test that enough to test so we do not need to write more unit test for the private methods. But if we want to write the private method before its calling method and we want to write the unit tests before writing the private method.

Examples

public class testClass
{
private String testMethod(String s)
{
return s;
}
}

The above sample example is only for testing private methods. If we have a class ‘testClass’ then it is called by a method as ‘testMethod( )’, then that method is private and by passing string or object we can return the value.

2. Give the method package access

Giving method package access is works fine to testing private methods with JUnit but it is slightly costly. When a method having private access modifier then it is part of the implementation of the class. We can ignore the method if we just trying to use a class from another class in the package and we can figure out this by using the package access method. Also, the testing of non-public methods of classes can be done by using the method access package.
The first way to make the method package private with no access modifier and put tests into the same package, this is a common way but still, if we want another code then the second way is to make the method public.

3. Use a nested test class

The third thing about testing private methods in java is the use of nested test class, it can be done by nesting a static class inside the production class being tested. The nested class has access to the private member of its enclosing class, it would be able to invoke the private methods directly. The static class itself could be package access, allowing it to be a load part of the white box test.
The downside of this nested test class is that, if we don’t want the nested class being accessible in our department JAR file, then we need to do extra work to extract it. This may be problematic because quality analysts will make changes in source code.

4. Use reflection

Now come towards the next use of reflection. The advantage of using reflection to testing private methods is that it provides a clean separation of test code and production code. As we already see in the nested class approach that the tests need not be tested inside the class, in which the class is under test but they can be placed alongside the other tests that exercise the package level and public methods of the class. As we use nested class then we do not need to add any extra nested class at the package access level.

Reflection is an API that is used to examine or modify the behavior of the methods, classes, interfaces at run time. The disadvantage of the use of reflection is that the test code is far more tedious because it uses the reflection API.

Example: Java program to describe the use of reflection

Code:

import java.lang.reflect.Method;
import java.lang.reflect.Field;
class Test
{
private String s;
public Test()
{
s = "Java Testing Methods";
}
public void method1()
{
System.out.println("The string is " + s);
}
public void method2(int n)
{
System.out.println("The number is " + n);
}
private void method3()
{
System.out.println("Private method invoked");
}
}
class Reflect
{
public static void main(String args[]) throws Exception
{
Test obj = new Test();
Class cls = obj.getClass();
System.out.println("The name of class is " +
cls.getName());
System.out.println("The public methods of class are : ");
Method[] methods = cls.getMethods();
for (Method method:methods)
System.out.println(method.getName());
Method methodcall1 = cls.getDeclaredMethod("method2",
int.class);
methodcall1.invoke(obj, 19);
Field field = cls.getDeclaredField("s");
field.setAccessible(true);
field.set(obj, "JAVA");
Method methodcall2 = cls.getDeclaredMethod("method1");
methodcall2.invoke(obj);
Method methodcall3 = cls.getDeclaredMethod("method3");
methodcall3.setAccessible(true);
methodcall3.invoke(obj);
}
}

Output:

Java testet private Methoden

Im obigen Java-Programm müssen wir zuerst die java.lang-Pakete importieren und dann ein privates Feld in der Klasse erstellen, das auch vom Konstruktor erstellt wurde. Die Methode 1() wurde als öffentliche Methode ohne Argumente angenommen und dann wurde Methode 2() erstellt, ebenfalls eine öffentliche Methode, jedoch mit ganzzahligen Argumenten und einer privaten Methode. Methode 3 wurde widerrufen und die oben genannten drei Methoden wurden in der Reflect-Klasse aufgerufen. Der Ausgang ist oben angegeben Screenshot.

Fazit

In diesem Artikel kommen wir zu dem Schluss, dass wir die privaten Methoden nicht direkt testen sollten, sondern nur ihre Auswirkungen auf die öffentlichen Methoden, die sie aufrufen. Außerdem kommen wir zu dem Schluss, dass der Test nur auf die öffentliche Schnittstelle der Klasse zugreifen sollte. Wenn wir private Methoden testen, ordnen Sie den reflektierenden Code in der privaten statischen Klasse an.

Das obige ist der detaillierte Inhalt vonJava testet private Methoden. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Java-TesttoolsNächster Artikel:Java-Testtools