Eclipse Junit正确为吸气剂和吸气剂编写测试

时间:2019-09-30 18:43:03

标签: java eclipse junit junit4

我需要在Eclipse中编写Junit测试,我想知道自己是否走对了。我有一个Allergey.java和TestAllergey.java。我听说您不应该对吸气剂和吸气剂进行测试,但这是一项要求,我别无选择。我也对toString方法进行了测试,并且很好奇我是否正确地进行了测试。所有测试都通过了。 谢谢

package medical.com.medicalApplication.model;
/**
 * This class represent the Allergy model in the application
 *
 */
public class Allergey {
    private String name;

    public Allergey(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Allergy " + name;
    }   

}

package medicalApplication.model;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import medical.com.medicalApplication.model.Allergey;

public class AllergeyTest {

    private Allergey allergy;

    @Before
    public void setUp() throws Exception {
        allergy = new Allergey("Peanut");
    }

    @Test
    public void testGetName() {
        String expectedValue = allergy.getName();
        String actualValue = "Peanut";
        assertTrue(expectedValue.equals(actualValue));
    }

    @Test
    public void testSetName() {
        String expectedValue = "Peanut";
        allergy.setName(expectedValue);
        String actualValue = allergy.getName();
        assertTrue(expectedValue.equals(actualValue));
    }

    @Test
    public void testToString() {
        assertTrue(allergy.toString().equals("Allergy " + allergy.getName()));
    }

}

0 个答案:

没有答案