UNit test failed (java.lang.NullPointerException) when running with Gradle

Hi,

When i run unit tests for my gradle with bnd projects, it runs successfully in Eclipse. But when i run with gradle build command, then i face this issue:

fi.abc.ctc.chat.ChatClient.SetupTest > testReadLocalProperties FAILED
java.lang.NullPointerException at SetupTest.java:26

Here is the test:
@Test
public void testReadLocalProperties() throws Exception {
this.properties.load(Setup.class.getResourceAsStream(“ChatClient.properties”));
this.setup.readLocalProperties(this.properties);
assertNotNull(this.setup.getHostName());
assertNotNull(this.setup.getPortNumber());
assertNotNull(this.setup.getUseSSL());
if (this.setup.getHostName() == null) {
fail(“Hostname is null”);
}
}

So, the issue is it can not load the file: ChatClient.properties located under src/test/resources. Can you please help me what i should do to include src/test/resources during the testing.

Thank you so much!

Regards,
Viet

In bnd, the test folder is a singleton. So you can have only one folder for test, which in your case is likely target/test-classes.

To quickly fix this, place your src/test/resources files in the src/test/java folder, they will automatically be copied to the target/test-classes. Is more modular as well.

I expect you got a warning on your project? As I recall, bnd warns you about this mismatch between Eclipse & Gradle.

Hi,

Thank you so so much!

It works like a charm. And thank you for your explaination as well.

For warning, i got quite a lot since my own code has a lot of warinings already so I may miss that one.

Regards,
Viet

I am annoying most of my customers by insisting on zero (0!!) warnings. Once you have more than one warning, they become dead weight. This is the reason I created the green patch in the bndtools explorer when all projects are error- and warning free.

Imho, having warnings, any warning, is one of the dumbest not so smart things you can do. It is by far the cheapest way to prevent problems in runtime, where they are a lot harder to find. warnings almost always indicate a real problem. I consider ignoring warnings a special form of masochism :slight_smile:

Thank you so much for your sharing :). I will keep in mind this one. And for me, i agree with you that warning could indicate a real problem.