2

The simplest possible assert usage I could think of:

thufir@dur:~/NetBeansProjects/helloAssertions/src/main/java$ 
thufir@dur:~/NetBeansProjects/helloAssertions/src/main/java$ ls
App.java
thufir@dur:~/NetBeansProjects/helloAssertions/src/main/java$ 
thufir@dur:~/NetBeansProjects/helloAssertions/src/main/java$ cat App.java 



public class App {

    public String getGreeting() {
        boolean foo = false;
        assert foo;
        return "Hello world.";
    }

    public static void main(String[] args) {
        System.out.println(new App().getGreeting());
    }
}

thufir@dur:~/NetBeansProjects/helloAssertions/src/main/java$ 
thufir@dur:~/NetBeansProjects/helloAssertions/src/main/java$ javac App.java
thufir@dur:~/NetBeansProjects/helloAssertions/src/main/java$ 
thufir@dur:~/NetBeansProjects/helloAssertions/src/main/java$ ls
App.class  App.java
thufir@dur:~/NetBeansProjects/helloAssertions/src/main/java$ 
thufir@dur:~/NetBeansProjects/helloAssertions/src/main/java$ java App
Hello world.
thufir@dur:~/NetBeansProjects/helloAssertions/src/main/java$ 
thufir@dur:~/NetBeansProjects/helloAssertions/src/main/java$ java -ea App
Exception in thread "main" java.lang.AssertionError
    at App.getGreeting(App.java:10)
    at App.main(App.java:15)
thufir@dur:~/NetBeansProjects/helloAssertions/src/main/java$ 

Which is fine, so far as it goes. How would I modify the build file:

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/5.0/userguide/tutorial_java_projects.html
 */

plugins {
    // Apply the java plugin to add support for Java
    java

    // Apply the application plugin to add support for building an application
    application
}

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is found on compile classpath of this component and consumers.
    implementation("com.google.guava:guava:26.0-jre")

    // Use TestNG framework, also requires calling test.useTestNG() below
    testImplementation("org.testng:testng:6.14.3")
}

application {
    // Define the main class for the application
    mainClassName = "App"
}

val test by tasks.getting(Test::class) {
    // Use TestNG for unit tests
    useTestNG()
}

so that it will run with assertions enabled?

1 Answer 1

1

You must enable assertions via the -ea flag.

application {
    // Define the main class for the application
    mainClassName = "App"
    // enable assertions
    applicationDefaultJvmArgs = ['-ea']
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.