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

  • SEARCH
  • Home
  • 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 7060649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:22:40+00:00 2026-05-28T04:22:40+00:00

I wrote an implementation for a priority queue I needed, and now I would

  • 0

I wrote an implementation for a priority queue I needed, and now I would like to test it. I decided on using moq, as I’ve already used rhino mocks at work and want to try something new/possibly easier.

The interface for my PriorityQueue is pretty straight-forward:

public interface IPriorityQueue<TKey, TValue>  where TKey : IComparable
{
    void Enqueue(TKey priority, TValue value);
    bool IsEmpty();
    TValue Peek();
    TValue Dequeue();
}

I went to write my first test, which tests the Enqueue method. Here’s the implementation for it:

public void Enqueue(TKey priority, TValue value)
{
    if (priority == null) { throw new ArgumentNullException("priority"); }

    if (_queue.ContainsKey(priority))
    {
        // queue contains key, therefore just add value to existing key's list
        _queue[priority].Add(value);
    }

    // otherwise just add pair
    else
    {
        _queue.Add(priority, new List<TValue>() { value });
    }
}

The first unit test I wrote was to test if the key was null, it should throw an argument null exception.

[TestMethod]
public void EnqueueNullKey_ThrowsArgumentNullException()
{
    /* Arrange */
    var mock = new Mock<IPriorityQueue<string, IMessage>>();

    // string implements the IComparable interface, and is nullable.
    mock
        .Setup(x => x.Enqueue(null, It.IsAny<IMessage>()))
        .Throws<ArgumentNullException>();

    /* Assert */
    mock.VerifyAll();   
}

So I realize now, that my method Enqueue will never be called, because I am instantiating an instance of my interface, not the implementation. Then the question begs to be asked, if I’m supposed to test with using my interfaces (at-least that was the impression I got after watching Roy Osherove’s TDD – Understanding Mock Objects video) how am I to test my implementation?

Am I misunderstanding the advice to test with interfaces?

In the video he created a class within the test he was writing, and used that to test. I don’t see how that would help me test my implementation of PriorityQueue (specifically the Enqueue method).

Thanks stack overflow!

edit: Here’s the following (ugly) working test that I’ve came up with. I’m extremely unhappy with it, it feels so primitive. Can anyone suggest a better way of doing this? From the responses below, it seems like the framework is completely unnecessary for this unit test.

However, here it is:

[TestMethod]
public void EnqueueNullKey_ThrowsArgumentNullException()
{
    /* Arrange */
    var pq = new PriorityQueue<string, IMessage>();

    try
    {
        pq.Enqueue(null, null);
    }
    catch(ArgumentNullException)
    {
        Assert.IsTrue(true);
        return;
    }

    // failure condition if we don't catch the exception
    Assert.IsTrue(false);   
}
  • 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-05-28T04:22:41+00:00Added an answer on May 28, 2026 at 4:22 am

    You are getting it wrong. You don’t need a mock of IPriorityQueue when you are testing methods on one of its implementation. What you need is a mock of queue

    The responsibilities of Enqueue method are

    1. throwing exception if priority is null
    2. doing something on if condition
    3. doing something on else condition

    For #1 testing is quite easy and a stub implementation of queue will suffice, for #2 and #3 you will need a mock of queue. For #2 and #3 you should test if mock queue’s Add methods is called with correct parameters

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

Sidebar

Related Questions

I've wrote a ThreadPool implementation in C# and now I would like to port
I wrote an implementation of the Mandelbrot set in Java using a JComponent but
I just wrote some NIO-code and wonder how to stress-test my implementation regarding SocketChannel.write(ByteBuffer)
Mr. Ayende wrote in his latest blog post about an implementation of a queue
I am new to REST webservice, I tried using Jersey implementation and wrote a
The other day I decided to write an implementation of radix sort in Java.
I'm trying to write a program to test student code against a good implementation.
Wrote a quick Java proggy to spawn 10 threads with each priority and calculate
I wrote a windows service using VB that read some legacy data from Visual
I wrote a prime number generator using Sieve of Eratosthenes and Python 3.1. The

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.