cancel
Showing results for 
Search instead for 
Did you mean: 

What is the correct use of IF ELSE statement with Groovy?

Frequent Contributor

What is the correct use of IF ELSE statement with Groovy?

Hi,

i want to know the correct use of if else statement in groovy.

I have a text file with the following content:

c='0'

in table x:

a=N

b=N

a and b are saved in project properties tha i can retrieve in my test.

 

Can someone confirm me if the following statement is correct:

try{
if( a=='N' || b=='N' ){
assert c == '0'
}else if ( a=='Y' || b=='Y' ){
assert c=='1'
}catch (AssertionError e){

...

Question:

if i put a==Y and b==Y in the first IF and put a==Y and b==Y

Will work correctly?

In my opinion it's not working, it asserts c==1 and we have a and b == N

 

5 REPLIES 5
Community Hero

Re: What is the correct use of IF ELSE statement with Groovy?

Not clear.

What you actually looking for?
You may look at documentation below
https://groovy-lang.org/semantics.html#_if_else


Regards,
Rao.
Frequent Contributor

Re: What is the correct use of IF ELSE statement with Groovy?

OK so see the follwing example:

It should fail for me but it doesn't

 

try{

def b = 14

def a =1

 

if(a == '2'){

assert a == b

}

}catch (AssertionError e)

....

Frequent Contributor

Re: What is the correct use of IF ELSE statement with Groovy?

ok i found my problem, it works without try catch block .

Community Hero

Re: What is the correct use of IF ELSE statement with Groovy?

should it not be a==2 instead?


Regards,
Rao.
Frequent Contributor

Re: What is the correct use of IF ELSE statement with Groovy?

Yes Rao.

But in my code there were more problems because some data were string and some not, so i first remove the try catch block then i convert my data correctly and i just use the if else and assert. it works well for what i was looking for. Sorry for all mistakes.

New Here?
Join us and watch the welcome video:
Watch the new Interview
Top Kudoed Authors