• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Whizlabs assert question

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ranchers, this is from the Whizlab simulator and doesn't add up. Probably something small I'm overseeing, but I don't get it:

Here's the code, pardon the layout, I know it isn't supposed to be like this:

public class AssertTest {

private Boolean isValid(int i) {
if(i<1 || i > 12 ) {
return new Boolean("false return"); // OK
}
else {
return new Boolean(true);
}
}

private void testValue( int val ) {
assert isValid(val) : "out of range value";
}

public static void main(String[] args) {
AssertTest test = new AssertTest();
test.testValue(22); // When I compile, no AssertionError is thrown???
}
}

How come there's no Assertion Error?

Thanks.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by jan avest:
...How come there's no Assertion Error? ...


When you run the program, you need to enable assertions with the -ea flag...

java -ea AssertTest

If you are using Java 1.4, then you also need to compile with the -source 1.4 flag...

javac -source 1.4 AssertTest.java

(Note: The AssertionError is thrown at runtime, not compile time.)
 
jan ter avest
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked, alright cheers. Ofcourse, assertions are runtime, thanks for reminding.
 
Nothing? Or something? Like this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic