【发布时间】:2020-11-21 22:10:33
【问题描述】:
所以我正在使用一个有效的 UI,并使用一个 DB2 数据库。我正在尝试在控制器/服务/dao 层上运行单元测试,并且正在使用 mockito 和 junit 进行测试。以下是每一层的部分:
Measures.java
@Controller
@RequestMapping(value = "/measures"})
public class Measures {
@Resource
private CheckUpService checkUpService;
public void setCheckUpService(CheckUpService checkUp) {
this.checkUpService = checkUpService;
}
@RequestMapping(value = "/eligibility/{userId}/{effDate}/{stageInd}", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody List<Model> findEligibility(@PathVariable int userId, @PathVariable String effDate, @PathVariable String stageInd) throws Exception
{
List<Model> result = new ArrayList<Model>();
if (stageInd.equals("stage"))
{
result = checkUpService.findEligibilityStage(userId, effDate);
}
if (stageInd.equals("prod"))
{
result = checkUpService.findEligibility(userId, effDate);
}
return result;
}
...
}
CheckUpService.java
public class CheckUpService {
@Resource
EligibilityDao eligDao;
public List<Model> findEligibility(int userId, String effDate) throws Exception
{
return eligDao.findEligibility(userId, effDate, db_table);
}
}
EligibilityDao.class
public class EligibilityDao {
public List<Model> findEligibility(int userId, String effDate, String table) throws Exception
{
// uses some long sql statement to get some information db2 database and
// jdbctemplate helps return that into a list.
}
}
这是我正在尝试做的控制器测试,我已经花了大约 10 个小时在这上面,我真的不明白为什么它给了我 406 错误而不是 200。
ControllerTest.java
@EnableWebMvc
@WebAppConfiguration
public class ControllerTest {
@Autowired
private Measures measures;
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
private List<Model> findEligibility() {
List<Model> list = new ArrayList<>();
Model test_model = new Model();
test_model.setUserId(99);
test_model.setCreateID("testUser");
test_model.setEffDate("2020-07-30");
list.add(test_model);
return list;
}
@Before
public void setup() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void test_find() throws Exception {
CheckUpService checkUpService = mock(CheckUpService.class);
when(checkUpService.findEligibility(99, "2020-07-30")).thenReturn(findEligibility());
measures.setCheckUpService(checkUpService);
String URI = "/measures/eligibility/99/2020-07-30/prod";
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(URI).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
MvcResult handle = mockMvc.perform(requestBuilder).andReturn();
// MvcResult handle = mockMvc.perform(requestBuilder).andExpect(status().isOk()).andReturn();
MvcResult result = mockMvc.perform(asyncDispatch(handle)).andExpect(status().isOk()).andReturn();
// assertThat(result.getResponse().getContentAsString()).isEqualTo(findEligibility());
}
}
MvcResult 结果是在 junit 中抛出“StatusExpected 但为 ”错误的原因,我对它的原因感到很生气。另一个问题是,如果你能看到,我用 .andExpect(status().isOk()) 注释掉了句柄,并且那个也抛出了同样的问题。是我设置的测试有问题还是什么?
【问题讨论】:
标签: java spring spring-boot spring-mvc junit