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(HudsonTestCase.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: 

                <dependency>
                        <groupId>org.jenkins-ci.main</groupId>
                        <artifactId>jenkins-core</artifactId>
                        <version>1.498</version>
                </dependency>

                <dependency>
                        <groupId>org.jenkins-ci.main</groupId>
                        <artifactId>jenkins-test-harness</artifactId>
                        <version>1.498</version>
                </dependency>

                <dependency>
                        <groupId>org.jenkins-ci.main</groupId>
                        <version>1.498</version>
                        <artifactId>jenkins-war</artifactId>
                        <type>war</type>
                </dependency>
                <dependency>
                        <groupId>org.jenkins-ci.main</groupId>
                        <version>1.498</version>
                        <artifactId>jenkins-war</artifactId>
                        <classifier>war-for-test</classifier>
                </dependency>

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-JUnit-tp4651594.html
Sent from the Jenkins users mailing list archive at Nabble.com.

Reply via email to