Results 1 to 8 of 8
  1. #1
    jRookie is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default How to test a void method in Junit?

    Hi,
    I have a Dog class and here one of its methods:

    public void makeSound(){
    System.out.println(mName + " says Roof!");
    }

    How do I make a test method for makeSound() in Junit?

    Thanks for your help!

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default

    Your method was too simple, you don't event need to test it. That's what I think.

  3. #3
    jRookie is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    Yes, you are right, but want to know the principle..

    Let's say that the method looks like this:

    public void allMammalsMeet(){
    for(int i=0; i<mMammals.size()-1; i++){
    for(int j=i+1;j<mMammals.size(); j++){
    System.out.println(mMammals.get(i).getName() + " meets " + mMammals.get(j).getName());
    }
    }
    }

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default

    Hi,

    Let say that in your class you have an ArrayList named mMammals. In the allMammalsMeet method for instance you remove some elements from the list. To test your method you can check if the size of the list is decrease correctly by the size removed from the list.

    One more thing, please format your code using the code tag :)

  5. #5
    jRookie is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    Hi,
    Thanks for your reply.
    I know how to make those kind of tests, but I still don't know how to test a void method that changes several variables of the object. For example when the method:
    - does a printout.
    - changes value of several variables.

    Java Code:
    	public void eat(Food food){
    		
    		mAmountOfEatenFood += food.eatFood(mNeededAmountOfFood - mAmountOfEatenFood);
    		if(mAmountOfEatenFood == mNeededAmountOfFood){
    			System.out.println(mName + " has eaten " + mAmountOfEatenFood + " and will not be hungry for a while..");
    			mAmountOfEatenFood = 0;
    			mIsHungry = false;	
    		}else{
    			System.out.println(mName + " has eaten " + mAmountOfEatenFood + " and is still hungry..");
    			System.out.println("Needs= " + mNeededAmountOfFood);
    			mIsHungry = true;
    		}
    	}

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default

    Well, to test a method you should understand what the method does to your object, how it change the state of your object. This state change that you need to test in your unit testing.

    Java Code:
    public class Something {
        private int size;
    
        public void setSize(int size) {
            this.size = size;
        }
    
        public int getSize() {
            return size;
        }    
    }
    
    public class SomethingTest {
        private Something instance;
    
        public void setUp() {
            instance = new Something();
        }
        public void testSize() {
            instance.setSize(10);
            Assert.assertEquals(10, instance.getSize());
        }
    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    Mock the output stream, using one of the mocking frameworks.
    Not great in this case since it's a class and not an interface, but there are ways.
    Failing that, simply mock it yourself.

    Java Code:
    public class MockedPrintStream extends PrintStream {
        private String last message;
        public void println(String x) {
            lastMessage = x;
        }
    
        public String getLastMessage() {
            return lastMessage;
        }
    }
    Now, there's loads of holes in that but it should give you the idea.

    As has been said, though, you don't normally check System.out.println. You'd be mocking persistence calls (ie db or file access or somesuch), which would be represented by some interface making mocking easier...and tie in properly with something like JUnit (which the above doesn't really).

  8. #8
    jRookie is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    Thank you guys!

    Now you have given me some ideas how make those kind of tests.

Similar Threads

  1. junit test problem
    By moamen in forum Eclipse
    Replies: 2
    Last Post: 03-14-2010, 09:41 PM
  2. Replies: 1
    Last Post: 08-03-2009, 05:31 PM
  3. JUnit Test Help!
    By pharo in forum New To Java
    Replies: 0
    Last Post: 04-10-2009, 05:15 PM
  4. Junit test
    By alice in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 01: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
  •