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() + "]"));
}
}
이 상단은 객체이고 클래스는 작업하지 않습니다. 이것이 제가 지금까지 가지고있는 것입니다.
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
클래스 를 가져올 필요가 없습니다 .
이상적 Module
와 ModuleTest
같은 패키지에 있지만 같은 폴더에 있어야합니다 : 테스트 및 응용 프로그램에 대한 다른 하나에 대해 하나.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다