Results 1 to 8 of 8
- 08-30-2010, 09:44 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 4
- Rep Power
- 0
- 08-30-2010, 10:29 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
Your method was too simple, you don't event need to test it. That's what I think.
Website: Learn Java by Examples
- 08-30-2010, 12:30 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 4
- Rep Power
- 0
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());
}
}
}
- 08-30-2010, 02:51 PM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
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 :)Website: Learn Java by Examples
- 08-30-2010, 03:10 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 4
- Rep Power
- 0
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; } }
- 08-30-2010, 03:27 PM #6
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
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()); } }Website: Learn Java by Examples
- 08-31-2010, 10:41 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
Now, there's loads of holes in that but it should give you the idea.Java Code:public class MockedPrintStream extends PrintStream { private String last message; public void println(String x) { lastMessage = x; } public String getLastMessage() { return lastMessage; } }
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).
- 09-01-2010, 07:47 AM #8
Member
- Join Date
- Aug 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
junit test problem
By moamen in forum EclipseReplies: 2Last Post: 03-14-2010, 09:41 PM -
JUnit Test??? What is it all about????? Please help!!
By nikosa in forum New To JavaReplies: 1Last Post: 08-03-2009, 05:31 PM -
JUnit Test Help!
By pharo in forum New To JavaReplies: 0Last Post: 04-10-2009, 05:15 PM -
Junit test
By alice in forum New To JavaReplies: 1Last Post: 06-14-2008, 01:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks