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

為{幸葍}努か 提交于 2019-12-08 05:40:26

问题


I'l will take an example:
requirement: construct a piece of material following these rule:
among given < 100 => material is green
100 < among <300=> material is yellow
among > 300=> material is red

My test class:

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

From know i don't how to implemented materialCons.build(); After design and analysis i've implemented MaterialCosntructor as follow:

    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 initial test class i must includes 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 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");

          }
            }

hence my test code will be update after writen my source code!

This will appear many time ( since it's difficult at specification time to know which dependencies class you must use to solve your problem). Then unit-test class will be always obsolete after written source code!


回答1:


Following a strict TDD workflow such as the 3 Laws of TDD would definitely put an end to your hesitations.

With it you can't have such things as "After design and analysis i've implemented MaterialCosntructor ..." since you are supposed to

Write only enough production code to pass a test

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

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




回答2:


No I think your idea is absolutely brilliant to have unit test cases for testing objects. I do not think the test class will be obsolete after written source code because in your case you have designed the class members and class methods.. the only thing that seems to be stopping you is to write the EasyMock testcases.. Here is some help material, maybe it will make things more clear for you IBM Developer Works Simulation Page ..

There is also one more hyperlink which is by EasyMock readme themselves EasyMock ReadMe



来源:https://stackoverflow.com/questions/14538910/how-to-write-test-code-before-write-source-code-when-they-are-objects-dependenci

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!