Open Closed

ABP Test projects #9690


User avatar
0
DominaTang created

Since our EF core Mapping use some syntax not supporting SqlLite, we need to mock each repository / app service when write test code for an AppService. However, while doing this way, AppService's Logger is null and throw exception for all places using it in the AppService. I guess any place using CurrentUser, CurrentTenant, CurrentUnitOfWork would also throw null reference exception


6 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    • When you mock repositories or app services in ABP unit tests (especially to avoid using SQLite due to unsupported EF Core mappings), dependencies like Logger, CurrentUser, CurrentTenant, and CurrentUnitOfWork are not automatically injected and will be null unless explicitly provided.

    To resolve this, you should manually provide these dependencies in your test setup. ABP provides default/fake implementations for these services, which you can register in your test module or test setup. Here’s how you can do it:

    public class MyAppServiceTests
    {
        private readonly MyAppService _appService;
    
        public MyAppServiceTests()
        {
            var logger = Substitute.For<ILogger<MyAppService>>();
            var currentUser = Substitute.For<ICurrentUser>();
            var currentTenant = Substitute.For<ICurrentTenant>();
            var currentUnitOfWork = Substitute.For<ICurrentUnitOfWork>();
    
            _appService = new MyAppService(
                /* your mocked repository */,
                logger,
                currentUser,
                currentTenant,
                currentUnitOfWork
                // ...other dependencies
            );
        }
    
        [Fact]
        public void Test_Something()
        {
            // Arrange/Act/Assert
        }
    }
    

    Alternatively, if you use ABP’s dependency injection in your test base, you can register these as singletons or use ABP’s built-in fake services:

    protected override void BeforeAddApplication(IServiceCollection services)
    {
        services.AddSingleton(Substitute.For<ILogger<MyAppService>>());
        services.AddSingleton(Substitute.For<ICurrentUser>());
        services.AddSingleton(Substitute.For<ICurrentTenant>());
        services.AddSingleton(Substitute.For<ICurrentUnitOfWork>());
        base.BeforeAddApplication(services);
    }
    

    This ensures that when your AppService is resolved, all these dependencies are injected and not null.

    • For more details, see the official documentation on ABP unit testing:

    Sources

    • https://abp.io/docs/latest/testing/unit-tests

    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.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    These base class properties will be obtained from LazyServiceProvider

    Can you also mock and set the LazyServiceProvider of the base class?

    Thanks

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Btw, you can consider using Docker in your unit tests

    https://abp.io/community/articles/using-testcontainers-in-abp-unit-test-b67gzpxg

  • User Avatar
    0
    DominaTang created

    Thank you very much. I will try the Docker solution for SQL server. ABP's integration testing bootstrap is simple to use. However, for Micro Service architect, if an app service directly call another Micro service's API, then it would have problem when set up integration testing.

  • User Avatar
    0
    DominaTang created

    Another approach that worth a try is using Aspire.Net. Here is AI's answer: Never use it, but it seems worth a try.

    .How to Use Aspire for Integration Testing: Add the Testing Package: Include the Aspire.Hosting.Testing NuGet package in your test project. Create a Test Host: Use DistributedApplicationFactory<TProgram> to create a factory that can launch your Aspire app host for testing. Configure the Test Environment: Define any necessary environment variables or containerized resources required for your tests within the DistributedApplicationFactory setup. Interact with Services: Obtain HttpClient instances or other client proxies from the DistributedApplicationFactory to interact with your services under test. Write Assertions: Implement test assertions to verify the expected behavior and interactions between your microservices. Clean Up Resources: Ensure proper disposal of the DistributedApplicationFactory to clean up resources after tests complete.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    ABP can work with Aspire without any issues. You can use any testing method that suits you.

    https://abp.io/community/articles/how-to-use-.net-aspire-with-abp-framework-h29km4kk

    Thanks.

Learn More, Pay Less
33% OFF
All Trainings!
Get Your Deal
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on September 12, 2025, 10:20