Issue
I am trying to write unit test cases for below class. Specially, for 'isSumOfTaskWeightIsValid()' method from the below. it has private member involved it. Could you please help writing test cases for that. I find it difficult because of the 'for loop' in that method where it loops over the 'mTasks'. Thanks in advance.
Class TaskCard {
private List<Integer> mTasks = new ArrayList<>();
private boolean mIsGood;
public TaskCard(boolean isGood) { mIsGood = isGood}
public void setUpListofTasks(DataBaseHelper db){
mTasks.addAll(db.getTasks());
}
public boolean isSumOfTaskWeightIsValid(){
int sum = 0;
for(int taskWeight : mTasks)
{ sum += taskWeight;
}
return (sum>0 || mIsGood);
}
}
Solution
You can use @Before
annotation to fill your mTasks
list and then you can call your isSumOfTaskWeightIsValid
method. You also need set your mIsGood
parameter in your constructor. Here is a sample test class.
private TaskCard taskCard;
@Before
public void initObjects() {
taskCard = new TaskCard(...); //True or False
//Initialize DataBaseHelper here
DataBaseHelper db = new DataBaseHelper();
taskCard.setUpListofTasks(db);
}
@Test
public void testIsSumOfTaskWeightIsValid() {
// Now your list is filled with the value you prove in @Before
assertTrue(taskCard.isSumOfTaskWeightIsValid());
}
Answered By - M. Oguz Ozcan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.