views:

83

answers:

4

how to create generic functions that could be called from each java test? In my function startappli I have :

public class startappli{

public void testMain (String[] args) 
 {

  String[] logInfos = new String[3];
  logInfos[0] = (String) args[0];
  logInfos[1] = (String) args[1];
 }
  @BeforeClass
    public static void setupOnce() {
        final Thread thread = new Thread() {
            public void run() {

             entrypointtoGUI.main(new String[]{"arg0 ", "arg1"});

            }
        };
        try {
            thread.start();
        } catch (Exception ex) {

        }
    }

}

in the toto.java , I call the function as follow : startappli.testmain(loginfo) it doesn't work help ?

A: 

I assume your problem is that startappli.setupOnce() is not executed.

I believe this is because startappli contains no @Test methods, therefore JUnit does not take it as a test class. Thus @BeforeClass is omitted and the function is not executed by JUnit.

A solution could be to put a @Test method in this class. Or, if you want it to be called before each Java test method, you should call it explicitly from the @Before methods in each of your test classes (or from the @BeforeClass methods if you want it to run only once for each test class).

Note: startappli is not a good name for a Java class, as the convention is that class names should be camel case, e.g. StartAppli.

Péter Török
A: 

You are doing a number of things wrong (actually - all)

  1. The Java Naming Conventions say that class names should be uppercase

  2. You are calling testmain() and main() while the method is camelCased - testMain().

  3. You should not run a main method in a new thread with JUnit. A JUnit runner takes care of the instantiation.

  4. Your testclass should perform tests - i.e. it should have a method annotated with @Test

I'd suggest reading the JUnit manual before starting.

Bozho
A: 

my function: Runner.java contains : public class RunAppli {

@BeforeClass public static void setupOnce() { final Thread thread = new Thread() { public void run() {

            Main.main(new String[]{"-rtebase ", "C:\\bin"});

        }
    };
    try {
        thread.start();
    } catch (Exception ex) {

    }
}

@Test public void test() {

    URL path = this.getClass().getResource("../Tester/map.xml");


    System.out.println("Cover: " + cmapURL);




    }

}

}
and from my java test TestDemo.java , I call StartAppli THAT launch the appli GUI : RunAppli .setupOnce(); and I get the path to xml file : RunAppli .path should we use @Test in functions ? any suggession ? thanks

Please edit your question instead of adding an answer. Also: please make sure your code is actually highlighted as such (read the markdown help).
Joachim Sauer
A: 

my function: Runner.java contains : public class RunAppli {

@BeforeClass public static void setupOnce() { final Thread thread = new Thread() { public void run() {

            Main.main(new String[]{"-rtebase ", "C:\\bin"});

        }
    };
    try {
        thread.start();
    } catch (Exception ex) {

    }
}

@Test public void test() {

    URL path = this.getClass().getResource("../Tester/map.xml");


    System.out.println("Cover: " + cmapURL);




    }

}

}
and from my java test TestDemo.java , I call StartAppli THAT launch the appli GUI : RunAppli .setupOnce(); and I get the path to xml file : RunAppli .path should we use @Test in functions ? any suggession ? thanks

I have add more information about my problem