我正在学习有关Spring Boot Application的Junit测试。我的帐户控制器方法取决于服务类方法。为此,我使用了Mockito
。我尝试简单,但是在这里我没有得到如何为以下方法编写测试用例?我如何使用模仿。
有人可以帮助我编写此测试用例吗?
AccountController
@RestController
@RequestMapping("/spacestudy/$ {InstituteIdentifier}/admin/account")
public class AccountController {
@Autowired
AccountService accService;
@GetMapping("/findAccountData")
public ResponseEntity<List<Tuple>> populateGridViews(@RequestParam(value="sClientAcctId",required=false) String sClientAcctId,
@RequestParam(value="sAcctDesc",required=false) String sAcctDesc,
@RequestParam(value="sInvestigatorName",required=false)String sInvestigatorName,
@RequestParam(value="sClientDeptId",required=false) String sClientDeptId) throws Exception {
return ResponseEntity.ok(accService.populateGridViews(sClientAcctId, sAcctDesc,sInvestigatorName,sClientDeptId));
}
}
AccountService
public List<Tuple> populateGridViews(String sClientAcctId, String sAcctDesc, String sInvestigatorName,
String sClientDeptId)throws Exception{
QAccount account = QAccount.account;
QDepartment department = QDepartment.department;
QAccountCPCMapping accountCPCMapping = QAccountCPCMapping.accountCPCMapping;
QInvestigator investigator = QInvestigator.investigator;
JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
query.select(Projections.bean(Account.class, account.sClientAcctId, account.sAcctDesc, account.sLocation,
Projections.bean(Department.class, department.sDeptName, department.sClientDeptId).as("department"),
Projections.bean(Investigator.class, investigator.sInvestigatorName).as("investigator"),
Projections.bean(AccountCPCMapping.class, accountCPCMapping.sCCPCode).as("accountCPC"))).from(account)
.innerJoin(account.department, department).innerJoin(account.accountCPC, accountCPCMapping)
.innerJoin(account.investigator, investigator);
if (StringUtils.isNotEmpty(sClientAcctId)) {
query.where(account.sClientAcctId.equalsIgnoreCase(sClientAcctId));
}
// code.......
return query.fetch();
}
AccountControllerTest
@RunWith(SpringRunner.class)
public class TestAccountController {
private MockMvc mockMvc;
@Mock
private AccountService accountService;
@InjectMocks
private AccountController accountController;
@Before
public void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(accountController).build();
}
@Test
public void populateGridViewsTest() throws Exception {
//????
//????
}
}
答案 0 :(得分:0)
会是这样的:
Debug.Print objIE.document.querySelectorAll("table.keyvalue-table td").Item(1).innerText
因此,基本上,您是用模拟代替服务并说它应该返回什么。在您的特殊情况下,我看不到对此类进行单元测试的任何理由,因为它内部没有任何逻辑。但是,如果您会遇到以下情况:
@RunWith(SpringRunner.class)
public class TestAccountController {
private MockMvc mockMvc;
@Mock
private AccountService accountService;
@InjectMocks
private AccountController accountController;
@Before
public void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(accountController).build();
}
@Test
public void populateGridViewsTest() throws Exception {
when(accountService.populateGridViews("foo","bar")).thenReturn(Arrays.asList(new Tuple("bazz"));
mockMvc.perform(get("/spacestudy/STACKOVERFLOW/admin/account/foo/bar"))
.andExpect(status().isOk())
.andExpect(jsonPath("someField").value("bazz"));
}
}
然后测试此类,例如,更有意义
第一次测试-模拟 @GetMapping("/findAccountData")
public ResponseEntity<List<Tuple>> populateGridViews(...) throws Exception {
List<Tuple> result = accService.populateGridViews(...);
if(result==null){
return ResponseEntity.notFound();
}
return ResponseEntity.ok(result);
}
以返回accService
并验证响应状态为404
第二次测试-模拟您的null
以不返回accService
并确认响应状态为200