Weird .parse() behavior

Hello everyone :slight_smile:

I just pushed a bunch of simple tests and my daily test result confuses me.
The test I’m talking about (just for example, there were some failing like that) is Prog2.tests.daily.BrokenImplTestTests::testSchemeSimple.

The error looks like follows:

Error Trace
java.lang.AssertionError: All failing test cases also failed on a correct
implementation: uri.tests.SimpleTests.testUserInfoSimple(
NullPointerException: Cannot invoke "uri.Uri.getUserInfo()" because the return value
of "uri.UriParser.parse()" is null

As I’ve understood this error message, my test couldn’t call .getUserInfo(), because .parse() returned unexpected null, thus, .parse().getUserInfo() could not be evaluted.
But now I’m confused, why this happens, and why this happens also for a given test?

The URI the URIParserFactory had to parse looks like follows:

In my understanding, this URI is valid and .parse() should return a valid URI instance, and .getUserInfo() should return ilove:penguins.

What do I get wrong?

Thanks in advance,

The URL you provided is parsed on a correct implementation.
In your test, you use another URL with invalid characters.

It might be the case that parse returns null which leads the test to fail with a NullPointerException compared to an AssertionError.
This indicates an error in your tests compared to a mistake in the tested function.
Therefore, a cleaner test construction would be to also ensure that the first step, the parsing, was done correctly by the implementation that is tested.

You might want to move tests into own classes for a better organization.

1 Like

So, for best practice, a test SHOULD be structured like follows?

 * A test example that tests something on a hostname
public void testNameHere() {
	UriParser parser = UriParserFactory.create("URL-HERE");

	Uri uri = parser.parse();

    Host host = uri.getHost();
	// assert something on host here....

Yes, that way (maybe with messages in the asserts), you can make sure that the bug at the correct location is identified.
Also, one might want to factor common code fragments out.

1 Like