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(SimpleTests.java:173):
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:

https://ilove:penguins@penguinlovers.com/penguins/forthewin

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,
David

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

Note:
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.

Note:
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
 */
@Test
public void testNameHere() {
	UriParser parser = UriParserFactory.create("URL-HERE");
	assertNotNull(parser);

	Uri uri = parser.parse();
    assertNotNull(uri);

    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