首页 > 解决方案 > 使用 Mockito 测试用例失败

问题描述

@RunWith(MockitoJUnitRunner.class) //Class cannot be resolved to a type //@SpringBootTest public class MbankingApplicationTest {

    @Mock  CanmbTransactionDao dataServiceMock;
    @Mock  CanmbBaseDao baseDao;
    @InjectMocks  CanmbTransactionServiceImpl businessImpl;
    @Autowired  OminiController controller;
    Customer customer;
    @Test public void test() {
        Customer cust= new Customer();
        cust.setMbnumber("+919990176197");
        cust.setDeviceid("abcdef");
        UserProfileMaster profile = new UserProfileMaster();
        profile.setChkflag(22);
        profile.setStatus("ACTIVE");
        cust.setUserProfile(profile);
        cust.setMpin("123456");
        cust.setMpinsalt("12345");
        when(dataServiceMock.getUserMbAndDevice("+919990176197", "abcdef")).thenReturn(cust);
        this.customer = cust;
        assertEquals(cust, businessImpl.getUserMbAndDevice("+919990176197", "abcdef"));
    }

    @Test public void testMpin() {
        Customer cust = new Customer();
        cust.setMbnumber("+919990176197");
        cust.setDeviceid("abcdef");
        UserProfileMaster profile = new UserProfileMaster();    profile.setChkflag(22);
        profile.setStatus("ACTIVE");
        cust.setUserProfile(profile);
        cust.setMpin("d150cb2c64171a95eb3fa1bbf2ea786aef16b04d389a1ac67a52c75e95f61e66");
        cust.setMpinsalt("12345");

        when(dataServiceMock.getUserMbAndDevice("+919990176197", "abcdef")).thenReturn(cust);

        //assertEquals(cust, businessImpl.getUserMbAndDevice("+919990176197", "abcdef"));
        MBSOMNIIntegration reqData = new MBSOMNIIntegration();
        reqData.setMbnumber("+919990176197");
        reqData.setDeviceid("abcdef");
        reqData.setMpin("123456");

        OMNIIntegration omni=new OMNIIntegration();

        // businessImpl.validateOmniMpin(reqData, omni, "123");
        ResponseData data= new ResponseData();
        Map<String,String> ominiMap= new HashMap<>();
        ominiMap.put("Msg", "verified");        ominiMap.put("statusCode", "0");
        data.setStatusCode(0);
        data.setTid("");
        data.setData(ominiMap);
        when(businessImpl.validateOmniMpin(reqData, omni, "123")).thenReturn(data);
        assertEquals(data,businessImpl.validateOmniMpin(reqData, omni, "123"));
    }

测试运行:2,失败:1,错误:0,跳过:0,经过的时间:0.835 秒
<<< 失败!- 在 com.npst.mb.MbankingApplicationTest testMpin(com.npst.mb.MbankingApplicationTest) 中经过的时间:0.823 秒
<<< 失败!java.lang.AssertionError:预期:com.npst.mb.pojo.ResponseData 但在 org.junit.Assert.fail 的 com.npst.mb.pojo.ResponseData(Assert.java:89)在 org.junit.Assert .failNotEquals(Assert.java:835) at org.junit.Assert.assertEquals(Assert.java:120) at org.junit.Assert.assertEquals(Assert.java:146) at com.npst.mb.MbankingApplicationTest.testMpin( MbankingApplicationTest.java:93)

结果:

测试失败:MbankingApplicationTest.testMpin:93 预期:com.npst.mb.pojo.ResponseData 但为:com.npst.mb.pojo.ResponseData

测试运行:2,失败:1,错误:0,跳过:0

[信息] --------------------------------------------- ------------------------- [信息] 构建失败 [信息] ----------------- -------------------------------------------------- ----- [INFO] 总时间:12.493 s [INFO] 完成时间:2019-01-17T14:57:43+05:30 [INFO] 最终内存:36M/346M [INFO] ------ -------------------------------------------------- ---------------- [错误] 无法在项目 CapexMbankingPhase2 上执行目标 org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) : 有测试失败。[错误] [错误] 个别测试结果请参考 /home/npstx/raj/Canara Projects/MBS_APP_OMNI/target/surefire-reports。[ERROR] -> [Help 1] [ERROR] [ERROR] 要查看错误的完整堆栈跟踪,请使用 -e 开关重新运行 Maven。[错误] 使用 -X 开关重新运行 Maven 以启用完整的调试日志记录。http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

标签: junitmockito

解决方案


推荐阅读