当我测试(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");
}
我该如何解决问题
答案 0 :(得分:1)
在测试文件中,在运行测试之前调用initialize方法
@RunWith(MockitoJUnitRunner.class)
public class testClass {
@InjectMocks
private mainClass mainclass;
@Mock
private ClassA obja;
@Before
public void init() {
mainclass.initialize();
}
}