Create a complete set of unit tests for the Cache class. I have created a test c
ID: 3601259 • Letter: C
Question
Create a complete set of unit tests for the Cache class. I have created a test class called CacheTests that contains a sample test. You should add your tests to this class.
The key to testing class implementations is to realize that it is not usually possible to test each constructor and method in isolation. For example, to test the getOwner() method of the Cache class, you must first use the constructor to create a Cache object.
package cs1410;
import static org.junit.Assert.*;
import org.junit.Test;
public class CacheTests
{
/**
* An example test for Cache objects
*/
@Test
public void test ()
{
Cache c = new Cache("GCRQWK Old Three Tooth geocadet 3.5 3 N40 45.850 W111 48.045");
assertEquals("GCRQWK", c.getGcCode());
}
}
Design a representation and then declare and document your instance variables.
Implement the constructor. Each line of the file caches.txt is an example of a string that the constructor should be able to take as a parameter. (Hint: Use the String split method.)
Implement the methods (other than toString, which is already complete).
Test/debug/modify your Cache and CacheTests classes until you are certain that the Cache class behaves according to its specifications.
Explanation / Answer
up vote14down voteaccepted
If you want true Unit Tests, then you have to mock the cache: write a mock object that implements the same interface as the cache, but instead of being a cache, it keeps track of the calls it receives, and always returns what the real cache should be returning according to the test case.
Of course the cache itself also needs unit testing then, for which you have to mock anything it depends on, and so on.
What you describe, using the real cache object but initializing it to a known state and cleaning up after the test, is more like an integration test, because you are testing several units in concert.
up vote14down voteaccepted
If you want true Unit Tests, then you have to mock the cache: write a mock object that implements the same interface as the cache, but instead of being a cache, it keeps track of the calls it receives, and always returns what the real cache should be returning according to the test case.
Of course the cache itself also needs unit testing then, for which you have to mock anything it depends on, and so on.
What you describe, using the real cache object but initializing it to a known state and cleaning up after the test, is more like an integration test, because you are testing several units in concert.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.