我们的应用程序中有一个基于 cron 的作业。
工作类别如下:
public class DailyUpdate implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
testMethod();
}
private void testMethod()
{
System.out.pritnln("Executed From scheduler");
}
}
我们应该如何编写单元测试用例来测试方法 测试方法()
我无法在没有调度程序的情况下直接调用 testMethod,因为它是私有(private)的。.有关如何为调度程序编写单元测试用例的任何建议
最佳答案
为了编写测试,您需要有预期的行为,因此测试不执行任何操作的方法是没有意义的。
现在解决您的主要问题。如果您有某种遗留应用程序,并且不允许更改方法的可见性,则测试私有(private)方法的最佳方法是使用 reflection 。
所以你可以使用以下模式
Method testMethod = DailyUpdate.getDeclaredMethod(testMethod, argClasses);
testMethod .setAccessible(true);
return testMethod.invoke(targetObject, argObjects);
另请参阅这个问题 how to test a class that has private methods fields or inner classes
关于java - java 中的单元测试调度程序作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35728587/