Bazaprogram.ru

Новости из мира ПК
0 просмотров
Рейтинг статьи
1 звезда2 звезды3 звезды4 звезды5 звезд
Загрузка...

Java lang assertionerror

Java Exception Handling – AssertionError

Making our way through our detailed Java Exception Handling series we arrive at the AssertionError that we’ll be going over today. As with many other languages, the AssertionError in Java is thrown when an assert statement fails (i.e. the result is false).

Within today’s article we’ll explore the detailed of the AssertionError by first looking at where it sits in the larger Java Exception Hierarchy. We’ll also examine some fully functional Java code that will illustrate one example of a best practice for using assertions in Java applications, and how such code should lead to an unexpected AssertionError if something goes wrong. Let’s take a look!

The Technical Rundown

All Java errors implement the java.lang.Throwable interface, or are extended from another inherited class therein. The full exception hierarchy of this error is:

Full Code Sample

Below is the full code sample we’ll be using in this article. It can be copied and pasted if you’d like to play with the code yourself and see how everything works.

This code sample also uses the Logging utility class, the source of which can be found here on GitHub.

When Should You Use It?

To understand how AssertionErrors should be thrown within Java code we should first briefly review how assertions and the assert keyword are intended for use. The purpose of assertion is to test assumptions about the application’s logic. These assumptions might include pre-conditions, post-conditions, invariants, and the like. As with most other programming assertion features, the Java assert keyword expects a boolean expression that the code assumes will be/should be true. This is because, the moment an assert expression is false the Java Virtual Machine ( JVM ) will throw an AssertionError , which should typically halt execution of the application.

Before we go any further it’s worth noting that, by default, assertions are disabled for most JVMs. Thus, executing code with a failing assertion will completely ignore the failure and will not throw an AssertionError . This is typically not desired, since the purpose of using assert expressions at all is to properly test the assumptions throughout your code. To enable assertion checking you’ll need to add a command-line switch: -enableassertions or -ea . For example, if you’re executing code via the java command line tool, you’d need to issue something like the following command: $ java -ea . For most IDEs such as IntelliJ IDEA or Eclipse, you should look in the run configuration for JVM/VM command-line options, in which you can add the -ea flag.

There is a bit of discussion and debate in the Java development community about the exact and proper use of assertions, but the general consensus is that the appearance of an AssertionError should indicate a fundamentally broken application/code snippet. Consequently, assert statements should be used as a form of sanity checks as final “no-turning-back” statements that should never be reachable by proper code. In other words, it is common to use an assert statement that always produces a false value, yet in a location where execution of the assert statement should not be possible.

To illustrate this particular usage of assertions we’ve created a modified version of our Book class called BookWithStringPublicationType :

As the name indicates we’re using a simple String value to store the PublicationType field of each Book . We then perform a simple sanity check prior to setting the value within the setPublicationType(String type) method:

Читать еще:  Как восстановить жесткий диск с помощью программы

As you can see we perform a simple switch case test on the passed String type parameter. If it is one of the two valid values we set the value and explicitly break from the switch statement. However, any other value will reach the default case, which contains an always-false assert false expression, with an additional argument passed to it that will be used for the error message of the subsequent AssertionError instance. In this case, the goal of the code is to be completely certain that a publicationType field can never be set to something that is invalid. Attempting to do so will produce a failure via an AssertionError , which will require alteration by a developer to fix the bug.

To illustrate how this works in practice we have the testEnumPublicationType method:

We start by creating a valid BookWithStringPublicationType instance with a publicationType property of «PAPERBACK» . We then try to change to publicationType to «INVALID» before outputting the resulting object. To give us something to show when a failure occurs we’re explicitly catching any AssertionErrors that are thrown here, but in normal code we would not want to catch such errors, allowing them to instead crash the application.

Executing the testStringPublicationType() method produces the following output:

Everything works just as expected. Our original BookWithStringPublicationType instance as a «PAPERBACK» is instantiating just fine, but attempting to change it to «INVALID» throws an AssertionError , since execution within the BookWithStringPublicationType.setPublicationType(String type) method reached the final, default switch case and executing the assert false statement.

While this example illustrates one common way to use assertions in Java, we can also modify how the BookWithStringPublicationType class handles the publicationType to ensure it doesn’t rely on an assertion statement, while also ensuring that code cannot try to set invalid values for the field. We do this for the plain Book class by using the PublicationType enum :

Since only the publicationType field and its related getter/setter methods were modified to use the PublicationType enum we’ll only look at these changes in code:

As with enumerations in other languages, using one in Java allows us to maintain a collection of valid values for a particular data type. To illustrate this the testEnumPublicationType() method seen below creates an intial Book instance with the PublicationType.PAPERBACK value for publicationType , then attempts to invoke the book.setPublicationType(PublicationType.INVALID) method call to change it:

As it happens, since the JVM can identify the enumeration that is used here during compilation time, we can’t even execute this code — the compiler detects that PublicationType.INVALID is, well, not a valid symbol within the PublicationType enum, so it halts compilation and delivers an error message. This implementation serves the same purpose of the assert technique seen above, except it captures an issue at compilation/development time, rather than during runtime.

The Airbrake-Java library provides real-time error monitoring and automatic exception reporting for all your Java-based projects. Tight integration with Airbrake’s state of the art web dashboard ensures that Airbrake-Java gives you round-the-clock status updates on your application’s health and error rates. Airbrake-Java easily integrates with all the latest Java frameworks and platforms like Spring , Maven , log4j , Struts , Kotlin , Grails , Groovy , and many more. Plus, Airbrake-Java allows you to easily customize exception parameters and gives you full, configurable filter capabilities so you only gather the errors that matter most.

Читать еще:  Java sql drivermanager

Check out all the amazing features Airbrake-Java has to offer and see for yourself why so many of the world’s best engineering teams are using Airbrake to revolutionize their exception handling practices!

Using Java Assertions

Last modified: December 8, 2019

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

In the 9 years of running Baeldung, I’ve never, ever done a «sale».
But. we’ve also not been through anything like this pandemic either.
And, if making my courses more affordable for a while is going to help a company stay in business, or a developer land a new job, make rent or be able to provide for their family — then it’s well worth doing.
Effective immediately, all Baeldung courses are 33% off their normal prices!
You’ll find all three courses in the menu, above, or here.

1. Introduction

The Java assert keyword allows developers to quickly verify certain assumptions or state of a program.

In this article, we’ll take a look at how to use the Java assert keyword.

2. History of Java Assertions

The Java assert keyword was introduced in Java 1.4, so it’s been around for quite a while. However, it remains a little-known keyword that can drastically reduce boilerplate and make our code more readable.

For example, often times in our code we need to verify certain conditions that might prevent our application from working properly. Typically we’d write something like this:

Using assertions we can remove the if and throw statement with a single assert statement.

3. Enabling Java Assertions

Because Java assertions use the assert keyword, there are no libraries needed or packages to import.

Note that prior to Java 1.4 it was perfectly legal to use the word “assert” for naming variables, methods, etc. This potentially creates a naming clash when using an older code with newer JVM versions.

Therefore, for backward compatibility, the JVM disables assertion validation by default. They must be explicitly enabled using either the -enableassertions command line argument, or its shorthand -ea:

In this example, we’ve enabled assertions for all classes.

We can also enable assertions for specific packages and classes:

Here, we’ve enabled assertions for all the classes in the com.baeldung.assertion package.

Likewise, they can be disabled for specific packages and classes using the -disableassertions command line argument, or its shorthand -da. We can also use all four of these arguments together.

4. Using Java Assertions

To add assertions, simply use the assert keyword and give it a boolean condition:

Java also provides a second syntax for assertions that takes a string, which will be used to construct the AssertionError if one is thrown:

In both cases, the code is checking that a connection to an external resource returns a non-null value. If that value is null, the JVM will automatically throw an AssertionError.

In the second case, the exception will have the additional detail that will show up in the stack trace and can aid in debugging the problem.

Let’s have a look at the result of running our class with assertions enabled:

5. Handling an AssertionError

The class AssertionError extends Error, which itself extends Throwable. This means that AssertionError is an unchecked exception.

Читать еще:  Как восстановить удаленные файлы с почты яндекс

Therefore methods that use assertions are not required to declare them, and further calling code should not try and catch them.

AssertionErrors are meant to indicate unrecoverable conditions in an application, so never try to handle them or attempt recovery.

6. Best Practices

The most important thing to remember about assertions is that they can be disabled, so never assume they’ll be executed.

Therefore keep the followings things in mind when using assertions:

  • Always check for null values and empty Optionals where appropriate
  • Avo >

The Java assert keyword has been available for many years but remains a little-known feature of the language. It can help remove lots of boilerplate code, make the code more readable, and help identify bugs early in program development.

Just remember that assertions aren’t enabled by default, so never assume they will be executed when used in the code.

As always, the full source code is available over on GitHub.

Сайт тестировщика

Что такое Assert?

Assert помогает нам проверять условия теста и принимать решения когда тест провален или выполнен. Тест считается выполненным только если завершается без вызова какого-либо исключения.

В приведенном далее коде мы проверяем соответствие title страницы значению «Google». Если соответствие не выполняется, тест проваливается.

public void testCaseVerifyHomePage() <

driver = new FirefoxDriver();

Во-первых в этом коде мы создаем Firefox driver, затем переходим на страницу Google. И затем вызываем метод Assert.assertEquals().

Здесь можно сделать также другую запись: Assert.assertEquals(«Goooogle», driver.getTitle(), «Title not matching»);

TestNG поддерживает механизм проверок с помощью класса Assert, эти проверки играют важную роль при тестировании приложения.

А теперь давайте изменим заголовок страницы в коде и посмотрим на результат его выполнения:

public void testCaseVerifyHomePage() <

driver = new FirefoxDriver();

Этот код выдаст следующую ошибку проверки:

java.lang.AssertionError: expected [Google] but found [Gooooogle]

Вариантов проверок в TestNG существует много. далее будут перечислены самые популярные:

assertEqual(String actual, String excepted): принимает два аргумента типа String и проверяет их на эквивалентность. В случае различий в аргументах тест будет провален.

assertEqual(String actual, String excepted, String message): принимает три аргумента и проверяет первые два на равенство. Если они не равны тест будет провален и в консоль будет выдано сообщение из третьего аргумента.

assertEquals(boolean actual, boolean excepted): принимает два аргумента типа boolean и проверяет их на равенство. Тест провален если аргументы не равны.

assertEquals(java.util.Collection actual, java.util.Collection excepted, java.lang.String message): принимает два объекта коллекций и проверяет, что они содержат одни и те же элементы в одном и том же порядке. В случае отличий тест будет провален и в консоль будет выдано сообщение из третьего аргумента.

Assert.assertTrue(condition): принимает один аргумент boolean и проверяет что он имеет значение true. Если нет, генерируется AssertionError.

Assert.assertTrue(condition, message): принимает один аргумент boolean и один String. Проверяет на истинность condition и если оно не равно true, генерируется AssertionError и в консоль выдается сообщение message.

Assert.assertFalse(condition): принимает один аргумент boolean и проверяет что он имеет значение false. Если нет, генерируется AssertionError.

Assert.assertFalse(condition, message): принимает один аргумент boolean и один String. Проверяет на истинность condition и если оно не равно false, генерируется AssertionError и в консоль выдается сообщение message.

Ниже расположен пример кода с проверками:

public void testCaseVerifyHomePage() <

driver = new FirefoxDriver();

Assert.assertEquals(«Gooogle», driver.getTitle(), «Strings are not matching»);

//Напишите код для авторизации и методы isUserLoggedSuccessfully и isUserLoggedOut, возвращающие значения типа boolean.

Ссылка на основную публикацию
Adblock
detector