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.

Nicer way to support soft assertions (Expect.create junit4 Rule) in JUnit5?

See original GitHub issue

From a purely JUnit5 perspective, am I missing something?

I made this wrapper around the core Expect function:

    public class ThingyThing {
        public static void softly(final SoftAssertions softly) throws Throwable {
            Expect expect = Expect.create();
            expect.apply(new Statement() {
                @Override
                public void evaluate() throws Throwable {
                        softly.apply(expect);
                }
            }, Description.EMPTY)
                    .evaluate();
        }

        public interface SoftAssertions{
            void apply(final Expect expect);
        }
    }

Used like:

        ThingyThing.softly(expector -> {
            expector.that(1).isEqualTo(2);
            expector.that(-1).isGreaterThan(0);
        });

Outputs:

2 expectations failed:
  1. expected: 2
     but was : 1
     	at io.<snip>.ThingTest.lambda$testRemit$0(ThingTest.java:113)
     
  2. expected to be greater than: 0
     but was                    : -1
     	at io.<snip>.ThingTest.lambda$testRemit$0(ThingTest.java:114)

P.s. amazing framework BTW - beautiful. Especially for chained custom objects. Amazing. Wish I’d found it sooner.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ddaeyeonkimcommented, Oct 1, 2021

My solution Inspired by ThingyThing.

class ExpectExtension : BeforeEachCallback, AfterEachCallback, InvocationInterceptor,
                        ParameterResolver {

    private var _expect: Expect? = null

    val expect: Expect
        get() = _expect!!

    override fun beforeEach(context: ExtensionContext) {
        _expect = Expect.create()
    }

    override fun interceptTestMethod(
        invocation: InvocationInterceptor.Invocation<Void>,
        invocationContext: ReflectiveInvocationContext<Method>,
        extensionContext: ExtensionContext
    ) {
        _expect?.apply(
            object : Statement() {
                override fun evaluate() {
                    invocation.proceed()
                }
            },
            Description.EMPTY,
        )?.evaluate()
    }

    override fun afterEach(context: ExtensionContext) {
        _expect = null
    }

    override fun supportsParameter(
        parameterContext: ParameterContext,
        extensionContext: ExtensionContext
    ): Boolean {
        val paramType = parameterContext.parameter.type
        return paramType is Type && paramType == Expect::class.java
    }

    override fun resolveParameter(
        parameterContext: ParameterContext,
        extensionContext: ExtensionContext
    ): Any {
        return expect
    }
}

A.

@Test
@ExtendWith(ExpectExtension::class)
fun test(expect: Expect) {
    expect.that<Int>(1).isEqualTo(2)
    expect.that<Int>(1).isEqualTo(3)
    expect.that<Int>(1).isEqualTo(4)
}

B.

@JvmField
@RegisterExtension
val extension = ExpectExtension()

@Test
fun test2() {
    extension.expect.that<Int>(1).isEqualTo(2)
    extension.expect.that<Int>(1).isEqualTo(3)
    extension.expect.that<Int>(1).isEqualTo(4)
}

@Test
fun test3(expect: Expect) {
    expect.that<Int>(1).isEqualTo(2)
    expect.that<Int>(1).isEqualTo(3)
    expect.that<Int>(1).isEqualTo(4)
}

I look forward to any form of Expect Extension for JUnit5 being added in the future!

0reactions
cpovirkcommented, Oct 19, 2021

As belatedly noted on #894, anything that we label as P3 has no timeline for being reviewed 😦 We hope to eventually schedule some more time for Truth (especially for features that could work well in Kotlin, like this one), but there are no plans yet.

(Another thing we should do: Figure out if this feature request and https://github.com/google/truth/issues/266 represent different requests or if they could be merged.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Assertions in JUnit 4 and JUnit 5 - Baeldung
Assertions are utility methods to support asserting conditions in tests; these methods are accessible through the Assert class, in JUnit 4, ...
Read more >
Is there ErrorCollector rule analogue for JUnit5 - Stack Overflow
For now I see that the best way is to use assert-j like this @ExtendWith(SoftAssertionsExtension.class) public class ...
Read more >
Writing Assertions With JUnit 5 Assertion API - Petri Kainulainen
This blog post describes how you can write assertions by using the JUnit 5 assertion API.
Read more >
Migrating From JUnit 4 to JUnit 5: A Definitive Guide
Methods for asserting reside in the org.junit.jupiter.api.Assertions class instead of org.junit.Assert class. In most cases, we can just find ...
Read more >
JUnit5 Assertion Migration Strategy - EvilTester.com
The main differences between JUnit4 and JUnit5 seem to be: annotations, rules, and assertions. ... But assertions have the issue that in JUnit5...
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