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 8534037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:06:10+00:00 2026-06-11T10:06:10+00:00

I have added a cache layer to my project . now I wonder if

  • 0

I have added a cache layer to my project . now I wonder if I could unit test methods that manipulate cache ? or is there a better way to test Layer’s logic ?

I just want to check the process , for example :

1- when the item is not in the cache , method should hit the database

2- the next time method should use cache

3- when a change is made to database , cache should be cleared

4- if data retrieved from databse is null , it shouldn’t be added to cache

I want to ensure that the logic I have placed into the methods are working as expected .

  • 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-11T10:06:11+00:00Added an answer on June 11, 2026 at 10:06 am

    I’m presuming the cache is a third party cache? If so, I would not test it. You’re testing someone else’s code otherwise.

    If this caching is so important you need to test it, I’d go with an integration or acceptance test. In other words, hit the page(s)/service(s) in question and check the content that way. By the very definition of what you wish to test, this is not a unit test.

    On the flip side, if the cache is one you’ve rolled yourself, you’ll easily be able to unit test the functionality. You might want to check out verification based testing in order to test the behavior of the cache, as apposed to actually checking stuff is added/removed from the cache. Check out mocking for ways to achieve this.

    To test for behaviour via Mock objects (or something similar) I’d do the following – although your code will vary.

    class Cacher
    {
        public void Add(Thing thing) 
        {
             // Complex logic here...
        }
    
        public Thing Get(int id) 
        { 
            // More complex logic here...
        } 
    }
    
    void DoStuff() 
    {
       var cacher = new Cacher();
       var thing = cacher.Get(50);
       thing.Blah();
    }
    

    To test the above method I’d have a test which used a mock Cacher. You’d need to pass this into the method at runtime or inject the dependency into the constructor. From here the test would simply check that cache.Get(50) is invoked. Not that the item is actually retrieved from the cache. This is testing the behavior of how the cacher should be used, not that it is actually caching/retrieving anything.

    You could then fall back to state based testing for the Cacher in isolation. E.g you add/remove items.

    Like I said previously, this may be overkill depending on what you wish to do. However you seem pretty confident that the caching is important enough to warrant this sort of testing. In my code I try to limit mock objects as much as possible, though this sounds like a valid use case.

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

Sidebar

Related Questions

I have added a checker to ensure that all fields in a form have
I have added an external .exe file into my VS2010 setup project and I'd
i have added an sqlite libsqlite3.0.dylib into my project's resources as per given in
I use Spring 3.0 and ehcache. I have added @Cacheable annotations to some methods
We have recently added AppFabric as a caching option in our project (Windows server
I have an actionResult that I've added caching to. [OutputCache(Duration = 120)] When I
I have a MVC3 application to which I've added a couple of simple cache
I have added a navigation controller inside a viewcontroller thats inside another navigation controller...
I have added a plugin called scrollpaper to a site I'm working on. The
I have added an UIImageView as a subview in UIView , and then I

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.