Junit测试方法返回语句" void类型不允许"

时间:2016-01-15 17:29:59

标签: java unit-testing junit bluej

我在Junit测试类中有一个方法,它测试电子邮件是否已成功更改,问题是它返回了一条错误消息,说明" void类型不允许"我不确定为什么。

以下是测试电子邮件是否已成功更改但返回上述错误的方法。

  @Test
public void testChangeEmail()
{
    assertEquals("001", teacher.getEmail());
    assertEquals("002", teacher.changeEmail("002"));
    assertEquals("002", teacher.getEmail());

}

这是全班,

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class TeacherTest
{
    private Teacher teacher;

    /**
     * Default constructor for test class TeacherTest
     */
    public TeacherTest()
    {
    }

    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
    @Before
    public void setUp()
    {
        teacher = new Teacher("Amy Blunt", "001", "MNHA");
    }

    /**
     * Tears down the test fixture.
     *
     * Called after every test case method.
     */
    @After
    public void tearDown()
    {
    }

    @Test
    public void testGetName()
    {
        assertEquals("Amy Blunt", teacher.getName());
    }

    @Test
    public void testMembership()
    {
        assertEquals("001", teacher.getEmail());
    }


    @Test
    public void testQualifications()
    {
        assertEquals("MNHA", teacher.getQualifications());
    }

    @Test
    public void testChangeEmail()
    {
        assertEquals("001", teacher.getEmail());
        assertEquals("002", teacher.changeEmail("002"));
        assertEquals("002", teacher.getEmail());

    }

}

2 个答案:

答案 0 :(得分:3)

我猜你的'http://localhost:63342/'方法实际上并没有返回任何内容。

而不是teacher.changeEmail();只需致电assertEquals("002", teacher.changeEmail("002"));。下一个断言检查以确保电子邮件已正确更新。

teacher.changeEmail("002");

答案 1 :(得分:0)

您的方法teacher.changeEmail(“002”)返回一些值?如果没有,请更换:
      assertEquals(“002”,teacher.changeEmail(“002”));
      为此:
      teacher.changeEmail( “002”)