-3

I want to fail if state does not equal to "Running" and status does not equal to "OK" I am using this but the code is going inside the if block even though the state is Running and status is ok

    if (!(state.equals("RUNNING")) && (status.equals(" OK")))
    {
Assert.fail();
}

I used this also

if ((state != ("RUNNING")) && (status !=(" OK")))

But this is also not working

joshua
  • 177
  • 1
  • 12
  • 1
    Off topic but is it really " OK" you want to use and not "OK"? – Joakim Danielson Jun 30 '20 at 07:32
  • "even though the state is Running **and** status is ok" - it can't be both at the same time. Or did you mean like `"RUNNING OK"` String? Then you should use `contains()` instead of `equals()`. – Amongalen Jun 30 '20 at 07:32
  • why do you use `!` and `equals(..)` in the first case but `!=` in the second? – jhamon Jun 30 '20 at 07:34
  • How can I make it fail then and I mean and condition it should not be running and should not be OK then it should fail – joshua Jun 30 '20 at 07:34
  • *"I want my scenario to fail if either state is not Running or status is not OK. It should fail if either of them does not suffice."* -> `state.equals("RUNNING") && status.equals("OK")` – akuzminykh Jun 30 '20 at 07:35
  • @akuzminykh you're missing some `!`s in your conditions – Federico klez Culloca Jun 30 '20 at 07:36
  • @akuzminykh I want not equals – joshua Jun 30 '20 at 07:37
  • Count the `(`and `)`. And see on which the `!` is applied – jhamon Jun 30 '20 at 07:37
  • @jhamon because i was not getting expected result from first so tried the second way – joshua Jun 30 '20 at 07:38
  • 2
    @joshua Take your first code example and make sure it fits the the first sentence in your question, you are halfway there. _state does not equal to "Running" and status does not equal to "OK"_ – Joakim Danielson Jun 30 '20 at 07:38
  • related: [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – jhamon Jun 30 '20 at 07:39
  • @jhamon is this fine if (!((getstate.equals("RUNNING")) && (gethealth.equals(" OK")))) – joshua Jun 30 '20 at 07:39
  • Does it look like what you tried to do on the second case? Did you tried that? Do you know boolean algebra? – jhamon Jun 30 '20 at 07:41
  • Yes I tried that And I am not able to figure out the way what operator to use for this and where to use. – joshua Jun 30 '20 at 07:45
  • @Joshua see my previous comment. – Joakim Danielson Jun 30 '20 at 07:48
  • @JoakimDanielson I tried but I am not getting where to put not equal to the operator while comparing. – joshua Jun 30 '20 at 07:50
  • @akuzminykh I understood at the beginning we have to use! and parenthesis should cover both the condition Will it be like this if(! state.equals("RUNNING") && status.equals("OK")) – joshua Jun 30 '20 at 08:00
  • @akuzminykh These two are working ~~~ if (!getstate.equals("RUNNING") && gethealth.equals(" OK")) if ((!getstate.equals("RUNNING")) && (!gethealth.equals(" OK"))) ~~~ – joshua Jun 30 '20 at 08:34

2 Answers2

4
  1. Double-check your parenthesis, the status check is not negated properly
  2. Remove the leading space in " OK"
  3. "...the state is Running and status is ok", then you might want to use equalsIgnoreCase instead of equals

So, all in all:

if (!state.equalsIgnoreCase("RUNNING") && !status.equalsIgnoreCase("OK")) 
{
    Assert.fail();
}

Furthermore, you cannot use != to compare String objects.

Lennart
  • 91
  • 3
2

Maybe you should step back and make a simple test case.

static public void test( String running, String status){
    return !"RUNNING".equals(running) && !" OK".equals(status);
}

Now have a main.

public static void main(String[] args){
    System.out.println( test( "fail", " OK") );
    System.out.println( test( "RUNNING", "fail) );
    System.out.println( test( "this one", "is true") );
}

If you like using extra parenthesis to be clearer then the condition can be changed to.

 ( !"RUNNING".equals(running) ) && ( !" OK".equals(status) );

I use the "String".equals form because it removes the chance of an NPE but it isn't necessary.

To use this criteria in an if statement.

if( ( !"RUNNING".equals(running) ) && ( !" OK".equals(status) ) ){
    //do something.
} else{
    //do something else.
    System.out.println(running + ", " + status);
}
matt
  • 10,892
  • 3
  • 22
  • 34
  • Can I not use if the condition for this comparison ? – joshua Jun 30 '20 at 07:47
  • @joshua yes you can, I just separated this to isolate the condition. I think you have the concept but you've muddled it a bit with the fact your conditions are true when the values are not equal. So I've set it up to make a little truth table. – matt Jun 30 '20 at 07:49
  • if ( !state.equals("RUNNING") ) && ( !status.equals("OK") ) will this work? – joshua Jun 30 '20 at 07:53
  • @joshua you're missing a parenthesis, so it is not correct. – matt Jun 30 '20 at 08:05
  • check this is this right if (!getstate.equals("RUNNING") && gethealth.equals(" OK")) – joshua Jun 30 '20 at 08:31
  • These both are fine right? if (!getstate.equals("RUNNING") && gethealth.equals(" OK")) if ((!getstate.equals("RUNNING")) && (!gethealth.equals(" OK"))) – joshua Jun 30 '20 at 08:33
  • 1
    @joshua why are you asking me if it's correct? You can use a compiler and test it out. The first one looks good, the second one looks the same with more parenthesis. Also naming a variable 'getState' is not good practice. The variable should probably be `state` and then you would have a method, `public String getState()` which would return the value of the variable. – matt Jun 30 '20 at 08:40
  • @joshua https://ideone.com/rCHjdq you can try the code here, and play around with the conditions. – matt Jun 30 '20 at 08:45