Test The Take Method Second

This is part of JavaUnitTestChallengeSolved. -- DonWells


We can quickly write a unit test for stand-alone behavior of the take method:

 public class TakeAloneTest? extends Test {
    BoundedBuffer buffer;

public void run(){ buffer = new BoundedBuffer(); buffer.buffer[0] = "a"; buffer.buffer[1] = "b"; buffer.buffer[2] = "c"; buffer.buffer[3] = "d"; buffer.occupied = 4; buffer.takeAt = 2; should(take() == "c", "first out should be c"); should(take() == "d", "second out should be d"); should(buffer.takeAt == 4, "takeAt should be out of bounds now"); should(take() == "a", "third out should be a"); should(take() == "b", "fourth out should be b"); should(buffer.occupied == 0, "The buffer should be empty"); should(buffer.takeAt == 2, "takeAt should be 2 again");}

public String take(){ String taken; try { taken = (String) buffer.take();} catch (InterruptedException exception){ taken = "";}; return taken;} }

And this runs just fine.

I will need to extend my unit TestFramework? to have a setUp and tearDown like JUnit I think. I seem to understand this half fairly well now. So I must look at how these two behave when in a thread. To do this, I should first extend the TestFramework? to have some facility for identifying when a test is deadlocked.


EditText of this page (last edited March 4, 2002) or FindPage with title or text search