2

working assertions example using standard Java SE with no imports:

thufir@dur:~/jshell$ 
thufir@dur:~/jshell$ javac App.java
thufir@dur:~/jshell$ 
thufir@dur:~/jshell$ java -ea App
Exception in thread "main" java.lang.AssertionError
    at App.main(App.java:12)
thufir@dur:~/jshell$ 
thufir@dur:~/jshell$ java App
Dec 21, 2018 1:56:23 AM App main
INFO: false
thufir@dur:~/jshell$ 
thufir@dur:~/jshell$ cat App.java 
import java.util.logging.Logger;


public class App {



    private static final Logger LOG = Logger.getLogger(App.class.getName());

    public static void main(String[] args) {
    boolean isFoo = false;
        assert isFoo;
        LOG.info(String.valueOf(isFoo));
    }
}

thufir@dur:~/jshell$ 

But, trying to get that to run from Gradle:

thufir@dur:~/NetBeansProjects/HelloAssertions$ 
thufir@dur:~/NetBeansProjects/HelloAssertions$ gradle clean

> Configure project :
e: /home/thufir/NetBeansProjects/HelloAssertions/build.gradle.kts:42:9: Unresolved reference: enableAssertions

FAILURE: Build failed with an exception.

* Where:
Build file '/home/thufir/NetBeansProjects/HelloAssertions/build.gradle.kts' line: 42

* What went wrong:
Script compilation error:

  Line 42:         enableAssertions = true
                   ^ Unresolved reference: enableAssertions

1 error

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s
thufir@dur:~/NetBeansProjects/HelloAssertions$ 

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 = "HelloAssertions.App"
}

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

run{
        enableAssertions = true
}

How else should I enable assertions?

1

You would need to specify the flag for the JUnit platform. Reference on the following answer: https://stackoverflow.com/a/48178018

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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