How to write test code before writing source code when they are object dependencies?

I will take an example:
requirement : build a piece of material following this rule: 100 => material is green

100 <among <300 => material yellow among> 300 => material red

My test class:

class MaterialTest{ MaterialConstructor materialCons public void testBuild(){ materialCons.setAmount(150); Material material=materialCons.build(); assertEqual(material.getColor(),"Yellow"); } } 

From know that I do not use materialCons.build (); After designing and analyzing, I implemented MaterialCosntructor as follows:

  public class MaterialConstructor { private Helper1 helper1; private Helper2 helper2 MaterialConstructor (Helper1 help1, Helper2 help2){ ................. } public Material build(Double among){ part1=helper1.builpart(among); return helper2.construct(part1); } } 

In my first test class, I should include code like this:

 helper1=createMock(Helper1) helper2=createMock(Helper2) materialCons=new MaterialConstructor (helper1, helper2) ..................... expected(helper1.builpart(150)).andReturn(some result) expected(helper2.construct(some result)).andReturn("Yellow") 

as a result, we can get this updated class test:

  class MaterialTest{ MaterialConstructor materialCons public void testBuild(){ helper1=createMock(Helper1) helper2=createMock(Helper2) materialCons=new MaterialConstructor (helper1, helper2) expected(helper1.builpart(150)).andReturn(some result) expected(helper2.construct(some result)).andReturn("Yellow") materialCons.setAmount(150); Material material=materialCons.build(); assertEqual(material.getColor(),"Yellow"); } } 

so my test code will be updated after my source code is written!

This will appear a lot of time (since during the specification it is difficult to determine which dependency classes you should use to solve your problem). Then the unit-test class will always be deprecated after writing the source code!

+1
source share
2 answers

Following rigorous TDD workflows, such as the 3 laws of TDD , will definitely put an end to your vibrations.

With it, you cannot have things like "After designing and analyzing, I implemented MaterialCosntructor ...", since you should

Write down only enough production code to pass the test

Which basically means: just return "Yellow" as your first implementation.

Then you would add more tests (green, red material) and gradually create your own class to handle these additional cases.

+2
source

Not. I think your idea is absolutely brilliant to have unit test cases for testing objects. I don’t think the test class will become obsolete after writing the source code, because in your case you developed the class members and class methods. The only thing that seems to stop you is to write EasyMock test windows. Here is some help material, maybe it will make things more understandable for you IBM Developer Works modeling page .

There is also another hyperlink that EasyMock reads on its own EasyMock ReadMe

0
source

All Articles