Expected Exceptions Message fails to match multi-line messages
See original GitHub issueProviding 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:
- Created 10 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
EDIT: This is incorrect. See dimo414’s post below.
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 expectedExceptionsMessageRegExpEdited : I misread the comment, adding
(?s)
at the beginning of regex is way more cleaner.