我想制作具有相同变量但值不同的同一对象的单元测试套件。但是,如果对象具有相同的名称(由this.setName("testlaunch");
创建(我们必须具有JUnit测试的方法的名称),它只运行一个测试。
如果我不写this.setName("testlaunch");
,它会抱怨说junit.framework.AssertionFailedError: TestCase.fName cannot be null
。
我不知道该怎么做......
public class LanceurRegleGestion extends TestSuite { public static Test suite() { Class maClasse = null; TestSuite suite = new TestSuite(); String filtre = ".*.xml"; // on compile le pattern pour l'expression réguliere Pattern p = Pattern.compile(filtre); String path = "D:/Documents/workspace/Solipsisme/src/ReglesGestion/XML/"; // on liste les fichiers du repertoire String [] u = new File(path).list(); // on parcours la liste de fichier System.out.println("Initialisation"); for (int i=0; iet le code de l'objet serialisé
public class Application extends TestCase { private String nomappli; private String id2_1; private String id3_1; private String id4_1; private String id2_2; private String id3_2; private String id4_2; private String id5_2; private String id6_2; private String id7_2; private String id8_2; private String id9_2; private String id2_3; private String id3_3; private String id4_3; private String id2_4; private String id3_4; private String id4_4; private String id2_5; private String id3_5; private String id4_5; private String id5_5; private String id6_5; private String id7_5; private static Selenium selenium; public Application(String nomappli,String id2_1,String id3_1,String id4_1,String id2_2,String id3_2,String id4_2,String id5_2,String id6_2,String id7_2,String id8_2,String id9_2,String id2_3,String id3_3,String id4_3,String id2_4,String id3_4,String id4_4,String id2_5, String id3_5,String id4_5,String id5_5,String id6_5,String id7_5) { this.setName("testlaunch"); this.nomappli = nomappli; this.id2_1 = id2_1; this.id3_1 = id3_1; this.id4_1 = id4_1; this.id2_2 = id2_2; this.id3_2 = id3_2; this.id4_2 = id4_2; this.id5_2 = id5_2; this.id6_2 = id6_2; this.id7_2 = id7_2; this.id8_2 = id8_2; this.id9_2 = id9_2; this.id2_3 = id2_3; this.id3_3 = id3_3; this.id4_3 = id4_3; this.id2_4 = id2_4; this.id3_4 = id3_4; this.id4_4 = id4_4; this.id2_5 = id2_5; this.id3_5 = id3_5; this.id4_5 = id4_5; this.id5_5 = id5_5; this.id6_5 = id6_5; this.id7_5 = id7_5; } public Application(){ } public String toString() { return getNomappli(); } public void setNomappli(String nomappli) { this.nomappli = nomappli; } public String getNomappli() { return this.nomappli; } public void setId2_1(String id2_1) { this.id2_1 = id2_1; } public String getId2_1() { return this.id2_1; } public void setId3_1(String id3_1) { this.id3_1 = id3_1; } public String getId3_1() { return this.id3_1; } public void setId4_1(String id4_1) { this.id4_1 = id4_1; } public String getId4_1() { return this.id4_1; } public void setId2_2(String id2_2) { this.id2_2 = id2_2; } public String getId2_2() { return this.id2_2; } public void setId3_2(String id3_2) { this.id3_2 = id3_2; } public String getId3_2() { return this.id3_2; } public void setId4_2(String id4_2) { this.id4_2 = id4_2; } public String getId4_2() { return this.id4_2; } public void setId5_2(String id5_2) { this.id5_2 = id5_2; } public String getId5_2() { return this.id5_2; } public void setId6_2(String id6_2) { this.id6_2 = id6_2; } public String getId6_2() { return this.id6_2; } public void setId7_2(String id7_2) { this.id7_2 = id7_2; } public String getId7_2() { return this.id7_2; } public void setId8_2(String id8_2) { this.id8_2 = id8_2; } public String getId8_2() { return this.id8_2; } public void setId9_2(String id9_2) { this.id9_2 = id9_2; } public String getId9_2() { return this.id9_2; } public void setId2_3(String id2_3) { this.id2_3 = id2_3; } public String getId2_3() { return this.id2_3; } public void setId3_3(String id3_3) { this.id3_3 = id3_3; } public String getId3_3() { return this.id3_3; } public void setId4_3(String id4_3) { this.id4_3 = id4_3; } public String getId4_3() { return this.id4_3; } public void setId2_4(String id2_4) { this.id2_4 = id2_4; } public String getId2_4() { return this.id2_4; } public void setId3_4(String id3_4) { this.id3_4 = id3_4; } public String getId3_4() { return this.id3_4; } public void setId4_4(String id4_4) { this.id4_4 = id4_4; } public String getId4_4() { return this.id4_4; } public void setId2_5(String id2_5) { this.id2_5 = id2_5; } public String getId2_5() { return this.id2_5; } public void setId3_5( String id3_5) { this.id3_5 = id3_5; } public String getId3_5() { return this.id3_5; } public void setId4_5(String id4_5) { this.id4_5 = id4_5; } public String getId4_5() { return this.id4_5; } public void setId5_5(String id5_5) { this.id5_5 = id5_5; } public String getId5_5() { return this.id5_5; } public void setId6_5(String id6_5) { this.id6_5 = id6_5; } public String getId6_5() { return this.id6_5; } public void setId7_5(String id7_5) { this.id7_5 = id7_5; } public String getId7_5() { return this.id7_5; } public void setSelenium(Selenium selenium) { this.selenium = selenium; } public Selenium getSelenium() { return this.selenium; } public final static void login() { selenium.open("apj/ident"); selenium.type("username", "hsuzumiya-cp"); selenium.type("password", "1"); selenium.click("enterButton"); selenium.waitForPageToLoad("9999999"); } public void testlaunch() { generique(this.nomappli,this.id2_1,this.id3_1,this.id4_1,this.id2_2,this.id3_2,this.id4_2,this.id5_2,this.id6_2,this.id7_2,this.id8_2,this.id9_2,this.id2_3,this.id3_3,this.id4_3,this.id2_4,this.id3_4,this.id4_4,this.id2_5,this.id3_5,this.id4_5,this.id5_5,this.id6_5,this.id7_5); } public void setUp() throws Exception { System.out.println("Initialisation"); selenium = new DefaultSelenium("127.0.0.1",4444,"*iexplore", "http://hsuzumiya/"); selenium.start(); selenium.setTimeout("90000"); selenium.setSpeed("500"); login(); } public void generique(String nomappli,String id2_1,String id3_1,String id4_1,String id2_2,String id3_2,String id4_2, String id5_2,String id6_2,String id7_2,String id8_2,String id9_2,String id2_3,String id3_3,String id4_3,String id2_4, String id3_4,String id4_4,String id2_5, String id3_5,String id4_5,String id5_5,String id6_5,String id7_5 ) { System.out.println(nomappli); selenium.click("valider"); selenium.waitForPageToLoad("30000"); selenium.click("validertout"); } public final void tearDown() throws Exception { System.out.println("Killing session"); selenium.stop(); } }
答案 0 :(得分:6)
作为junit的新手,我偶然发现了这个问题,希望解决一个问题,我得到了同样的信息。通过进一步的研究,我发现有必要通过addTest将要调用的测试函数的名称传递给测试用例类的构造函数。下面是一个简单的(无用的,除了插图)示例:
<强> JunitTestCases.java 强>
import junit.framework.TestCase;
public class JunitTestCases extends TestCase {
public JunitTestCases(String fnName) {
super(fnName);
}
public void testA() {
assertTrue("assertTrue failed", true);
}
}
<强> JunitTestSuite.java:强>
import junit.framework.*;
public class JunitTestSuite {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new JunitTestCases("testA"));
return suite;
}
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
}
当我编译时:
javac -cp。: path / to / junit-X.X.X.jar JunitTestSuite.java
并用
运行java -cp。: path / to / junit-X.X.X.jar JunitTestSuite
这没有任何错误,junit给了我一个好消息。