Spring单元测试私有方法
想要测试某类中的私有方法,必须通过反射。
反射获取私有方法有两种方式,一种是直接new对象,然后通过getClass;一种是extends要测试的类,然后this.getClass().getSuperclass()。对于不需要依赖spring容器的私有方法,用上述两种获取方式都可以。但是对于依赖bean的私有方法,必须采用extends的方式
原因如下:
new对象的方式不会将该类注入spring容器,故也不会获取到该类中通过spring容器注入的bean。反映在结果中就是该bean报空指针异常,而用第二种方式的话,由于@RunWith(SpringRunner.class)表示将当前测试类加入spring容器,故可将当前测试类及其超类注入bean。所以使用当前测试类的实例就可以获取到其超类的私有方法,并且通@SpringBootTest获取到项目中的其他bean
package com.test.impl;
import com.test.ServerApplication.class;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ServerApplication.class)
public class XXXTest extends XXXServiceImpl {
/**
* 测试私有方法示例及说明:
* 想要测试某类中的私有方法,必须通过反射。
* 反射获取私有方法有两种方式,一种是直接new对象,然后通过getClass;一种是extends要测试的类,然后this.getClass().getSuperclass()
* 对于不需要依赖spring容器的私有方法,用上述两种获取方式都可以。但是对于依赖bean的私有方法,必须采用extends的方式
* 原因如下:
* new对象的方式不会将该类注入spring容器,故也不会获取到该类中通过spring容器注入的bean。反应在结果中就是该bean报空指针异常
* 而用第二种方式的话,由于@RunWith(SpringRunner.class)表示将当前测试类加入spring容器,故可将当前测试类及其超类注入bean。
* 所以使用当前测试类的实例就可以获取到其超类的私有方法,并且通过@SpringBootTest获取到项目中的其他bean
*/
@Test
public void xxxTest() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method method = this.getClass().getSuperclass().getDeclaredMethod("xxx", List.class, Set.class);
method.setAccessible(true);
Object o = method.invoke(this, Collections.singletonList("1111"), new HashSet<>());
}
}