当我测试(JUnit)此类时,我在调用getdata方法时得到NullPointerException
private static final Map<String, Function<String, String>> REFERENCE_MAP = new HashMap<>();
@Autowired
private static classA aobj;
@PostConstruct
public void initialize() {
REFERENCE_MAP.put("s1", (bucketName) -> {
return aobj.getSomething1(bucketName);
});
REFERENCE_MAP.put("s2", (bucketName) -> {
return aobj.getSomething2(bucketName);
});
REFERENCE_MAP.put("s3", (bucketName) -> {
return aobj.getSomething3(bucketName);
});
REFERENCE_MAP.put("s4", (bucketName) -> {
return aobj.getSomething4(bucketName);
});
}
public String getdata() {
return REFERENCE_MAP.get("s1").apply("bucketname1");
}
我该如何解决问题
最佳答案
在测试文件中,请在运行测试之前调用initialize方法
@RunWith(MockitoJUnitRunner.class)
public class testClass {
@InjectMocks
private mainClass mainclass;
@Mock
private ClassA obja;
@Before
public void init() {
mainclass.initialize();
}
}