0

I have a list of counters , and I would like to check that the value of counters is > 0 : have you any suggestion on my code ?

 ArrayList<Integer> list = new ArrayList<Integer>(); int count1=0; int count2=0; for (int i = 0; i <x; i++) { if (action1) { count1++; } if (action2) { count2++; } } list.add(count1); list.add(count2); 

I need to check that only count1 is != 0

 assertTrue(list>0); ??? 
2
  • 3
    What about assertTrue(list.size() > 0) or assertTrue(!list.isEmpty())? Commented Oct 14, 2011 at 7:54
  • 1
    Please have a look at the javadoc or code completion that every decent IDE provides. That'll help you getting to know the standard API better and not have to just ask questions like "how do I check the size of a list". Commented Oct 14, 2011 at 7:55

5 Answers 5

2

You forgot the size() call which returns the count of elements inside the list object:

 assertTrue(list.size() > 0) 

EDIT:

You loop through the list and check every value inside the list like this:

for(int y : list) { assertTrue(y > 0); } 
Sign up to request clarification or add additional context in comments.

1 Comment

I need to check each value of list is >0 and afterdoing aother action I need to check that value of count2 is equal to 0?
1

assetrts is java work only when you start the you java program by passing -ea to JVM argments. So asserts will not work in normal scenario (unless an untill jvm starts with -ea argument). asserts in java are only use for debug purposes. You shoud consider throwing IllegalStateException or some other appropriate runtime exception in this case.

Comments

0

Do you mean the list size is greater than 0 or the counters in the list are greater than 0?

For the former it would be:

list.size() > 0 

If it's the counters, you could just check the values of the counters you have added to the list.

1 Comment

no, I mean, the value of count1 and count2 are greatter than 0 , as list give me :[2,5]
0

Since you have just added two elements to the list there is no point checking that its size is >0.

If you want to check that all the elements in the list are >0 then you need to do it individually:

for (int i : list) { assertTrue(i > 0); } 

Or use Guava:

assertTrue(Collections2.filter(list, new Predicate() { public boolean apply(Integer val) { return val > 0; }).count() > 0); 

Comments

0

If you're comparing values with zero, you can use the following methods instead.

assertThat(value).isZero(); // value == 0 assertThat(value).isNotZero(); // value != 0 assertThat(value).isPositive(); // value > 0 assertThat(value).isNegative(); // value < 0 

This way, you will have more intuitive messages when the tests fail than assertTrue or assertFalse.

If you want to assert that only the first element in the list has a positive value, you can do the following.

assertThat(list.get(0)).isPositive(); 

If you want to assert all the elements in the list are positive, it will look like this.

list.forEach(count -> assertThat(count).isPositive()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.