public class Student {
private String name;
private int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
private String getGrade() { //private method
if (score >= 90) {
return "A";
} else if (score >= 80) {
return "B";
} else if (score >= 70) {
return "C";
} else if (score >= 50) {
return "D";
} else {
return "E";
}
}
}
Junit Test Code:
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals;
public class StudentTest {
@Test
public void testGetGrade() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Student student = new Student("Mike Kane", 90);
// Using reflection to access private method
Method getGradeMethod = Student.class.getDeclaredMethod("getGrade");
getGradeMethod.setAccessible(true);
// Accessing Private getGradeMethod method
String grade = (String) getGradeMethod.invoke(student);
// Asserting the result of the private method
assertEquals("A", grade);
}
}
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Resolve System.IO.PathTooLongException [Sharepoint C# .Net] - SharePoint
- Grep Alternative for Windows CMD or PowerShell: findstr - Windows
- What is macOS Ventura? - MacOS
- How to Stop Photos App from auto loading when device connected to the Mac - Mac-OS-X
- How to Change Mac Terminal Prompt - MacOS
- Cannot access files within Microsoft Teams - We are setting up your file directory - Teams
- [Android Studio] MainActivity does not exist - Android-Studio
- How to query database table names [MySQL/Oracle/SQL Server] - MySQL