question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to use Java assertions?

Using a Python3 kernel and the line:

assert(1==2)

an AssertionError is returned:

AssertionErrorTraceback (most recent call last)
<ipython-input-2-000000000000> in <module>()
----> 1 assert(1==2);

AssertionError:

Using the IJava kernel and the same line, there is no return value or exception displayed.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
SpencerParkcommented, Sep 12, 2018

Assertions in java are a bit different, there is the assert keyword but it is disabled by default. To enable it the -ea flag must be passed in the jvm arguments. In the IJava kernel this can be done by editing the kernel.json and adding the -ea flag to the argv list but this makes your notebooks less reproducible as users would require the same setup.

What I would recommend is use a library like junit assertions which are common in java unit testing and easily included with the maven magic.

Run a cell with the following to include all of the assert methods from junit to the static space in your notebook:

%maven junit:junit:4.12
import static org.junit.Assert.*;

Use the assertion methods like the following:

assertTrue("Assertion message", 1 == 1);
assertTrue(1 == 2);

which is the same as the following pure java code if -ea is enabled:

assert 1 == 1: "Assertion message";
assert 1 == 2;
1reaction
hkarlcommented, Oct 5, 2018

Thanks for following up! Current preliminary testing seems to indicate it works. It will get its main workout when we run >400 students and their homeworks through it in December 😃 - we will keep you posted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Assertion Definition & Meaning - Merriam-Webster
The meaning of ASSERTION is the act of asserting or something that is asserted. How to use assertion in a sentence.
Read more >
Assertion (software development) - Wikipedia
In computer programming, specifically when using the imperative programming paradigm, an assertion is a predicate connected to a point in the program, ...
Read more >
Assertions in Auditing - Overview, Importance, and Types
Assertions are claims that establish whether or not financial statements are true and fairly represented in the process of auditing.
Read more >
Programming With Assertions - Oracle Help Center
An assertion is a statement in the Java programming language that enables you to test your assumptions about your program. For example, if...
Read more >
ASSERTION | definition in the Cambridge English Dictionary
a statement that you strongly believe is true: I certainly don't agree with his assertion that men are better drivers than women.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found