tw-oocamp-201512 / questions

上课期间各种疑问可以发在这个repo的issues里,方便大家讨论,随时想到随时写
8 stars 0 forks source link

当我们写单元测试的时候如果测试对象与另一个对象具有相同行为(实现的接口相同),是否可以使用同一个测试来测试相同的部分,而对于特殊的部分另外再写测试。 #32

Open sunjintw opened 8 years ago

sunjintw commented 8 years ago

例如

    interface People{
        String eat();
    }

    public class Man implements People {
        public boolean isDrinkWine;
        public String eat(){
            isDrinkWine = true;
            return "I can't eat any more; I'm full up.";
        }
        public String gaming(){
            return "I like gaming."
        }
    }

    public class Women implements People {
        public boolean isDrinkJuice;
        public String eat(){
            isDrinkJuice = true;
            return "I can't eat any more; I'm full up.";
        }
        public String dancing(){
            return "I like dancing."
        }
    }

    @Test
    public void testPeopleEat(){
        People people = new Man();
        assertEquals("I can't eat any more; I'm full up.", people.eat());
        people = new Women();
        assertEquals("I can't eat any more; I'm full up.", people.eat());
    }

    @Test
    public void testManGaming(){
        Man man = new Man();
        assertEquals("I like gaming.", man.gaming());
    }

    @Test
    public void testWomenDancing(){
        Women women = new Women();
        assertEquals("I like dancing.", women.dancing())
    }

代码仅供说明问题