Skip to content
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 (where relevant) don't actually make sure that an exception was thrown. #106

Open
SoniEx2 opened this issue Nov 11, 2021 · 2 comments · May be fixed by #107
Open

Tests (where relevant) don't actually make sure that an exception was thrown. #106

SoniEx2 opened this issue Nov 11, 2021 · 2 comments · May be fixed by #107

Comments

@SoniEx2
Copy link

SoniEx2 commented Nov 11, 2021

Tests like these:

@Test
public void readQuotedString_noOpen() throws Exception {
try {
new StringReader("hello world\"").readQuotedString();
} catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedStartOfQuote()));
assertThat(ex.getCursor(), is(0));
}
}

will actually pass if no exception is thrown.

@Pokechu22
Copy link

According to this random article I found, JUnit 5 has an assertThrows that returns the exception if a matching one is found (and fails otherwise), which is probably the ideal thing for this kind of test. JUnit 4 has the ExpectedException rule, but I don't think that it would be usable for this purpose.

The easiest approach, and one that already seems to be used by some of the existing tests, is to call fail() after the code that should throw the exception. That's easy enough that I'll look into making a PR for it.

Pokechu22 added a commit to Pokechu22/brigadier that referenced this issue Nov 12, 2021
Fixes Mojang#106.  Without this, the test could pass if no exception is thrown, in addition to the wanted result of a matching exception being thrown.
@Marcono1234
Copy link

JUnit 4 also has an assertThrows method since version 4.13, see https://github.com/junit-team/junit4/wiki/Exception-testing#using-assertthrows-method

Also Brigadier uses the old Maven artifact coordinates for JUnit: junit:junit-dep:4.11
It should switch (and update) to junit:junit:4.13.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants