3
votes

I want to make a unit test suite of the same object with same variable but different values. However if the object get the same name (created by this.setName("testlaunch"); (we must have the name of a method tested by JUnit), it runs only one test.

If i don't write this.setName("testlaunch"); it complains saying junit.framework.AssertionFailedError: TestCase.fName cannot be null. I don't know what to do...

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; i

et 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(); } }
1
It is hard to follow your description of the problem. Can you post the stack trace for the assertion that is failing?Paul Jowett

1 Answers

5
votes

Being new to junit, I stumbled upon this question hoping to solve a problem I had getting the same message. Through further research, I found that it is necessary to pass the name of the test function you want to invoke through addTest to the constructor of the test case class. A simple (and useless, other than illustration) example follows:

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());
    }
}

When I compiled with:

javac -cp .:path/to/junit-X.X.X.jar JunitTestSuite.java

and ran with

java -cp .:path/to/junit-X.X.X.jar JunitTestSuite

this worked with no errors, with junit giving me an OK message.