我是第一次研究Power mockito。这是我的代码
@SuppressWarnings("unchecked")
public List<CurrencyBasedTotalCharges> getCurrencyBasedTotalChargesforCustomerSuperGroupforOperatingAccount(String customerSuperGroupId)
{
Session session =getSessionFromContext();
String sql = "SELECT ROUND(SUM(qst),2) ||'`'||ROUND(SUM(pst),2)||'`'||ROUND(SUM(gst),2)||'`'||ROUND(SUM(hst),2)||'`'||ROUND(SUM(converted_val),2)||'`'||currency " +
"FROM (" +
"SELECT CASE WHEN cr.oper_acc_curr is null or cr.oper_acc_curr='NA'" +
"THEN cr.charge_account_curr" +
"ELSE cr.oper_acc_curr END AS currency," +
"nvl ((select case when cr.DR_CR_FLAG='C' then -mcr.value else mcr.value end from mi_charge_record mcr where mcr.original_charge_record_id=cr.reference_id and substr(mcr.charge_code,3,length(mcr.charge_code))='GST'), 0" +
" ) AS gst," +
"nvl((select case when cr.DR_CR_FLAG='C' then -mcr.value else mcr.value end from mi_charge_record mcr where mcr.original_charge_record_id=cr.reference_id and substr(mcr.charge_code,3,length(mcr.charge_code))='PST'), 0" +
" ) AS pst," +
"nvl((select case when cr.DR_CR_FLAG='C' then -mcr.value else mcr.value end from mi_charge_record mcr where mcr.original_charge_record_id=cr.reference_id and substr(mcr.charge_code,3,length(mcr.charge_code))='QST'), 0" +
" ) AS qst," +
"nvl((select case when cr.DR_CR_FLAG='C' then -mcr.value else mcr.value end from mi_charge_record mcr where mcr.original_charge_record_id=cr.reference_id and substr(mcr.charge_code,3,length(mcr.charge_code))='HST'), 0" +
" ) AS hst," +
"CASE WHEN cr.DR_CR_FLAG='C'" +
" THEN (-cr.value) ELSE cr.value END AS converted_val," +
"cr.reference_id FROM mi_charge_record cr, mi_bl_billing_preference bp, account acc, customer c," +
"mii_customer_group cg"+
"WHERE acc.account_number=cr.account_number AND acc.customer_id=c.customer_id AND c.customer_group_id=cg.id AND cg.customer_super_group_id=:superGroupId AND" +
"to_date(cr.BILLED_DATE) BETWEEN to_date(bp.billing_cycle_start_date,'yyyymmdd') AND to_date(bp.billing_cycle_end_date,'yyyymmdd') AND CR.PRODUCT_SHORT_NAME<> 'TAX'" +
"AND CR.IS_COST <> 'Y'"+
") z" +
"GROUP BY currency having SUM(ROUND(converted_val,2)) != 0";
List<String> totalchargedetails=session.createSQLQuery(sql).setString("superGroupId", customerSuperGroupId).list();
LinkedHashMap<String, String> paramMap=new LinkedHashMap<>();
paramMap.put(CHARGE_LEVEL, CHARGE_BY_OPERATING_ACCOUNT);
paramMap.put(LINKED_ENTITY_GUID, customerSuperGroupId);
paramMap.put(STATEMENT_LEVEL, CUSTOMER_SUPER_GROUP_STATEMENT_LEVEL);
return getFinalCurrencyBasedTotalCharges(setCurrencyBasedTotalCharges(totalchargedetails),paramMap);
}
我正在尝试使用模拟测试上述功能,如下所示:
public void getCurrencyBasedTotalChargesforCustomerGroupforOperatingAccountTest() throws Exception
{
String customerGroupId = "CG-100001-01";
PowerMockito.doReturn(session).when(implStatementDAOImpl, "getSessionFromContext");
Mockito.when(query.setString(anyString(), anyString())).thenReturn(query);
Mockito.when(session.createQuery(anyString())).thenReturn(query);
Mockito.doReturn(entityObjectBuilder.currencyBasedTotalCharges()).when(query).list();
List<String> totalchargedetails = new ArrayList<String>();
List<CurrencyBasedTotalCharges> test = implStatementDAOImpl.getCurrencyBasedTotalChargesforCustomerGroupforOperatingAccount(customerGroupId);
//given( implStatementDAOImpl.getCurrencyBasedTotalChargesforCustomerGroupforOperatingAccount(customerGroupId).t, session)).willReturn(new ArrayList<CurrencyBasedTotalCharges>());
//Assert.assertFalse(test.isEmpty());
Mockito.verify(session).createQuery(anyString());
Mockito.verify(query).setString(anyString(), anyString());
Mockito.verify(query).list();
System.out.println(test);
}
我在session.createQuery
行上收到空指针异常。请指导我如何模拟和测试。
谢谢。
会话在哪里初始化?
您可以按如下方式进行初始化:
@Mock
private Session session;
@Before
public void setup(){
MockitoAnnotations.initMocks(this);
}
使用注释进行模拟/间谍时初始化模拟很重要
另一种选择是像这样初始化会话,例如直接在您的测试方法中:
Session session = Mockito.mock(Session.class);