-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tests that depend on specific error messages are brittle. #24
Comments
Hello, Yes I agree with you, all the more so I used to explain such things to junior developers a long time ago, but... So 3 options for me here:
I'd go for option 3 and slightly change the exception message as follows (line 181 of YamlLintConfig): throw new YamlLintConfigException("invalid config (unexpected error): " + e.getMessage(), e); and have the following test: assertTrue(e.getMessage().startsWith("invalid config (unexpected error): ")); |
Ah, but there's a catch here. The build just specifies 1) that the source code is in Java 8, and 2) that the build will produce class files using Java 8 format. But what JVM is used to run the code is a different matter altogether! Here I think you're getting lucky and the GitHub tests (I assume you have that configured, but I haven't personally used it) uses the Java 8 runtime for running the tests. If GitHub (or another developer like me) decided to use Java 11 to run the tests (which we should be able to do!), it would break (as it did for me).
You're saying you would change the test to check only the part of the string you have complete control over. OK, that's better than nothing. I personally still think it's brittle and it's not what I would do, but the important thing is that you have taken the JVM out of the equation and stopped it from breaking on different systems. 👍 (I personally think that if the "flavor" is so important that it needs to be checked, it needs to be moved to something semantic like an exception flavor or a field, e.g. |
I launch Maven from IntelliJ that runs with its custom JDK 11, so that's odd. Travis uses the openjdk8 for the build, so no issue with it. Anyway, fixed but still in a brittle way... |
The test ran fine for me in Eclipse as well; when Eclipse imported the Maven project it automatically opted for the Java 8 I had configured as one of my Eclipse JREs. I installed it a long time ago; I guess it is the Oracle version:
But it broke when I ran the tests from Maven on the command line, because currently my default system Java is:
|
It's not a good idea to depend on specific error messages in an exception. For example in
ExtendedYamlLintConfigTest
:You want to check that the correct exception will be thrown, yes. But error messages can easily change, and shouldn't be part of the semantics of the error. They are just useful for a user. But you shouldn't depend on them.
The above example is failing for me on Java 11, because Java 11 seems to have changed the error message:
If you need to check for some specific error condition, you should add this condition as some property/attribute of the returned exception, or if it is a large category of things, make a new exception class or subclass. But checking for a specific error message makes the test brittle.
I'm going to have to do the changes for issue #20 "blind" to some extent (unless I want to switch to using a Java 8 JVM, which I shouldn't have to do, as Java 11 runs most Java 8 projects just fine).
The text was updated successfully, but these errors were encountered: