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.

Expected Exceptions Message fails to match multi-line messages

See original GitHub issue

Providing an expectedExceptionsMessageRegExp value to a test should intuitively match any provided regex. However since Invoker.messageRegExpMatches() uses Pattern.matches() instead of Matcher.find() or Pattern.compile(re, Pattern.DOTALL) it is possible for a message to match or fail arbitrarily based on the existence of a newline.

Even if the user should be expected to explicitly drop the pattern into dotall mode, the current error message is unclear and confusing:

The exception was thrown with the wrong message: expected ".*Illegal.*" but got "Illegal State.
Two lines breaks the test."

Example test:

import org.testng.annotations.Test;

public class MessagePatternTest {
    @Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp=".*Illegal.*")
    public void singleLine() {
        throw new IllegalStateException("Illegal State.");
    }

    @Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp=".*Illegal.*")
    public void multiLine() {
        throw new IllegalStateException("Illegal State.\nTwo lines breaks the test.");
    }

    @Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp="(?s).*Illegal.*")
    public void multiLineDOTALL() {
        throw new IllegalStateException("Illegal State.\nTwo lines breaks the test.");
    }
}

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
nthexwncommented, Dec 6, 2013

Had a similar problem, found this on Google, solved my own problem, and came back to help. 😃

You’ve got the right idea with the (?s) modifier on . in your multiLineDOTALL test before “Illegal”. However, the linebreak in the exception you’re throwing occurs AFTER “Illegal”, so you need another (?s).* there as well:

expectedExceptionsMessageRegExp="(?s).*Illegal(?s).*"

EDIT: This is incorrect. See dimo414’s post below.

0reactions
yusong-shencommented, May 15, 2020

In case someone stucks at older version before fix but still want this to work (should rarely be the case at 2020 😂 ), here is a workaround : replace all the . to (\\S|\\s) in expectedExceptionsMessageRegExp

Edited : I misread the comment, adding (?s) at the beginning of regex is way more cleaner.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TestNG test fails with wrong message even when the ...
The exception was thrown with the wrong message: expected "'authority' Uri should have at least one segment in the path (i.e. https://<host>/< ...
Read more >
Manage multiline messages | Filebeat Reference [8.5] - Elastic
multiline.pattern​​ Specifies the regular expression pattern to match. Note that the regexp patterns supported by Filebeat differ somewhat from the patterns ...
Read more >
How to Handle the Unclosed String Literal Error in Java - Rollbar
Python unclosed string literal error refers to the Java compiler failing to interpret a string literal due to the missing of a double...
Read more >
ScalaTest 108: How to test expected exceptions with 'intercept'
Solution. Use the intercept method to verify the exception occurs. In the following example, the boom method will always throw an exception.
Read more >
ExUnit.DocTest – ExUnit v1.6.6 - HexDocs
At this moment, the exception parser would make the parser treat the next line as a start of a completely new expression (if...
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