2

How do I verify whether printPreview.createShowReport(); is called. I can't even set the mock object because as soon as I call the method it instantiates to null and creates new object.

     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.

I tried setting using Whitebox.setInternalstate("obj","",mockobject). but both objects are different.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
bhavya
  • 99
  • 8

1 Answers1

2

A solution for it, without using PowerMockito would be following:

1) Add a method which would be responsible for return an instance of ExamReportPrintUI and change the method under test to get that instance this way:

    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) Spy the manager and mock the getExamReportInstance method to return a desired mock:

    @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"));

    }

Now you can control what will be instance of that class and you can inject a configured mock.

With 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
  • 25,605
  • 12
  • 54
  • 63
  • i cant change the implementation – bhavya Mar 24 '17 at 10:25
  • @ Maciej Kowalski . i cant change the implementation – bhavya Mar 24 '17 at 10:28
  • @ Maciej Kowalski as i call a method it will create new instance with that new instance my methods will be invoked.... pls can yu tel me how powermockito is helping out – bhavya Mar 24 '17 at 11:28
  • not sure what you mean exactly.. you get any errors? some assertions are not working for you? – Maciej Kowalski Mar 24 '17 at 11:39
  • @ Maciej Kowalski because i have one more method like _examReportDiaolg=new ExamReportDialog(230, 650, data); same i need to create a instance but i facing problems with prameters to set (data is array of strings) – bhavya Mar 24 '17 at 11:42
  • when you call new ExamReportPrintUI(this,file);.. mockito will pick that up and replace that new instance with its own Mock instance. – Maciej Kowalski Mar 24 '17 at 11:43
  • try any(String[].class) – Maciej Kowalski Mar 24 '17 at 11:47
  • public void showReportManager(Object data, String[] srData ) { if(_examReportDiaolg==null){ _examReportDiaolg=new ExamReportDialog(230, 650, data); _examReportDiaolg.initialize( srData); _examReportDiaolg.setManagerInstance(this); } – bhavya Mar 24 '17 at 11:58
  • 1
    mytest case Object data=null; 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("initialize",Mockito.any(String[].class)); here its not happening – bhavya Mar 24 '17 at 11:59
  • do not verify calling of private methods.. thats a very bad practice – Maciej Kowalski Mar 24 '17 at 12:15
  • 1
    @ Maciej Kowalski if my public method is performing only calling private methods, what else i can test in that method other than verifying private methods(in my private method every variable is local to method so i cant even access anything) – bhavya Mar 25 '17 at 11:58
  • Your design is a bit off in that case.. you have probably put too much logic in a single class. You have to move some of the logic from the private methods to some new specialized classes having one or two public methods. Then you test those public methods. – Maciej Kowalski Mar 25 '17 at 12:54
  • 1
    i dont ve permission to change the implementation. what all are the possibilities to test, other than verifying private methods? pls give me ideas – bhavya Mar 27 '17 at 04:54
  • Well in that case you are left with verifying private method calls with powermock along with my solution.. http://stackoverflow.com/questions/16515809/powermock-verify-private-static-method-call-in-non-static-method. Btw if someone told you to test a method like that and not allow your to do some refactoring then its like trying to to go full speed but not being able to let go of the breaks.. – Maciej Kowalski Mar 27 '17 at 10:23
  • public void showReportManager(Object data, String[] srData ) { if(_examReportDiaolg==null){ _examReportDiaolg=new ExamReportDialog(230, 650, data); _examReportDiaolg.initialize( srData); _examReportDiaolg.setManagerInstance(this); } if i use mock ExamReportDiaolg then my if condition will fail so i will be not able to verify ... any suggestions for this method ?? – bhavya Mar 27 '17 at 11:48