Skip to content

The class org.junit.jupiter.api.AssertionUtils and its methods should be public #2967

Closed
@twwwt

Description

@twwwt

For our own purposes, we have implemented additional assertion methods (assert...(...)) in a test utility class. Motivated by reusing existing functionality, this class makes use of the static methods of org.junit.jupiter.api.AssertionUtils; mostly for throwing assertion errors and for formatting error messages. AssertionUtils and most of its methods, however, are package-protected. In order to make them visible to our class, we have put it in the same package org.junit.jupiter.api. Now that we are using Java's module system, we can no longer proceed this way since Java's module system does not allow to have the same package in more than one module. Therefore I would like to propose to make org.junit.jupiter.api.AssertionUtils public as well as all its methods that are currently package-protected.

Note that there are a couple of other utility classes in org.junit.platform.commons.util that are already public; though org.junit.jupiter.api.AssertionUtils is in junit-jupiter-api while org.junit.platform.commons.util is in junit-platform-commons. I'm not necessarily proposing to also relocate AssertionUtils into junit-platform-commons: But if that makes sense, it seems better. junit-platform-commons is already a compile dependency of junit-jupiter-api.

Deliverables

  • Pull request?

Activity

sormuras

sormuras commented on Jul 6, 2022

@sormuras
Member

Having and keeping internal utility classes/methods concealed was a design decision made on purpose. I don't think that will "make org.junit.jupiter.api.AssertionUtils public as well as all its methods that are currently package-"private.

As you noted, module org.junit.platform.commons exports its package org.junit.platform.commons.support with the following comment:

/**
 * Maintained common support APIs provided by the JUnit Platform.
 *
 * <p>The purpose of this package is to provide {@code TestEngine} and
 * {@code Extension} authors convenient access to a subset of internal utility
 * methods to assist with their implementation. This prevents re-inventing the
 * wheel and ensures that common tasks are handled in third-party engines and
 * extensions with the same semantics as within the JUnit Platform itself.
 */

Perhaps something like this might be doable for Jupiter's low-level assertion utilities as well? 🤔

marcphilipp

marcphilipp commented on Jul 6, 2022

@marcphilipp
Member

Motivated by reusing existing functionality, this class makes use of the static methods of org.junit.jupiter.api.AssertionUtils; mostly for throwing assertion errors and for formatting error messages.

Which of them are you using?

twwwt

twwwt commented on Jul 6, 2022

@twwwt
Author

Which of them are you using?

org.junit.jupiter.api.AssertionUtils.buildPrefix(String message)
org.junit.jupiter.api.AssertionUtils.fail(String message)
org.junit.jupiter.api.AssertionUtils.failNotEqual(Object expected, Object actual, String message)

failNotEqual has a rather large call tree.

jbee

jbee commented on Jul 6, 2022

@jbee
Contributor

👍

There is some logic in how values are formatted and printed which is inaccessible when building own assertions. To have the same logic applied it would be good to have some way to use these methods. But this could be done in a different way than exposing the utils. For example by exposing another method or exception that later is passing things through these utils.

changed the title [-]The class org.junit.jupiter.api.AssertionUtils and its method should be public[/-] [+]The class org.junit.jupiter.api.AssertionUtils and its methods should be public[/+] on Jul 6, 2022
self-assigned this
on Jul 8, 2022
marcphilipp

marcphilipp commented on Jul 8, 2022

@marcphilipp
Member

Team Decision: Explore adding an API to make failure message formatting publicly accessible.

marcphilipp

marcphilipp commented on Jul 10, 2022

@marcphilipp
Member

@twwwt @jbee Please see the API proposal in #2972. Feedback appreciated!

added this to the 5.9 GA milestone on Jul 10, 2022
twwwt

twwwt commented on Jul 11, 2022

@twwwt
Author

@marcphilipp Looks good to me.

Thanks for quickly adding this new class, and for integrating it already in the upcoming release!

marcphilipp

marcphilipp commented on Jul 11, 2022

@marcphilipp
Member

@twwwt Thanks for the feedback!

Reopening the issue since the corresponding PR is not yet merged.

added a commit that references this issue on Jul 17, 2022
e91e5dc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

    Development

    Participants

    @twwwt@marcphilipp@jbee@sormuras

    Issue actions

      The class org.junit.jupiter.api.AssertionUtils and its methods should be public · Issue #2967 · junit-team/junit-framework