Hon.IFS.SiteManagement.EntityFrameworkCore.Domains.Contacts.ContactRepositoryTests.GetListAsync Source: ContactRepositoryTests.cs line 21 Duration: 1 ms
Message: Volo.Abp.AbpInitializationException : An error occurred during the initialize Volo.Abp.Modularity.OnApplicationInitializationModuleLifecycleContributor phase of the module Hon.IFS.SiteManagement.SiteManagementTestBaseModule, Hon.IFS.SiteManagement.TestBase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: Object reference not set to an instance of an object.. See the inner exception for details. ---- System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
ModuleManager.InitializeModules(ApplicationInitializationContext context)
AbpApplicationBase.InitializeModules()
AbpApplicationWithExternalServiceProvider.Initialize(IServiceProvider serviceProvider)
AbpIntegratedTest1.ctor() SiteManagementTestBase1.ctor()
SiteManagementEntityFrameworkCoreTestBase.ctor()
ContactRepositoryTests.ctor() line 14
RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
----- Inner Stack Trace -----
SiteManagementDataSeedContributor.SeedAsync(DataSeedContext context) line 58
<11 more frames...>
ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
TaskExtensions.WaitAndUnwrapException(Task task)
AsyncContext.Run(Func1 action) AsyncHelper.RunSync(Func1 action)
SiteManagementTestBaseModule.SeedTestData(ApplicationInitializationContext context) line 32
SiteManagementTestBaseModule.OnApplicationInitialization(ApplicationInitializationContext context) line 27
OnApplicationInitializationModuleLifecycleContributor.Initialize(ApplicationInitializationContext context, IAbpModule module)
ModuleManager.InitializeModules(ApplicationInitializationContext context)
1 Answer(s)
-
0
- Solution Your test failure is thrown while ABP initializes the test application and runs your custom data seeding (NullReferenceException inside SiteManagementDataSeedContributor.SeedAsync). This typically happens in ABP tests when:
- Seed contributors assume services or data that aren’t available in the test environment.
- Seeding runs outside the proper tenant context.
- Seeding touches EF Core or repositories before the DbContext is ready or within an unexpected transaction.
Apply the following steps to make your integration tests deterministic and fix the failure:
- Guard your seed contributor against nulls and run inside a unit of work
public class SiteManagementDataSeedContributor : IDataSeedContributor, ITransientDependency { private readonly IUnitOfWorkManager _uowManager; private readonly IRepository<Contact, Guid> _contactRepository; // example private readonly ICurrentTenant _currentTenant; public SiteManagementDataSeedContributor( IUnitOfWorkManager uowManager, IRepository<Contact, Guid> contactRepository, ICurrentTenant currentTenant) { _uowManager = uowManager; _contactRepository = contactRepository; _currentTenant = currentTenant; } public async Task SeedAsync(DataSeedContext context) { // Avoid host-only seeding when you expect tenant-only data // and/or ensure you seed for a specific tenant using (_currentTenant.Change(context?.TenantId)) { using (var uow = _uowManager.Begin(requiresNew: true)) { // Defensive checks to prevent NREs if (_contactRepository == null) { await uow.CompleteAsync(); return; } // Example: skip if already seeded var count = await _contactRepository.GetCountAsync(); if (count == 0) { await _contactRepository.InsertAsync(new Contact(/* init fields */), autoSave: true); // ... add more seed data } await uow.CompleteAsync(); } } } }- Ensure your test base module seeds within OnApplicationInitialization and inside a scope
public class SiteManagementTestBaseModule : AbpModule { public override void OnApplicationInitialization(ApplicationInitializationContext context) { SeedTestData(context); } private static void SeedTestData(ApplicationInitializationContext context) { AsyncHelper.RunSync(async () => { using var scope = context.ServiceProvider.CreateScope(); var dataSeeder = scope.ServiceProvider.GetRequiredService<IDataSeeder>(); // Optionally seed for host or a specific test tenant await dataSeeder.SeedAsync(new DataSeedContext(/* tenantId: null or a tenant Guid */)); }); } }- If multi-tenancy is involved, make sure your tenant-scoped entities implement IMultiTenant If your Contacts (or any entity you seed/query in tests) are tenant-specific, the entity must implement IMultiTenant so ABP applies tenant filters and the correct connection (in database-per-tenant or split schemas) is used.
public class Contact : AggregateRoot<Guid>, IMultiTenant { public Guid? TenantId { get; set; } // other properties... }- Disable transactions in tests if you see transaction-related initialization errors Some providers/settings can cause issues with ambient transactions during seeding and initialization. In your EntityFrameworkCore test module, add:
public override void ConfigureServices(ServiceConfigurationContext context) { Configure<AbpUnitOfWorkDefaultOptions>(options => { options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled; }); context.Services.AddAlwaysDisableUnitOfWorkTransaction(); Configure<FeatureManagementOptions>(options => { options.SaveStaticFeaturesToDatabase = false; options.IsDynamicFeatureStoreEnabled = false; }); Configure<PermissionManagementOptions>(options => { options.SaveStaticPermissionsToDatabase = false; options.IsDynamicPermissionStoreEnabled = false; }); }Keep test seeding minimal and deterministic
- Avoid external services or configuration that are not present in tests.
- Use repositories only after Unit of Work is started.
- Prefer idempotent seeding (check existence before insert).
If you need tenant data during test, explicitly seed for that tenant
var tenantId = /* known test tenant Guid */; await dataSeeder.SeedAsync(new DataSeedContext(tenantId));These practices address:
- The NullReferenceException by defensive coding and proper DI usage.
- Ensuring the correct tenant context is used during seeding.
- Avoiding transaction-related issues that can surface during test initialization.
- Sources
- https://docs.abp.io/en/abp/latest/Testing
- https://abp.io/support/questions/8896
- https://abp.io/support/questions/6135
This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.