RE: Creating a FreeStyleProject outside JUnit?
My first approach was to simply: public class JenkinsApiTest { public void test() { ItemGroup itemGroup = new MyItemGroup(); FreeStyleProject p = new FreeStyleProject(itemGroup, "n0"); p.getConfigFile().asString(); } this results in the AbstractProject constructor being called: protected AbstractProject(ItemGroup parent, String name) { super(parent,name); if(!Jenkins.getInstance().getNodes().isEmpty()) { // if a new job is configured with Hudson that already has slave nodes // make it roamable by default canRoam = true; } } which throws a NullPointerException most likely because a Jenkins instance has not been created. I seems that creating types from hudson.model.* package requires a running Jenkins instance and hence the whole HudsonTestCase setup is necessary -- View this message in context: http://jenkins.361315.n4.nabble.com/Creating-a-FreeStyleProject-outside-JUnit-tp4651594p4651931.html Sent from the Jenkins users mailing list archive at Nabble.com.
RE: Creating a FreeStyleProject outside JUnit?
I think your problem is the manner in which you are trying to create the object: you're calling a utility method on HudsonTestCase which extends JUnit's TestCase class. HudsonTestCase assumes its being used from JUnit. -- Dean From: jenkinsci-users@googlegroups.com [jenkinsci-users@googlegroups.com] on behalf of jserup [johs.se...@gmail.com] Sent: Wednesday, January 16, 2013 3:43 AM To: jenkinsci-users@googlegroups.com Subject: Re: Creating a FreeStyleProject outside JUnit? Ok makes sense but is possible to create eg. a FreeStyleProject /outside/ a Junit TestRunner. Seems a bit strange that this can only be done through a TestRunner. -- View this message in context: http://jenkins.361315.n4.nabble.com/Creating-a-FreeStyleProject-outside-JUnit-tp4651594p4651665.html Sent from the Jenkins users mailing list archive at Nabble.com.
Re: Creating a FreeStyleProject outside JUnit?
Ok makes sense but is possible to create eg. a FreeStyleProject /outside/ a Junit TestRunner. Seems a bit strange that this can only be done through a TestRunner. -- View this message in context: http://jenkins.361315.n4.nabble.com/Creating-a-FreeStyleProject-outside-JUnit-tp4651594p4651665.html Sent from the Jenkins users mailing list archive at Nabble.com.
Re: Creating a FreeStyleProject outside JUnit?
IIRC, you need to use a Junit TestRunner to run the test, so that Junit's test lifecycle is followed. -- Dean On 1/15/13 11:22 AM, "jserup" wrote: >I am creating a FreeStyleProject using this test class: > >import org.jvnet.hudson.test.HudsonTestCase; >import org.apache.commons.io.FileUtils; >import hudson.model.*; >import hudson.tasks.Shell; > >public class AppTest extends HudsonTestCase >{ >public void test1() throws Exception { >FreeStyleProject project = createFreeStyleProject(); >project.getBuildersList().add(new Shell("echo hello")); > >FreeStyleBuild build = project.scheduleBuild2(0).get(); >System.out.println(build.getDisplayName()+" completed"); > >// TODO: change this to use HtmlUnit >String s = FileUtils.readFileToString(build.getLogFile()); >assertTrue(s.contains("+ echo hello")); >} >} > >From: > >https://wiki.jenkins-ci.org/display/JENKINS/Unit+Test > >But is it only possible to create a FreeStyleProject (and other project >types) from a JUnit test? If I do: > >public class Main { >public void runit() throws Exception{ >AppTest at = new AppTest(); >at.test1(); >} > >public static void main(String[] args) throws Exception { >Main main = new Main(); >main.runit(); >} >} > > >I get: > >Exception in thread "main" java.lang.NullPointerException >at >org.jvnet.hudson.test.HudsonTestCase.createUniqueProjectName(HudsonTestCas >e.java:688) >at >org.jvnet.hudson.test.HudsonTestCase.createFreeStyleProject(HudsonTestCase >.java:652) >at com.build.jenkins.AppTest.test1(AppTest.java:10) >at com.build.jenkins.Main.runit(MMain.java:7) >at com.build.jenkins.Main.main(MMain.java:13) > >I have also looked at: hudson.triggers.TriggerStartTest which use: > >@Rule public JenkinsRule j = new JenkinsRule(); > >instead. But moving that outside a Junit test class gives an error similar >to the above. > > >Its seems that its only possible to create objects from hudson.model.* >inside JUnit test or am I missing an alternative approach? > >Here are the dependencies that I use: > > >org.jenkins-ci.main >jenkins-core >1.498 > > > >org.jenkins-ci.main >jenkins-test-harness >1.498 > > > >org.jenkins-ci.main >1.498 >jenkins-war >war > > >org.jenkins-ci.main >1.498 >jenkins-war >war-for-test > > >available from : > >http://repo.jenkins-ci.org/webapp/browserepo.html?6 > > > >-- >View this message in context: >http://jenkins.361315.n4.nabble.com/Creating-a-FreeStyleProject-outside-JU >nit-tp4651594.html >Sent from the Jenkins users mailing list archive at Nabble.com.