Test Class Issue with UNKNOWN_EXCEPTION, portal account owner must have a role

You would need to create a test user which has a role defined, and then runas test user. With this all the account or contacts which are getting created will have Owner as this test user, and now when in your code the community user from your code it wont throw an error.

This test user is different from Community user, "test user" is just used to remedy the issue which you are facing in your test class.


This error comes when your community user' Account owner doesn't have any role. So if you are creating community user in test class and then check who is the account owner which you are using to create user. And then check if account owner has any role. If they don't have any role then first you need to assign them a role.

It will solve your problem.