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

Assert check not consistent #1773

Closed
LeonteDenismsgDev opened this issue Apr 19, 2024 · 9 comments
Closed

Assert check not consistent #1773

LeonteDenismsgDev opened this issue Apr 19, 2024 · 9 comments

Comments

@LeonteDenismsgDev
Copy link

So I was writing some tests for some lists and I noticed that if an assert is inside a loop but the loop by some erroneous behavior wont start I noticed that the test passes and the runner gives no warning whatsoever that the test doesnt actually run any assert at all. Maybe this is just me being stupid but I think it could be helpful to at least get warned whenever a test doesnt run any assert.
For example:

@Test
public void test1(){
result = method();
helper(result);
}

public void helper(List<Object> result){
//wont run
for(int i = 0; i < result.size(); i++){
assert(condition);
}
//wont give any warning that it didnt run any assert
}

Please pay no mind to my example, its just a proof of concept and it doesnt actually reflect my exact situation but the problem is that.
Please be respectful for my naivety, im just a junior =))

@sormuras
Copy link
Member

Are you using the assert statement, as in https://docs.oracle.com/javase/specs/jls/se22/html/jls-14.html#jls-14.10 ?

@panchenko
Copy link
Contributor

@LeonteDenismsgDev about your example - you can add an additional assert that result is not empty.
Overall it would be more appropriate to discuss such topics in another place, e.g. https://stackoverflow.com

@sbrannen
Copy link
Member

Hi @LeonteDenismsgDev,

The following is our general policy on questions of this sort:

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. We prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.


Having said that, and since you're "just a junior", let me say the following.

JUnit does not perform static analysis of your compiled code and therefore has no way to know if your code performs an assertion.

The best you can do is to ensure that you perform the necessary assertions in your tests.

For the example you gave, you could assert the size of the result before passing it to the helper() method. Or you could assert the size of the result at the beginning of your helper() method.

Alternatively, I believe there are some IDEs and plugins for IDEs that check whether your tests perform actual assertions.

In light of the above, I am closing this issue.

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Apr 19, 2024
@marcphilipp
Copy link
Member

@LeonteDenismsgDev Instead of using Java's assert keyword which needs to be enabled via a JVM flag (java -ea ...), you should use the static methods in org.junit.Assert which are always evaluated:

import static org.junit.Assert.assertTrue;

@Test
public void test1(){
	result = method();
	helper(result);
}

public void helper(List<Object> result){
	for(int i = 0; i < result.size(); i++){
		assertTrue(condition);
	}
}

@sbrannen
Copy link
Member

If I understood @LeonteDenismsgDev correctly, the reason that assert was not invoked was that the size of the result list was 0.

@marcphilipp
Copy link
Member

In that case, I'd suggest adding another assertion that checks the list is not empty: assertFalse(result.isEmpty()).

@sbrannen
Copy link
Member

Yep, that's what I meant when I suggested this:

assert the size of the result at the beginning of your helper() method.

Though, admittedly I should have explicitly stated that one should assert the size is not 0 (that the list is not empty). 😉

@LeonteDenismsgDev
Copy link
Author

yeah thats about it,. Sorry for the absence, being the weekend and stuff. @sbrannen thanks for the explanation in the closing comment, now I understand why its not supposed to warn me. Just a clarification: I did say assert but in the code I used AsserEquals (at that time not knowing you can assertEquals arrays) and now I got rid of that loop so the problem is not there anymore. Welp thank you all

@sbrannen
Copy link
Member

@LeonteDenismsgDev, you're welcome.

And we're glad we were able to help you sort it out.

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

No branches or pull requests

5 participants