2016-11-22 6 views
2
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() + "]")); 
    } 

} 

このトップはオブジェクトであり、クラスはそれを使用していませんこれは私がこれまで持っていたものです。jUnitテストのエラーを取り除くことはできませんか?

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 + "]"; 
    } 

} 

トップコードにエラーが表示され続けると、モジュールをタイプヘルプに解決できません。

+4

'Module'をインポートしましたか? – resueman

+0

@resuemanこれは答えとして書き直すことができます...そしてたぶん簡単な例です(そして通常、テストクラスはテストされるクラスと同じパッケージにあります) –

答えて

1

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();はコンパイルには適していますが、この場合は可読性が低下します。だから、ModuleTestModuleクラスをインポート
がより適しているようだ:

とにかく
package lib; 

import static org.junit.Assert.*; 

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

public class ModuleTest { 

、IDEを使用している場合、あなたは自動的に不足している輸入を解決するためのオプションを持っている必要があります。だから、それを使ってください。
さらに、テストされたクラスと同じパッケージをテストクラスに使用することをお勧めします。
副作用として、クラスをModuleTestクラスにインポートする必要はありません。
理想的には、ModuleModuleTestは同じパッケージにありますが、同じフォルダには存在しません(テスト用とアプリケーション用の両方)。

0

ModuleTest.javaファイルに

import main.Module; 

を追加します。ここで

関連する問題