Searching for bean gives classNotFoundException

3 views
Skip to first unread message

Steinar Bang

unread,
Dec 11, 2023, 9:15:43 AM12/11/23
to op...@googlegroups.com
I need some pax exam help.

I get a ClassNotFoundException I don't know how to fix.

I can access the bean Account for use in asserts without errors.

But once I use stream, filter and findFirst to find a particular account
I get ClassNotFoundException on the account.

I have the following pax exam test:
https://github.com/steinarb/liquibase-karaf-feature/blob/master/liquibase-integration-test/karaf.liquibase.tests/src/test/java/no/priv/bang/karaf/liquibase/tests/LiquibaseKarafFeatureIntegrationTest.java#L55

Here I access beans of type Account in the pax exam test.

However if I try searching for a bean using stream and filter, like this:
@Test
public void testLoadFeature() throws Exception { // NOSONAR this test has an assert, just not an assert sonar recognizes
installAndAssertFeature("karaf-liquibase-sample-datasource-receiver");
var service = getOsgiService(SampleLiquibaseDatasourceReceiverService.class);
var initialAccounts = service.accounts();
assertEquals(1, initialAccounts.size());
var initialAccount = initialAccounts.get(0);
assertEquals("jod", initialAccount.getUsername());
var newAccount = Account.with().username("jad").build();
var accountsAfterAdd = service.addAccount(newAccount);
assertEquals(2, accountsAfterAdd.size());
var addedAccount = accountsAfterAdd.stream().filter(a -> "jad".equals(a.getUsername())).findFirst().orElseThrow();
assertEquals(initialAccount.getId() + 1, addedAccount.getId());
assertEquals("jad", addedAccount.getUsername());
}

Then I get the following error:
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 6.93 s <<< FAILURE! - in no.priv.bang.karaf.liquibase.tests.LiquibaseKarafFeatureIntegrationTest
[ERROR] testLoadFeature(no.priv.bang.karaf.liquibase.tests.LiquibaseKarafFeatureIntegrationTest) Time elapsed: 0.061 s <<< ERROR!
java.lang.ClassNotFoundException: no.priv.bang.karaf.liquibase.sample.services.Account not found by PAXEXAM-PROBE-37daa15f-5c4f-4a37-b284-8ef8ac7cb232 [77]


I googled the error message and found this: https://stackoverflow.com/a/43009600

I have tried adding a probeConfiguration to the test, by adding this
method to the test:

@ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
probe.setHeader(Constants.IMPORT_PACKAGE, "*,no.priv.bang.karaf.liquibase.sample.services.*");
return probe;
}

But that just gave me a different error message:
ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 13.32 s <<< FAILURE! - in no.priv.bang.karaf.liquibase.tests.LiquibaseKarafFeatureIntegrationTest
[ERROR] no.priv.bang.karaf.liquibase.tests.LiquibaseKarafFeatureIntegrationTest Time elapsed: 11.865 s <<< ERROR!
java.lang.RuntimeException: Bundle cannot be installed
Caused by: org.osgi.framework.BundleException: Unable to resolve PAXEXAM-PROBE-20d166e8-9d0f-41f0-86b8-79228ed20833 [77](R 77.0): missing requirement [PAXEXAM-PROBE-20d166e8-9d0f-41f0-86b8-79228ed20833 [77](R 77.0)] osgi.wiring.package; (osgi.wiring.package=no.priv.bang.karaf.liquibase.sample.services.*) Unresolved requirements: [[PAXEXAM-PROBE-20d166e8-9d0f-41f0-86b8-79228ed20833 [77](R 77.0)] osgi.wiring.package; (osgi.wiring.package=no.priv.bang.karaf.liquibase.sample.services.*)]

Does anyone know why I get the ClassNotFoundException when I use the
stream, but not when accessing the Account variables in the asserts?

Does anyone know what to do to fix this?

Thanks!

Steinar Bang

unread,
Dec 11, 2023, 12:21:26 PM12/11/23
to op...@googlegroups.com
So I tried a simple for loop instead:
@Test
public void testLoadFeature() throws Exception { // NOSONAR this test has an assert, just not an assert sonar recognizes
installAndAssertFeature("karaf-liquibase-sample-datasource-receiver");
var service = getOsgiService(SampleLiquibaseDatasourceReceiverService.class);
var initialAccounts = service.accounts();
assertEquals(1, initialAccounts.size());
var initialAccount = initialAccounts.get(0);
assertEquals("jod", initialAccount.getUsername());
var newAccount = Account.with().username("jad").build();
var accountsAfterAdd = service.addAccount(newAccount);
assertEquals(2, accountsAfterAdd.size());
var addedAccount = findAccountWithUsername(accountsAfterAdd, "jad");
assertEquals(initialAccount.getId() + 1, addedAccount.getId());
assertEquals("jad", addedAccount.getUsername());
}

private Account findAccountWithUsername(List<Account> accounts, String username) {
for (var a : accounts) {
if (username.equals(a.getUsername())) {
return a;
}
}

return null;
}

But that gave me the same ClassNotFoundException:
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 6.943 s <<< FAILURE! - in no.priv.bang.karaf.liquibase.tests.LiquibaseKarafFeatureIntegrationTest
[ERROR] testLoadFeature(no.priv.bang.karaf.liquibase.tests.LiquibaseKarafFeatureIntegrationTest) Time elapsed: 0.068 s <<< ERROR!
java.lang.ClassNotFoundException: no.priv.bang.karaf.liquibase.sample.services.Account not found by PAXEXAM-PROBE-a4ad3d47-fbfa-48c6-a000-2a24fed9ffbf [77]

How come I can call Account.getUsername() in an assert but not in code
that iterates through a for loop?

When outputing trace debug I was able to call Account.toString() just fine.

Steinar Bang

unread,
Dec 11, 2023, 12:30:50 PM12/11/23
to op...@googlegroups.com
This is clumsy, but it worked:

@Test
public void testLoadFeature() throws Exception { // NOSONAR this test has an assert, just not an assert sonar recognizes
installAndAssertFeature("karaf-liquibase-sample-datasource-receiver");
var service = getOsgiService(SampleLiquibaseDatasourceReceiverService.class);
var initialAccounts = service.accounts();
assertEquals(1, initialAccounts.size());
var initialAccount = initialAccounts.get(0);
assertEquals("jod", initialAccount.getUsername());
var newAccount = Account.with().username("jad").build();
var accountsAfterAdd = service.addAccount(newAccount);
assertEquals(2, accountsAfterAdd.size());
Account addedAccount = null;
for (var a : accountsAfterAdd) {
if ("jad".equals(a.getUsername())) {
addedAccount = a;
}
}
assertEquals(initialAccount.getId() + 1, addedAccount.getId());
assertEquals("jad", addedAccount.getUsername());
}

So I guess it has something to do with function scopes?

Steinar Bang

unread,
Dec 12, 2023, 1:40:17 AM12/12/23
to op...@googlegroups.com
>>>>> Steinar Bang <sb-1rLz...@public.gmane.org>:

> So I guess it has something to do with function scopes?

I am thinking the clue is in one word in the error message:
> [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 6.93 s <<< FAILURE! - in no.priv.bang.karaf.liquibase.tests.LiquibaseKarafFeatureIntegrationTest
> [ERROR] testLoadFeature(no.priv.bang.karaf.liquibase.tests.LiquibaseKarafFeatureIntegrationTest) Time elapsed: 0.061 s <<< ERROR!
> java.lang.ClassNotFoundException: no.priv.bang.karaf.liquibase.sample.services.Account not found by PAXEXAM-PROBE-37daa15f-5c4f-4a37-b284-8ef8ac7cb232 [77]

The word is "PROBE".

I think this happens in code that check if the test is runnable, and not
when it actually runs.

As long as the test method itself will run in OSGi, then both the
stream/filter/findFirst example and the separate method example would
run fine in OSGi (because they would end up in the same .class file and
be loaded by the same classloader).

Oh well!

The current, clumsy, example works.
Reply all
Reply to author
Forward
0 new messages