V tomto tutoriále sa pomocou príkladov dozvieme o príkaze Java assert (Java assertions).
Tvrdenia v Jave pomáhajú detekovať chyby testovaním kódu, ktorý považujeme za pravdivý.
Tvrdenie sa robí pomocou assert
kľúčového slova.
Jeho syntax je:
assert condition;
Tu condition
je boolovský výraz, ktorý pri vykonaní programu predpokladáme ako pravdivý.
Povolenie tvrdení
Predvolene sú tvrdenia zakázané a ignorované za behu.
Na povolenie tvrdení používame:
java -ea:arguments
ALEBO
java -enableassertions:arguments
Ak sú tvrdenia povolené a sú splnené podmienky true
, program sa spustí normálne.
Ak sa však podmienka vyhodnotí, false
kým sú povolené tvrdenia, program JVM hodí znak an AssertionError
a program sa okamžite zastaví.
Príklad 1: Java tvrdenie
class Main ( public static void main(String args()) ( String() weekends = ("Friday", "Saturday", "Sunday"); assert weekends.length == 2; System.out.println("There are " + weekends.length + " weekends in a week"); ) )
Výkon
V týždni sú 3 víkendy
Získame vyššie uvedený výstup, pretože tento program nemá žiadne chyby kompilácie a v predvolenom nastavení sú tvrdenia zakázané.
Po povolení tvrdení dostaneme nasledujúci výstup:
Výnimka vo vlákne „main“ java.lang.AssertionError
Iná forma tvrdenia
assert condition : expression;
V tejto forme tvrdenia sa výraz odovzdá konštruktorovi AssertionError
objektu. Tento výraz má hodnotu, ktorá sa zobrazí ako podrobná správa o chybe, ak je splnená podmienka false
.
Podrobná správa sa používa na zachytenie a prenos informácií o zlyhaní tvrdenia, ktoré pomôžu pri ladení problému.
Príklad 2: Java tvrdenie s príkladom výrazu
class Main ( public static void main(String args()) ( String() weekends = ("Friday", "Saturday", "Sunday"); assert weekends.length==2 : "There are only 2 weekends in a week"; System.out.println("There are " + weekends.length + " weekends in a week"); ) )
Výkon
Výnimka vo vlákne „main“ java.lang.AssertionError: K dispozícii sú iba 2 víkendy v týždni
Ako vidíme z vyššie uvedeného príkladu, výraz sa odovzdá konštruktoru AssertionError
objektu. Ak je náš predpoklad false
a tvrdenia sú povolené, je vyvolaná výnimka s príslušnou správou.
Táto správa pomáha diagnostikovať a opravovať chyby, ktoré spôsobili zlyhanie tvrdenia.
Povolenie tvrdenia pre konkrétne triedy a balíčky
Pokiaľ neposkytneme argumentom prepínače príkazového riadku,
java -ea
Toto umožňuje tvrdenia vo všetkých triedach okrem systémových tried.
Môžeme tiež povoliť tvrdenie pre konkrétne triedy a balíčky pomocou argumentov. Argumenty, ktoré je možné poskytnúť týmto prepínačom príkazového riadku, sú:
Povoliť tvrdenie v názvoch tried
Aby sme umožnili tvrdenie pre všetky triedy nášho programu Main,
java -ea Main
Ak chcete povoliť iba jednu triedu,
java -ea:AnimalClass Main
Toto umožňuje uplatnenie iba AnimalClass
v Main
programe.
Povoliť tvrdenie v názvoch balíkov
Ak chcete povoliť tvrdenia iba pre balík com.animal
a jeho čiastkové balíčky,
java -ea:com.animal… Main
Povoliť tvrdenie v nepomenovaných balíkoch
Povoliť tvrdenie v nepomenovaných balíkoch (keď nepoužívame príkaz balíka) v aktuálnom pracovnom adresári.
java -ea:… Main
Povoliť tvrdenie v systémových triedach
Na umožnenie tvrdenia v systémových triedach používame iný prepínač príkazového riadku:
java -esa:arguments
ALEBO
java -enablesystemassertions:arguments
Argumenty, ktoré je možné poskytnúť k týmto prepínačom, sú rovnaké.
Zakázanie tvrdení
Na deaktiváciu tvrdení používame:
java -da arguments
ALEBO
java -disableassertions arguments
To disable assertion in system classes, we use:
java -dsa:arguments
OR
java -disablesystemassertions:arguments
The arguments that can be passed while disabling assertions are the same as while enabling them.
Advantages of Assertion
- Quick and efficient for detecting and correcting bugs.
- Assertion checks are done only during development and testing. They are automatically removed in the production code at runtime so that it won’t slow the execution of the program.
- It helps remove boilerplate code and make code more readable.
- Refactors and optimizes code with increased confidence that it functions correctly.
When to use Assertions
1. Unreachable codes
Unreachable codes are codes that do not execute when we try to run the program. Use assertions to make sure unreachable codes are actually unreachable.
Let’s take an example.
void unreachableCodeMethod() ( System.out.println("Reachable code"); return; // Unreachable code System.out.println("Unreachable code"); assert false; )
Let’s take another example of a switch statement without a default case.
switch (dayOfWeek) ( case "Sunday": System.out.println("It’s Sunday!"); break; case "Monday": System.out.println("It’s Monday!"); break; case "Tuesday": System.out.println("It’s Tuesday!"); break; case "Wednesday": System.out.println("It’s Wednesday!"); break; case "Thursday": System.out.println("It’s Thursday!"); break; case "Friday": System.out.println("It’s Friday!"); break; case "Saturday": System.out.println("It’s Saturday!"); break; )
The above switch statement indicates that the days of the week can be only one of the above 7 values. Having no default case means that the programmer believes that one of these cases will always be executed.
However, there might be some cases that have not yet been considered where the assumption is actually false.
This assumption should be checked using an assertion to make sure that the default switch case is not reached.
default: assert false: dayofWeek + " is invalid day";
If dayOfWeek has a value other than the valid days, an AssertionError
is thrown.
2. Documenting assumptions
To document their underlying assumptions, many programmers use comments. Let’s take an example.
if (i % 2 == 0) (… ) else ( // We know (i % 2 == 1)… )
Use assertions instead.
Comments can get out-of-date and out-of-sync as the program grows. However, we will be forced to update the assert
statements; otherwise, they might fail for valid conditions too.
if (i % 2 == 0) (… ) else ( assert i % 2 == 1 : i;… )
When not to use Assertions
1. Argument checking in public methods
Arguments in public methods may be provided by the user.
So, if an assertion is used to check these arguments, the conditions may fail and result in AssertionError
.
Instead of using assertions, let it result in the appropriate runtime exceptions and handle these exceptions.
2. To evaluate expressions that affect the program operation
Do not call methods or evaluate exceptions that can later affect the program operation in assertion conditions.
Let us take an example of a list weekdays which contains the names of all the days in a week.
ArrayList weekdays = new ArrayList(Arrays.asList("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" )); ArrayList weekends= new ArrayList(Arrays.asList("Sunday", "Saturday" )); assert weekdays.removeAll(weekends);
Here, we are trying to remove elements Saturday
and Sunday
from the ArrayList weekdays.
Ak je tvrdenie povolené, program funguje dobre. Ak sú však tvrdenia zakázané, prvky zo zoznamu sa neodstránia. Môže to mať za následok zlyhanie programu.
Namiesto toho priraďte výsledok k premennej a potom ju použite na uplatnenie.
ArrayList weekdays = new ArrayList(Arrays.asList("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" )); ArrayList weekends= new ArrayList(Arrays.asList("Sunday", "Saturday" )); boolean weekendsRemoved = weekdays.removeAll(weekends); assert weekendsRemoved;
Týmto spôsobom môžeme zabezpečiť, aby boli všetky víkendy odstránené z pracovných dní bez ohľadu na to, či je tvrdenie povolené alebo zakázané. Vo výsledku to nemá vplyv na fungovanie programu v budúcnosti.