java - 我无法从 jUnit 测试中删除错误?

标签 java

package lib;

import static org.junit.Assert.*;

import org.junit.Test;

public class ModuleTest {

    /* Both the default and 2 argument custom constructor should initialise the coursework
     * and exam weighting to 50.
     */
    @Test
    public void testDefaultConstructor() {
        Module m = new Module();

        //default modules should get a code of CTEC0000
        assertEquals("Code field should be initialised with CTEC0000", "CTEC0000", m.getCode());
        assertEquals("Name field should be initialised with an empty string", "", m.getName());
        assertEquals("Exam weight field should be initialised to 50", 50, m.getExamWeight());
        assertEquals("Cwk weight field should be initialised to 50", 50, m.getCwkWeight());
    }

    @Test
    public void testCustomConstructor2arg() {
        Module m = new Module("CTEC2602", "OO Development");

        assertEquals("Code field should be initialised with CTEC2602", "CTEC2602", m.getCode());
        assertEquals("Name field should be initialised with OO Development", "OO Development", m.getName());
        assertEquals("Exam weight field should be initialised to 50", 50, m.getExamWeight());
        assertEquals("Cwk weight field should be initialised to 50", 50, m.getCwkWeight());
    }

    /* The 3 argument custom constructor should accept a value for the exam weighting and then ensure
     * the coursework weighting is set so their combined total add up to 100. The following two tests check
     * this works correctly. One test is not sufficient as the value could have been hardcoded.
     */
    @Test
    public void testCustomConstructor3args_1() {
        Module m = new Module("CTEC2602", "OO Development", 60);

        assertEquals("Code field should be initialised with CTEC2602", "CTEC2602", m.getCode());
        assertEquals("Name field should be initialised with OO Development", "OO Development", m.getName());
        assertEquals("Exam weight field should be initialised to 60", 60, m.getExamWeight());
        assertEquals("Cwk weight field should be initialised to 40", 40, m.getCwkWeight());
    }

    @Test
    public void testCustomConstructor3args_2() {
        Module m = new Module("CTEC2901", "Data Structures", 70);

        assertEquals("Code field should be initialised with CTEC2901", "CTEC2901", m.getCode());
        assertEquals("Name field should be initialised with Data Structures", "Data Structures", m.getName());
        assertEquals("Exam weight field should be initialised to 70", 70, m.getExamWeight());
        assertEquals("Cwk weight field should be initialised to 30", 30, m.getCwkWeight());
    }

    @Test
    public void testSetAndGetCode() {
        Module m = new Module();
        m.setCode("CTEC2602");

        assertEquals("Code field should be set to and return CTEC2602", "CTEC2602", m.getCode());
    }

    @Test
    public void testSetAndGetName() {
        Module m = new Module();
        m.setName("OO Development");

        assertEquals("Name field should be set to and return OO Development", "OO Development", m.getName());
    }

    /* As well as testing the set/get methods for exam and cwk weight behave in a normal way, we also
     * need to ensure they check that the combined exam and cwk weighting adds up to 100.
     */
    @Test
    public void testSetAndGetExamWeight() {
        Module m = new Module();
        m.setExamWeight(60);

        assertEquals("Exam weight field should be set to and return 60", 60, m.getExamWeight());
    }

    @Test
    public void testSetExamWeightUpdatingCwkWeight() {
        Module m = new Module();
        m.setExamWeight(60);

        assertEquals("Cwk weight field should be set to 40", 40, m.getCwkWeight());
    }

    @Test
    public void testSetAndGetCwkWeight() {
        Module m = new Module();
        m.setCwkWeight(70);

        assertEquals("Cwk weight field should be set to and return 70", 70, m.getCwkWeight());
    }

    @Test
    public void testSetCwkWeightUpdatingExamWeight() {
        Module m = new Module();
        m.setCwkWeight(70);

        assertEquals("Exam weight field should be set to 30", 30, m.getExamWeight());
    }

    @Test
    public void testToString() {
        Module m = new Module("CTEC2602", "OO Development");
        String toStr = m.toString();

        assertTrue("The toString method should be in the standard convention format",
                toStr.startsWith("Module:[") &&
                toStr.contains("=" + m.getCode() + ", ") &&
                toStr.contains("=" + m.getName() + ", ") &&
                toStr.contains("=" + m.getExamWeight() + ", ") &&
                toStr.endsWith("=" + m.getCwkWeight() + "]"));
    }

}

这个 top 是对象,类没有使用它这是我目前所拥有的。

package main;

public class Module {

    //Fields
    private String code;
    private String name;
    private int examWeight;
    private int cwkWeight;


    //Constructors
    public Module() {
        this("CTEC0000", "", 50);
    }

    public Module(String code, String name) {
        this(code, name, 50);
    }

    public Module(String code, String name, int examWeight) {
        this.code = code;
        this.name = name;
        this.examWeight = examWeight;
        this.cwkWeight = 100 - examWeight;
    }


    //Methods
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

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

    public int getExamWeight() {
        return examWeight;
    }

    public void setExamWeight(int examWeight) {
        this.examWeight = examWeight;
        this.cwkWeight = 100 - examWeight;
    }

    public int getCwkWeight() {
        return cwkWeight;
    }

    public void setCwkWeight(int cwkWeight) {
        this.cwkWeight = cwkWeight;
        this.examWeight = 100 - cwkWeight;
    }

    @Override
    public String toString() {
        return "Module:[code=" + code + ", name=" + name +
                ", examWeight=" + examWeight + ", cwkWeight=" + cwkWeight + "]";
    }

}

我一直在顶部代码中收到错误,说模块无法解析为类型帮助。

最佳答案

这里:

package lib;

import static org.junit.Assert.*;

import org.junit.Test;

public class ModuleTest {

main.Module 类未导入。
所以输入 Module m = new Module(); 编译器无法识别没有完整限定名的。

main.Module m = new main.Module(); 对于编译是正确的,但在这种情况下它降低了可读性。
所以导入 Module ModuleTest 中的类似乎更合适:

package lib;

import static org.junit.Assert.*;

import org.junit.Test;
import main.Module;

public class ModuleTest {

无论如何,如果您使用 IDE,您应该可以选择自动解决丢失的导入。所以,使用它吧。
此外,一个好的做法是为测试类使用与被测试类相同的包。
作为副作用,您不需要在 中导入 Module 类ModuleTest 类。
理想情况下,ModuleModuleTest 应该在同一个包中,但不在同一个文件夹中:一个用于测试,另一个用于应用程序。

关于java - 我无法从 jUnit 测试中删除错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40743553/

相关文章:

java - Apache Tomcat 和 Apache TomEE 服务器未在 Eclipse 中运行 Web 项目

java - 有没有一种简单的方法可以让 Apache Tomcat 在部署后自动重启?

java - 获取文件路径的相对路径?

java - Java 递归前缀解析器阶乘

java - 不缓存的Guava Cache

java - 有什么办法可以去掉两端多余的 0's from the end of array when using Arrays.toString() and also the square brackets ' [ ]' 吗?

java - "cascading" hibernate 实体监听器?

Java默认编译目录

java - Java对象的并发

java - if 语句产生不正确的结果