Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default Mocking a class for Testing

    If I have a class called Library with a method as shown below

    public String searchForBook(){
    String bookNumberToBeSearched = new InputAndOutputHandling().obtainStringFromKeyboard( "Book Number");
    .........
    .........
    }

    where


    class InputAndOutputHandling {

    public String obtainStringFromKeyboard(String anotherString) {
    .................
    .................
    }
    }

    If i am writing a test for Library class and the method shown above, how will i mock the IO handling class so that i can mock the keyboard entry.

    I tried doing this with mockito

    InputAndOutputHandling ioHandling = mock(InputAndOutputHandling.class);
    when(ioHandling.obtainStringFromKeyboard("Book Number")).thenReturn("1");


    But it isnt working.

    Can anyone help?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What do you mean by "It isn't" working? What testing/mocking libraries are you using?

  3. #3
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    im using mockito..

    by isnt working i mean, i dont think i am able to mock using the about piece of code.. when i am testing, its asking for the keyboard input.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Then you need to read the manuals that came with your mocking libraries. I, for one, don't use that one but if I wanted to know how it works I would check the manuals for it.

  5. #5
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    I did find the solution. The mocked class has to be passed into the class which you are testing. I had not done that.... Its fine now.......

Similar Threads

  1. XML to JSP Unit Testing
    By Abder-Rahman in forum Advanced Java
    Replies: 2
    Last Post: 02-15-2009, 12:35 AM
  2. testing program
    By chrisbremen in forum New To Java
    Replies: 6
    Last Post: 11-08-2008, 05:23 PM
  3. Testing JUnit PDE with Ant
    By keynan in forum Eclipse
    Replies: 0
    Last Post: 02-14-2008, 11:35 PM
  4. Mocking static methods of class
    By Kat in forum New To Java
    Replies: 3
    Last Post: 11-08-2007, 12:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •