Unit testing with Liferay 6.2

1.4k views Asked by At

I am using liferay-plugins-sdk-6.2, I have created a table "patients" and corresponding model and services using the Service Builder. Now I am trying to create a JUnit test for my logic. I've looked a lot of forum threads regarding this, so for 6.2 I've created the following structure: (btw: I am using ant)

-docroot
-test
  -unit
    -src
  -integration
    -src

And this is the part that gives me trouble...

@Test
public void testValidatePatient() throws SystemException {
    Patient patient = new PatientImpl();
    ...
}

When I run the test (from ant test), I get the following exception:

java.lang.ExceptionInInitializerError 
    PatientModelImpl.<clinit>(PatientModelImpl.java:92)
    ValidatorUtilTest.testValidatePatient(ValidatorUtilTest.java:29)
Caused by: java.lang.NullPointerException
    at com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil.getConfiguration(ConfigurationFactoryUtil.java:27)
    at com.liferay.util.service.ServiceProps.<init>(ServiceProps.java:66)
    at com.liferay.util.service.ServiceProps.<clinit>(ServiceProps.java:70)

And this lead me to this magical code:

public static final boolean ENTITY_CACHE_ENABLED = GetterUtil.getBoolean(com.liferay.util.service.ServiceProps.get(
         "value.object.entity.cache.enabled.com.methodia.khearos.model.Patient"),
        true);

It seems ServiceProps cannot get properly instantiated, so (just for testing) I removed ServiceProps.get calls and placed the corresponding default values. After that, I ran the test again and everything worked like a charm.

So my question here is how to configure things properly, so I will have a configuration? Or is there any other workaround that doesn't requires me to modify Service Builder code?

3

There are 3 answers

1
Olaf Kock On BEST ANSWER

Especially if it's about Unit Tests you should be able to go without any service builder initialization. IMHO a unit test that touches an outside entity (like: database) is not a proper unit test. You should be able to pass any implementation class of the Patient interface into your validator for it to do the work (you probably want to test the validator, not the patient implementation.

You don't want to unittest that servicebuilder's persistence works. That would rather be an integration test.

So: Just (TM) create a PatientForTestImpl by autogenerating the methods, getters and setters, or use one of the common mocking libraries to do this on the fly and use that object for testing the validator.

0
Gevatterjan On

It looks like the Spring Context is not properly initialized.

Try the following:

In your test class, write setup method, which will invoke "InitUtil.initWithSpring()" method. Write your test with the @Test annotation

0
Velislava Kostadinova On

I had the same problem. I tried different scenarios and test libraries.

I solved my problems when I included PowerMockito as a test framework in my project.

Try to configure Mockito or PowerMockito as your test framework. If your tests failed again you can try to use wrapper classes as Olaf Kock suggested.