【问题标题】:how do i verify a method which is getting initiated inside the method?如何验证在方法内部启动的方法?
【发布时间】:2017-03-24 10:05:34
【问题描述】:

如何验证是否调用了printPreview.createShowReport();。我什至无法设置模拟对象,因为一旦我调用该方法,它就会实例化为 null 并创建新对象。

     public void previewOrDirectPrint(File file, boolean val) {
                try{
                    printPreview=null;
                    printPreview=new ExamReportPrintUI(this,file);

                }
                catch(Exception e){
                    e.printStackTrace();
                }
                if(!val)
                {
                    printPreview.createShowReport();
                }
                else
                {
                    printPreview.createHideReport();
                    printInitiate();
                    closeReportPreview();

                }

    here is my test case 
    @Test
        public void testpreviewOrDirectPrint()
        {
            File file=new File("/Desktop/config/reportTemplate.html");
            examReportManager.previewOrDirectPrint(file, false);
            assertNotNull(Whitebox.getInternalState(examReportManager, "printPreview"));

    }

Can anyone help me how to access the object or create mock and set to it. I can only verify if the object is mock.

我尝试使用Whitebox.setInternalstate("obj","",mockobject). 进行设置,但两个对象不同。

【问题讨论】:

    标签: java unit-testing junit mockito powermockito


    【解决方案1】:

    不使用 PowerMockito 的解决方案如下:

    1) 添加一个负责返回ExamReportPrintUI 实例的方法,并更改被测方法以通过这种方式获取该实例:

        public void previewOrDirectPrint(File file, boolean val) {
           try{
              printPreview=null;
              printPreview= getExamReportInstance(this,file);
    
           }
           ...
         }
    
         ExamReportPrintUI getExamReportInstance(ExamReportManager e, File f){
             return new ExamReportPrintUI(e,f);
         }
    

    2) 监视管理器并模拟 getExamReportInstance 方法以返回所需的模拟:

        @Test
        public void testpreviewOrDirectPrint()
        {
             ExamReportManager spyManager = Mockito.spy(examReportManager);
    
            doReturn(examReportPrintUIMock).when(spyManager).getExamReportInstance(Mockito.any(ExamReportManager.class), Mockito.any(File.class));
    
            File file=new File("/Desktop/config/reportTemplate.html");
            examReportManager.previewOrDirectPrint(file, false);
            assertNotNull(Whitebox.getInternalState(examReportManager,printPreview"));
    
        }
    

    现在您可以控制该类的实例,并且可以注入已配置的模拟。

    使用 PowerMockito

      @RunWith(PowerMockRunner.class)
      @PrepareForTest(ExamReportPrintUI.class)
      public ExamReportManagerTest{
    
        @Mock
        ExamReportPrintUI examReportPrintUIMock;
    
        @Test
        public void testpreviewOrDirectPrint()
        {
            PowerMockito.whenNew(examReportPrintUI.class)
             .withArguments(Mockito.any(ExamReportManager.class), Mockito.any(File.class))
             .thenReturn(examReportPrintUIMock);
    
            File file=new File("/Desktop/config/reportTemplate.html");
            examReportManager.previewOrDirectPrint(file, false);
            assertNotNull(Whitebox.getInternalState(examReportManager, "printPreview"));
    
        }
    
     }
    

    【讨论】:

    • 我无法更改实现
    • @Maciej Kowalski 。我不能改变实现
    • 我的测试案例对象数据=空; String[] srdata=new String[]{"xxx","yyy","bbb","hhh"}; PowerMockito.whenNew(ExamReportDialog.class) .withArguments(Mockito.any(int.class),Mockito.any(int.class),Mockito.any(Object.class)) .thenReturn(examReportDialog); ExamReportManager.showReportManager(data, srdata); assertNotNull(Whitebox.getInternalState(examReportManager,“_examReportDiaolg”)); PowerMockito.verifyPrivate(examReportDialog,Mockito.times(1)).invoke("初始化",Mockito.any(String[].class));这里没有发生
    • @Maciej Kowalski 如果我的公共方法只执行调用私有方法,除了验证私有方法之外,我还能在该方法中测试什么(在我的私有方法中,每个变量都是方法的本地变量,所以我什至不能访问任何东西)
    • 我没有更改实现的权限。除了验证私有方法之外,还有哪些测试的可能性?请给我一些想法
    猜你喜欢
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2013-12-05
    • 2019-04-22
    • 2014-04-29
    • 2019-04-01
    相关资源
    最近更新 更多