Reputation: 11
I try to test on how to get index 0 and 5 value from my file. I can get the value. But, to test the value with JUnit, i can only test if(index==0) but it did not check for (index==5).
So, how can i test both index in one time using if else ?
@Test
public void test(){
for(index = 0; index<arrayList.size(); index++){
if(index==0 && index==5){
given()
.header(something)
.get(something)
.then(something)
.body("try", contains(arrayList.get(index).getSomething(),arrayList.get(index).getSomething()))
.log().all()
.statusCode(HttpStatus.SC_OK)
}
}
}
Upvotes: 1
Views: 1466
Reputation: 140417
Simple:
for(index = 0; index<arrayList.size(); index++){
if(index==0 && index==5){
This says: if index is 0 and index is 5.
An int can't have two values at the same time. You probably want:
if(index==0 || index==5){
But of course: Timothy is correct, you want one "check" per test.
The proper solution would be: write a private helper method that takes an parameter indexToCheckFor
, so that you can do
private void testForIndex(int indexToCheckFor) {
....
if(index == indexToCheckFor) {
and then have two @Test methods, one calling testForIndex(0)
, the other one calling for 5.
Upvotes: 2
Reputation: 15622
In a unit test each test method verifies a single expectation about the tested units behavior. Therefore you write different tests for different input and/or different expectations.
Also tests should not have (self written) logic.
If you need to iterate over input values you might consider Parameterized tests: http://blog.schauderhaft.de/2012/12/16/writing-parameterized-tests-with-junit-rules/
Upvotes: 2