junit - Is it possible to have a main method with a @Before and @Test method in the same class? Java -
this question has answer here:
i want pass commandline arguments java class junit test annotations, possible?
example:
@before public void before(string a, string b) { // processing } @test public void test() { } public static void main(string[] args) { if (args[0] != null) default_content_dir = args[0]; if (args[1] != null) default_fonts_dir = args[1]; if (args[2] != null) formmldir = args[2]; before(default_content_dir, default_fonts_dir); }
is possible?
possible? yes. want? not.
public class customemain { public static void main(string [] args) { customsetup(args); junitcore.main(new string [] {"sometest"}); } }
the problem no 1 invokes junit themselves. use ant, maven, eclipse, or other build tool. if case there custom main method won't called.
also, despite above comments, type of testing not inherently evil, nice avoid if possible if can rewrite application avoid complex interaction. don't skip testing interaction if can't rewrite it. if gives grief tell them integration test , using junit integration test framework.
likely, easiest solution use environment variables or jvm properties. half-decent build tool have ways supply this. rid of main method , change before method to...
@before public void before() { string contentdir = system.getenv("content_dir"); if (contentdir == null) { contentdir = default_content_dir; } //... }
Comments
Post a Comment