Hi,

I have written this piece of code:

public class testAssert { 
  2         public static void main(String[] args) { 
  3                 int x = 5; 
  4                 int y = 7; 
  5                 assert x != y; 
  6         } 
  7 }

I am not sure how to make use of this because there is no output whether I make check for "x == y" or "x != y". I was expecting something like that "failed/successful" or any other kind of output just as in JUnit. I am not sure what is wrong here. Isn't it supposed to throw assertionError or something like that?

Thakns.

Recommended Answers

All 3 Replies

public class testAssert { 
          public static void main(String[] args) { 
                  int x = 5; 
                  int y = 7; 

                  boolean b = x!=y;

                  System.out.println(b);
          } 
  }

With an assert, you're testing for any condition you want to be "true":

assert (1 < 2); // passes
assert (1 != 2); //passes
assert (1 > 2); // fails

The asserts are used when you run the jvm with the -ea parameter.
//enable assertions

java -ea myCode

any failed assertions will throw a:
java.lang.AssertionError

See also: http://bit.ly/2JMbc2

commented: It's good to learn something new +4

The asserts are used when you run the jvm with the -ea parameter.
//enable assertions

java -ea myCode

any failed assertions will throw a:
java.lang.AssertionError

See also: http://bit.ly/2JMbc2

That is the correct solution to this problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.