【问题标题】:JUnit test of the same object同一对象的 JUnit 测试
【发布时间】:2011-03-26 04:32:15
【问题描述】:

我想为具有相同变量但值不同的同一对象制作一个单元测试套件。但是,如果对象具有相同的名称(由this.setName("testlaunch"); 创建(我们必须具有由 JUnit 测试的方法的名称),则它只运行一个测试。

如果我不写this.setName("testlaunch");,它会抱怨说junit.framework.AssertionFailedError: TestCase.fName cannot be null。 我不知道该怎么办...

公共类 LanceurRegleGestion 扩展了 TestSuite { 公共静态测试套件() { 类 maClasse = null; TestSuite 套件 = 新的 TestSuite(); 字符串过滤器 = ".*.xml"; // on compile le pattern pour l'expression réguliere 模式 p = Pattern.compile(filter); 字符串路径 = "D:/Documents/workspace/Solipsisme/src/ReglesGestion/XML/"; // 关于曲目列表 String [] u = new File(path).list(); // 在 parcours la liste de fichier System.out.println("初始化"); 对于 (int i=0; 我

et le code de l'objet serialisé

公共类应用程序扩展TestCase { 私有字符串 nomappli; 私有字符串 id2_1; 私有字符串 id3_1; 私有字符串 id4_1; 私人字符串 id2_2; 私有字符串 id3_2; 私有字符串 id4_2; 私有字符串 id5_2; 私有字符串 id6_2; 私有字符串 id7_2; 私有字符串 id8_2; 私人字符串 id9_2; 私有字符串 id2_3; 私有字符串 id3_3; 私有字符串 id4_3; 私有字符串 id2_4; 私有字符串 id3_4; 私有字符串 id4_4; 私有字符串 id2_5; 私有字符串 id3_5; 私有字符串 id4_5; 私有字符串 id5_5; 私有字符串 id6_5; 私有字符串 id7_5; 私有静态硒硒; 公共应用程序(字符串 nomappli,字符串 id2_1,字符串 id3_1,字符串 id4_1,字符串 id2_2,字符串 id3_2,字符串 id4_2,字符串 id5_2,字符串 id6_2,字符串 id7_2,字符串 id8_2,字符串 id9_2,字符串 id2_3,字符串 id3_3,字符串 id4_3,字符串 id2_4 ,字符串 id3_4,字符串 id4_4,字符串 id2_5,字符串 id3_5,字符串 id4_5,字符串 id5_5,字符串 id6_5,字符串 id7_5) { this.setName("testlaunch"); this.nomappli = nomappli; 这个.id2_1 = id2_1; 这.id3_1 = id3_1; this.id4_1 = id4_1; 这.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; 这.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; } 公共应用程序(){ } 公共字符串 toString() { 返回 getNomappli(); } 公共无效 setNomappli(字符串 nomappli) { this.nomappli = nomappli; } 公共字符串 getNomappli() { 返回这个.nomappli; } 公共无效 setId2_1(字符串 id2_1) { 这个.id2_1 = id2_1; } 公共字符串 getId2_1() { 返回 this.id2_1; } 公共无效 setId3_1(字符串 id3_1) { 这.id3_1 = id3_1; } 公共字符串 getId3_1() { 返回 this.id3_1; } 公共无效 setId4_1(字符串 id4_1) { this.id4_1 = id4_1; } 公共字符串 getId4_1() { 返回 this.id4_1; } 公共无效 setId2_2(字符串 id2_2) { 这.id2_2 = id2_2; } 公共字符串 getId2_2() { 返回 this.id2_2; } 公共无效 setId3_2(字符串 id3_2) { this.id3_2 = id3_2; } 公共字符串 getId3_2() { 返回 this.id3_2; } 公共无效 setId4_2(字符串 id4_2) { this.id4_2 = id4_2; } 公共字符串 getId4_2() { 返回 this.id4_2; } 公共无效 setId5_2(字符串 id5_2) { this.id5_2 = id5_2; } 公共字符串 getId5_2() { 返回 this.id5_2; } 公共无效 setId6_2(字符串 id6_2) { this.id6_2 = id6_2; } 公共字符串 getId6_2() { 返回 this.id6_2; } 公共无效 setId7_2(字符串 id7_2) { this.id7_2 = id7_2; } 公共字符串 getId7_2() { 返回 this.id7_2; } 公共无效 setId8_2(字符串 id8_2) { this.id8_2 = id8_2; } 公共字符串 getId8_2() { 返回 this.id8_2; } 公共无效 setId9_2(字符串 id9_2) { this.id9_2 = id9_2; } 公共字符串 getId9_2() { 返回 this.id9_2; } 公共无效 setId2_3(字符串 id2_3) { this.id2_3 = id2_3; } 公共字符串 getId2_3() { 返回 this.id2_3; } 公共无效 setId3_3(字符串 id3_3) { this.id3_3 = id3_3; } 公共字符串 getId3_3() { 返回 this.id3_3; } 公共无效 setId4_3(字符串 id4_3) { this.id4_3 = id4_3; } 公共字符串 getId4_3() { 返回 this.id4_3; } 公共无效 setId2_4(字符串 id2_4) { this.id2_4 = id2_4; } 公共字符串 getId2_4() { 返回 this.id2_4; } 公共无效 setId3_4(字符串 id3_4) { this.id3_4 = id3_4; } 公共字符串 getId3_4() { 返回 this.id3_4; } 公共无效 setId4_4(字符串 id4_4) { this.id4_4 = id4_4; } 公共字符串 getId4_4() { 返回 this.id4_4; } 公共无效 setId2_5(字符串 id2_5) { 这.id2_5 = id2_5; } 公共字符串 getId2_5() { 返回 this.id2_5; } 公共无效 setId3_5(字符串 id3_5) { this.id3_5 = id3_5; } 公共字符串 getId3_5() { 返回 this.id3_5; } 公共无效 setId4_5(字符串 id4_5) { this.id4_5 = id4_5; } 公共字符串 getId4_5() { 返回 this.id4_5; } 公共无效 setId5_5(字符串 id5_5) { this.id5_5 = id5_5; } 公共字符串 getId5_5() { 返回 this.id5_5; } 公共无效 setId6_5(字符串 id6_5) { this.id6_5 = id6_5; } 公共字符串 getId6_5() { 返回 this.id6_5; } 公共无效 setId7_5(字符串 id7_5) { this.id7_5 = id7_5; } 公共字符串 getId7_5() { 返回 this.id7_5; } 公共无效集硒(硒硒) { this.selenium = 硒; } 公共硒 getSelenium() { 返回 this.selenium; } 公共最终静态无效登录() { selenium.open("apj/ident"); selenium.type("用户名", "hsuzumiya-cp"); selenium.type("密码", "1"); selenium.click("enterButton"); selenium.waitForPageToLoad("9999999"); } 公共无效测试启动() { 通用(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); } 公共 void setUp() 抛出异常 { System.out.println("初始化"); selenium = new DefaultSelenium("127.0.0.1",4444,"*iexplore", "http://hsuzumiya/"); selenium.start(); selenium.setTimeout("90000"); selenium.setSpeed("500"); 登录(); } 公共无效通用(字符串 nomappli,字符串 id2_1,字符串 id3_1,字符串 id4_1,字符串 id2_2,字符串 id3_2,字符串 id4_2, 字符串 id5_2,字符串 id6_2,字符串 id7_2,字符串 id8_2,字符串 id9_2,字符串 id2_3,字符串 id3_3,字符串 id4_3,字符串 id2_4, 字符串 id3_4、字符串 id4_4、字符串 id2_5、字符串 id3_5、字符串 id4_5、字符串 id5_5、字符串 id6_5、字符串 id7_5 ) { System.out.println(nomappli); selenium.click("验证器"); selenium.waitForPageToLoad("30000"); selenium.click("validertout"); } public final void tearDown() 抛出异常 { System.out.println("杀死会话"); 硒。停止(); } }

【问题讨论】:

  • 很难理解您对问题的描述。您可以发布失败的断言的堆栈跟踪吗?

标签: java serialization object junit


【解决方案1】:

作为 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 给了我一个 OK 消息。

【讨论】:

    猜你喜欢
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多