【发布时间】:2020-10-06 06:31:44
【问题描述】:
我正在尝试测试一个方法,但它有一个为空的全局变量,请指导我,以便我可以为全局变量赋值,即 Map
我的朱尼特:
public class ErrorTest {
@Mock
private DataSource db;
@Mock
private JdbcTemplate jdbcTemplate;
@InjectMocks
private RateServiceImpl rateService = new RateServiceImpl();
@Mock
private RaterDao raterDao;
@Resource
private MessageSource msg ;
@Mock
Map<String, StringAttribute> errorMap = new HashMap<String, StringAttribute>();
@Before
public void setup() throws IOException, InterruptedException {
MockitoAnnotations.initMocks(this);
MockMvcBuilders.standaloneSetup(rateService).build();
}
@Test
public void findAllErrors() throws Exception {
String error;
List<Error> erList = new ArrayList<>();
Error er27 = new ErrorImpl("27",
"No detail found",
"Please enter detail.");
erList.add(er27);
Error er22 = new ErrorImpl("1",
"Maximum number exceeded",
"Please contact Technical Support.");
erList.add(er22);
for (int index = 0; index < erList.size(); index++) {
StringAttribute st = new StringAttributeImpl();
st.setName(erList.get(index).getDescription());
st.setValue(erList.get(index).getResolution());
errorMap.put(erList.get(index).getCode(), st);
}
List<Error> errorList = raterDao.findAllErrors();
assertThat(errorList, is(notNullValue()));
StringAttribute map27 = errorMap.get("27");
Mockito.when(rateService.findRwxlClientError("27")).thenReturn(map27);
StringAttribute map22 = errorMap.get("22");
Mockito.when(rateService.findRwxlClientError("22")).thenReturn(map22);
assertTrue("ParseShipment failed", map27.getName().equals("No detail found"));
assertTrue("ParseShipment failed", map22.getName().equals("Please contact Technical Support."));
}
}
我的主要课程:
@Service
public class RateServiceImpl implements RateService {
protected final Log logger = LogFactory.getLog(getClass());
@Autowired
private RaterDao raterDao;
private Map<String, StringAttribute> errorMap = new HashMap<String, StringAttribute>();
@Resource
private MessageSource msg;
@PostConstruct
public void init() throws Exception {
**errorMap** = findAllClientErrors();
}
public Map<String, StringAttribute> findAllClientErrors() throws Exception {
List<Error> errorList = raterDao.findAllClientErrors();
for (int index = 0; index < errorList.size(); index++) {
StringAttribute st = new StringAttributeImpl();
st.setName(errorList.get(index).getDescription());
st.setValue(errorList.get(index).getResolution());
errorMap.put(errorList.get(index).getCode(), st);
}
return errorMap;
}
@Override
public StringAttribute findClientError(String code) throws Exception {
StringAttribute error = new StringAttributeImpl();
if (code.equals(Constants.ERROR_CODE_SETTING_UNAVAILABLE)) {
error.setName(msg.getMessage("SETTING.MESSAGE.ERROR", null,null));
error.setValue(msg.getMessage("SETTING.MESSAGE.RESOLUTION", null,null));
return error;
}
StringAttribute map = errorMap.get(code);
if (map == null || map.getName().isEmpty()) {
error.setName(msg.getMessage("DEFAULT.MESSAGE", new Object[] { code }, null));
error.setValue("");
} else {
error.setName(errorMap.get(code).getName());
error.setValue(errorMap.get(code).getValue());
}
return error;
}
}
我尝试了多种解决方案但都不起作用,有时地图会变空或为空。 任何通过我的测试用例的解决方案都有效。 我想测试 findClientError(String code) 问题出在 errorMap
【问题讨论】:
-
你的
RateServiceImpl类很奇怪。你有一个errorMap成员变量,它被初始化为一个空映射。然后,在finalAllClientErrors中,您修改地图并将其返回。当它返回时,您将覆盖errorMap成员变量。当findAllClientErrors返回时,您可以通过不覆盖errorMap来解决此问题。 (由于副作用,这仍然不是特别好的代码。更好的解决方案可能是在函数中创建并返回新地图并将结果附加到errorMap)
标签: junit hashmap mockito global-variables powermockito