Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet HTML/CSS Java JavaScript Node.js Python Python 3.11, Pandas, SQL
      Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. C#
  4. Unit Testing NHibernate Repositories in ASP.NET Core Projects

Unit Testing NHibernate Repositories in ASP.NET Core Projects

Date- Apr 06,2026 2
unit testing nhibernate

Overview

Unit testing is a software testing method by which individual units of source code, such as functions or classes, are tested to determine if they are fit for use. In the context of NHibernate repositories, unit testing becomes a critical component for ensuring that data access logic behaves as expected. By isolating data access code from external dependencies, developers can validate the correctness of their repository implementations without the overhead of actual database interactions.

NHibernate is an object-relational mapping (ORM) framework for .NET, enabling developers to work with data in the form of domain-specific objects, while abstracting away the underlying database. This abstraction simplifies data manipulation but introduces layers of complexity that necessitate thorough testing. Real-world use cases for unit testing NHibernate repositories include validating CRUD operations, ensuring database constraints are respected, and verifying that business rules are enforced consistently.

Prerequisites

  • ASP.NET Core: Familiarity with building web applications using ASP.NET Core framework.
  • C# Programming: Proficiency in C# syntax and concepts, particularly in object-oriented programming.
  • Unit Testing Frameworks: Understanding of unit testing frameworks like xUnit or NUnit.
  • NHibernate: Basic knowledge of NHibernate and its configuration in ASP.NET Core applications.
  • Mocking Libraries: Experience with mocking frameworks such as Moq for creating test doubles.

Unit Testing Basics

Unit testing is the foundation of software quality assurance. It allows developers to test individual components of their applications in isolation, which is especially important for repositories that encapsulate data access logic. A well-structured unit test should verify that a method performs as intended, handling both expected and unexpected inputs correctly.

In the context of NHibernate, unit tests can help ensure that repository methods return the expected entities, handle exceptions appropriately, and interact with the database as intended. This reduces the risk of regressions and bugs creeping into the application as it evolves over time.

Creating a Simple Test Case

To illustrate the process, consider a simple repository interface and its implementation:

public interface IProductRepository { Product GetProductById(int id); void AddProduct(Product product); }

Here’s the implementation using NHibernate:

public class ProductRepository : IProductRepository { private readonly ISession _session; public ProductRepository(ISession session) { _session = session; } public Product GetProductById(int id) { return _session.Get(id); } public void AddProduct(Product product) { using (var transaction = _session.BeginTransaction()) { _session.Save(product); transaction.Commit(); } }}

In this code, the repository interacts with NHibernate's session to fetch a product by ID and to add a new product. Next, we will write a unit test for the GetProductById method.

public class ProductRepositoryTests { [Fact] public void GetProductById_ReturnsProduct_WhenProductExists() { // Arrange var mockSession = new Mock(); var product = new Product { Id = 1, Name = "Test Product" }; mockSession.Setup(s => s.Get(1)).Returns(product); var repository = new ProductRepository(mockSession.Object); // Act var result = repository.GetProductById(1); // Assert Assert.NotNull(result); Assert.Equal("Test Product", result.Name); }}

In this test, we create a mock of the ISession interface to simulate database interactions without requiring an actual database. The Setup method specifies that when Get is called with an ID of 1, it should return a predefined product. The test then verifies that the returned product is not null and that its name matches the expected value.

Configuring NHibernate for Unit Testing

Before diving deeper into testing, it is essential to configure NHibernate correctly for unit tests. Typically, NHibernate requires a session factory, which can be cumbersome to set up in a testing environment. Instead, we can utilize an in-memory database for testing purposes, allowing for rapid execution of tests without needing a persistent database.

To set up NHibernate with an in-memory database, you can use SQLite in memory mode. This way, each test can run against a fresh instance of the database. Here’s how to configure NHibernate for testing:

public class NHibernateHelper { public static ISessionFactory CreateSessionFactory() { return new Configuration() .Configure() // Config file .SetProperty(NHibernate.Cfg.Environment.Dialect, "NHibernate.Dialect.SQLiteDialect") .SetProperty(NHibernate.Cfg.Environment.ConnectionDriver, "NHibernate.Driver.SQLite20Driver") .SetProperty(NHibernate.Cfg.Environment.ConnectionString, "Data Source=:memory:") .BuildSessionFactory(); } }

This configuration specifies that NHibernate should use SQLite as the database dialect and connect to an in-memory database. The CreateSessionFactory method can be called during test initialization to ensure that each test has access to a new database instance.

Integration with Test Frameworks

To integrate our NHibernate setup with a testing framework like xUnit, we can create a TestBase class that initializes the session factory before each test. Here's an example:

public class TestBase : IDisposable { protected ISessionFactory _sessionFactory; protected ISession _session; public TestBase() { _sessionFactory = NHibernateHelper.CreateSessionFactory(); _session = _sessionFactory.OpenSession(); } public void Dispose() { _session.Dispose(); _sessionFactory.Dispose(); } }

The TestBase class implements IDisposable to clean up resources after tests are run. This ensures that each test starts with a fresh NHibernate session, preventing state leakage between tests.

Mocking Dependencies

Unit tests often require mocking dependencies to isolate the system under test. In the case of NHibernate repositories, this involves mocking the ISession interface. Using a mocking library like Moq allows developers to define how the mocked objects should behave in various scenarios.

For example, consider a scenario where you need to test how your repository handles an exception when trying to save a product. You can set up your mock to throw an exception:

mockSession.Setup(s => s.Save(It.IsAny())).Throws(new Exception("Database error"));

This setup will ensure that when Save is called, it throws an exception, allowing you to test how your repository handles this failure case.

Testing Exception Handling

Here’s how you might write a test to ensure that your repository handles exceptions correctly:

[Fact] public void AddProduct_ThrowsException_WhenDatabaseErrorOccurs() { // Arrange var mockSession = new Mock(); mockSession.Setup(s => s.Save(It.IsAny())).Throws(new Exception("Database error")); var repository = new ProductRepository(mockSession.Object); var product = new Product(); // Act & Assert Assert.Throws(() => repository.AddProduct(product)); }

This test checks that when an exception is thrown during the AddProduct operation, it is correctly propagated. Using Assert.Throws, we can verify that the expected exception type is thrown, ensuring that our repository responds to errors as designed.

Edge Cases & Gotchas

Unit testing NHibernate repositories can present specific challenges and pitfalls. One common issue arises from not properly managing the lifecycle of the NHibernate session, leading to stale data or connection leaks. Always ensure that sessions are correctly disposed of after use.

Another pitfall is failing to account for the behavior of NHibernate's lazy loading. If a property is not explicitly loaded, it will be fetched from the database when accessed, which can lead to unexpected results during testing. To avoid this, ensure that all necessary data is eagerly loaded or explicitly mocked during tests.

Example of Incorrect vs. Correct Approach

Incorrect Approach:

public Product GetProductById(int id) { var product = _session.Get(id); return product; // Lazy loading might occur here }

Correct Approach:

public Product GetProductById(int id) { var product = _session.Query().Fetch(p => p.Category).FirstOrDefault(p => p.Id == id); return product; // Eagerly loading Category }

Performance & Best Practices

When unit testing NHibernate repositories, performance can be a concern, especially with large datasets or complex queries. To mitigate this, keep your tests focused on small, isolated units of functionality. Use mocking to avoid hitting the database, which can significantly reduce test execution time.

It’s also essential to maintain a clear separation between unit tests and integration tests. Unit tests should focus on individual methods, while integration tests can validate the interaction between the repository and the actual database. This separation allows for faster feedback during development.

Best Practices

  • Use In-Memory Databases: Leverage in-memory databases for faster test execution.
  • Mock External Dependencies: Always mock dependencies like NHibernate sessions to isolate tests.
  • Keep Tests Isolated: Ensure tests do not depend on each other to avoid flaky tests.
  • Clear Naming Conventions: Use descriptive names for tests that indicate their purpose clearly.

Real-World Scenario

Consider a mini-project where we are developing a simple e-commerce application with product management functionality. The application uses NHibernate to manage product data. Below is a complete implementation of the repository and its tests.

public class Product { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual decimal Price { get; set; } } public class ProductRepository : IProductRepository { private readonly ISession _session; public ProductRepository(ISession session) { _session = session; } public virtual Product GetProductById(int id) { return _session.Get(id); } public virtual void AddProduct(Product product) { using (var transaction = _session.BeginTransaction()) { _session.Save(product); transaction.Commit(); } } } public class ProductRepositoryTests : TestBase { [Fact] public void GetProductById_ReturnsProduct_WhenProductExists() { // Arrange var product = new Product { Id = 1, Name = "Test Product" }; _session.Save(product); _session.Flush(); // Act var result = new ProductRepository(_session).GetProductById(1); // Assert Assert.NotNull(result); Assert.Equal("Test Product", result.Name); } [Fact] public void AddProduct_SavesProductSuccessfully() { // Arrange var product = new Product { Name = "New Product", Price = 19.99M }; var repository = new ProductRepository(_session); // Act repository.AddProduct(product); // Assert var savedProduct = _session.Get(product.Id); Assert.NotNull(savedProduct); Assert.Equal("New Product", savedProduct.Name); }}

This example demonstrates how to implement a repository for managing products and includes tests for retrieving and adding products. The GetProductById test verifies that the repository fetches the correct product, while the AddProduct test ensures that products are saved correctly in the in-memory database.

Conclusion

  • Unit testing NHibernate repositories is essential for ensuring data integrity and reliability in ASP.NET Core applications.
  • Mocking dependencies like NHibernate sessions helps isolate tests and improve performance.
  • It is critical to manage NHibernate session lifecycles to avoid stale data and connection issues.
  • Adopting best practices such as using in-memory databases and clear naming conventions can significantly enhance your testing strategy.
  • Real-world projects benefit from structured repository patterns, enabling cleaner code and easier testing.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Mastering Unit Testing with xUnit in .NET: A Comprehensive Guide
Mar 16, 2026
Performance Tuning NHibernate for ASP.NET Core Applications
Apr 05, 2026
Troubleshooting NHibernate Errors in ASP.NET Core Applications
Apr 05, 2026
Integrating ASP.NET Core Identity with NHibernate for Robust User Management
Apr 06, 2026
Previous in C#
Mapping Strategies for NHibernate in ASP.NET Core: A Comprehensiv…
Next in C#
Integrating ASP.NET Core Identity with NHibernate for Robust User…
Buy me a pizza

Comments

On this page

🎯

Interview Prep

Ace your C# interview with curated Q&As for all levels.

View C# Interview Q&As

More in C#

  • Zoom C# Wrapper Integration 12905 views
  • Convert HTML String To Image In C# 11490 views
  • The report definition is not valid or is not supported by th… 10835 views
  • Replacing Accent Characters with Alphabet Characters in CSha… 9811 views
  • Get IP address using c# 8664 views
View all C# posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor