0
    private PreconfigGroupOpenLPolicyRulesService openlApi;

@Override
public Object[] getPackageEligibility(IRulesRuntimeContext context, Dimension dimension) {
    return openlApi.PackageEligibility(context, dimension);
}

I am new to jUnits. Can anyone help me to write test case for method getPackageEligibility. Which are all i need to do @Mock.

i tried

PreconfigGroupOpenLComponentService preconfigGroupOpenLComponentService = new PreconfigGroupOpenLComponentService();

@Mock
PreconfigGroupOpenLPolicyRulesService openlApi;
@Mock
IRulesRuntimeContext context;
@Mock
Dimension dimension;



@Test
public void testGetPackageEligibility() {

    Object[] arg = preconfigGroupOpenLComponentService.getPackageEligibility(context, dimension);
    assertNotNull(arg);

}

But getting

java.lang.NullPointerException at com.exigen.eis.preconfig.policy.group.services.impl.PreconfigGroupOpenLComponentService.getPackageEligibility(PreconfigGroupOpenLComponentService.java:32) at com.exigen.eis.preconfig.policy.group.services.impl.PreconfigGroupOpenLComponentServiceTest.testGetPackageEligibility(PreconfigGroupOpenLComponentServiceTest.java:28)

drup
  • 1,047
  • 2
  • 9
  • 17
  • Remove `private` modifier and make the method `default = package private`. Then put a test class in the same package, it will have access to this method. – krokodilko Jun 11 '16 at 08:41
  • You need somehow to "inject" the mocked `PreconfigGroupOpenLPolicyRulesService` into the instance of `PreconfigGroupOpenLComponentService` used in the test (maybe mockito `@InjectMock` or some setter) –  Jun 11 '16 at 09:06
  • First you need to know the boundry of JUnit and Integration test. JUnit should be only limited to test the business logic of your class, not that of your dependent class. In your example `openlApi.PackageEligibility(context, dimension);` is not part of your class. This method should be tested in test class of `PreconfigGroupOpenLPolicyRulesService`. So if you want to test it, you need to mock `PreconfigGroupOpenLPolicyRulesService` class and add a mock return in case `PackageEligibility(context, dimension)` method is called. – sauumum Jun 11 '16 at 12:09
  • ` @Test public void testGetPackageEligibility() { Object obj = new Object(); Mockito.when(openlApi.PackageEligibility(context, dimension)).thenReturn((Object[]) obj); assertNotNull(obj); }` i tried it..Still null pointer exception. – drup Jun 11 '16 at 13:06

0 Answers0