Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8015859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:22:34+00:00 2026-06-04T20:22:34+00:00

How would I write a unit test to ensure that a lock was acquired?

  • 0

How would I write a unit test to ensure that a lock was acquired?

For example:

public void AddItem(object item)
{
    lock (_list)
    {
        _list.Add(item)
    }
}

Is there a way to ensure that the lock statement is run?

Edit: I’m not trying to prove thread-safety (this I would assume), just that the lock() statement is called.

For example, I might test a new object() statement by substituting it in a CreateObject() factory function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:22:35+00:00Added an answer on June 4, 2026 at 8:22 pm

    The same way you test any other thing: write a test that fails without it. In other words, determine why you are writing the lock in the first place, and write a test that specifies that reason. I assume the reason is thread-safety: if so, write the multi-thread test. If it is some other reason, right that test.

    If you really, really want to test calling the lock but not the behavior the lock causes, encapsulate:

    public class MyLock : IDisposable
    {
        private object _toLock;
    
        public MyLock(object toLock)
        {
            _toLock = toLock;
            Monitor.Enter(_toLock);
        }
    
        public virtual void Dispose()
        {
            Monitor.Exit(_toLock);
        }
    }
    

    You’ll have to make a mockable factory too, of course. Seems like overdoing it to me, but maybe it makes sense in your context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a unit test to ensure that the view I am
I am trying to write a unit test using .NET 4 to ensure that
I would like to write the unit test for a text parser class but
I would like to write unit tests to test whether a dependency exists between
I want to write a unit test that performs HTTP requests directly (instead of
I have an openGL display that I would like to write unit-tests for. However,
Is it possible to write a doctest unit test that will check that an
I'm trying to write a unit test for a module that will give me
I would like to write a Unit Test for a (rather complex) Bash completion
Came across the following MS Unit Test: [TestMethod] public void PersonRepository_AddressCountForSinglePerson_IsNotEqualToZero() { // Arrange.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.