Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing Clojure in Maven

I am new at Maven and even newer at Clojure. As an exercise to learn the language, I am writing a spider solitaire player program. I also plan on writing a similar program in Scala to compare the implementations (see my post https://stackoverflow.com/questions/2571267/modern-java-alternatives-closed).

I have configured a Maven directory structure containing the usual src/main/clojure and src/test/clojure directories. My pom.xml file includes the clojure-maven-plugin. When I run "mvn test", it displays "No tests to run", despite my having test code in the src/test/clojure directory. As I misnaming something? Here is my pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>SpiderPlayer</groupId>
    <artifactId>SpiderPlayer</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <inceptionYear>2010</inceptionYear>

    <packaging>jar</packaging>

    <properties>
        <maven.build.timestamp.format>yyMMdd.HHmm</maven.build.timestamp.format>
        <main.dir>org/dogdaze/spider_player</main.dir>
        <main.package>org.dogdaze.spider_player</main.package>
        <main.class>${main.package}.Main</main.class>
    </properties>

    <build>
        <sourceDirectory>src/main/clojure</sourceDirectory>
        <testSourceDirectory>src/test/clojure</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>com.theoryinpractise</groupId>
                <artifactId>clojure-maven-plugin</artifactId>
                <version>1.3.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <tasks>
                                <echo file="${project.build.sourceDirectory}/${main.dir}/Version.clj"
                                      message="(ns ${main.package})${line.separator}"/>
                                <echo file="${project.build.sourceDirectory}/${main.dir}/Version.clj" append="true"
                                      message="(def version &quot;${maven.build.timestamp}&quot;)${line.separator}"/>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                            <archive>
                                <manifest>
                                    <mainClass>${main.class}</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <redirectTestOutputToFile>true</redirectTestOutputToFile>
                    <skipTests>false</skipTests>
                    <skip>false</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>surefire-it</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.2</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

Here is my Clojure source file (src/main/clojure/org/dogdaze/spider_player/Deck.clj):

; Copyright 2010 Dogdaze

(ns org.dogdaze.spider_player.Deck
  (:use
    [clojure.contrib.seq-utils
     :only (shuffle)]))

(def suits [:clubs :diamonds :hearts :spades])
(def ranks [:ace :two :three :four :five :six :seven :eight :nine :ten :jack :queen :king])

(defn suit-seq
  "Return 4 suits:
  if number-of-suits == 1: :clubs :clubs :clubs :clubs
  if number-of-suits == 2: :clubs :diamonds :clubs :diamonds
  if number-of-suits == 4: :clubs :diamonds :hearts :spades."
  [number-of-suits]
  (take 4 (cycle (take number-of-suits suits))))

(defstruct card :rank :suit)

(defn unshuffled-deck
  "Create an unshuffled deck containing all cards from the number of suits specified."
  [number-of-suits]
  (for
    [rank ranks suit (suit-seq number-of-suits)]
    (struct card rank suit)))

(defn deck
  "Create a shuffled deck containing all cards from the number of suits specified."
  [number-of-suits]
  (shuffle (unshuffled-deck number-of-suits)))

Here is my test case (src/test/clojure/org/dogdaze/spider_player/TestDeck.clj):

; Copyright 2010 Dogdaze

(ns org.dogdaze.spider_player
  (:use
    clojure.set
    clojure.test
    org.dogdaze.spider_player.Deck))

(deftest test-suit-seq
  (is (= (suit-seq 1) [:clubs :clubs :clubs :clubs]))
  (is (= (suit-seq 2) [:clubs :diamonds :clubs :diamonds]))
  (is (= (suit-seq 4) [:clubs :diamonds :hearts :spades])))

(def one-suit-deck
  [{:rank :ace, :suit :clubs} {:rank :ace, :suit :clubs} {:rank :ace, :suit :clubs} {:rank :ace, :suit :clubs}
   {:rank :two, :suit :clubs} {:rank :two, :suit :clubs} {:rank :two, :suit :clubs} {:rank :two, :suit :clubs}
   {:rank :three, :suit :clubs} {:rank :three, :suit :clubs} {:rank :three, :suit :clubs} {:rank :three, :suit :clubs}
   {:rank :four, :suit :clubs} {:rank :four, :suit :clubs} {:rank :four, :suit :clubs} {:rank :four, :suit :clubs}
   {:rank :five, :suit :clubs} {:rank :five, :suit :clubs} {:rank :five, :suit :clubs} {:rank :five, :suit :clubs}
   {:rank :six, :suit :clubs} {:rank :six, :suit :clubs} {:rank :six, :suit :clubs} {:rank :six, :suit :clubs}
   {:rank :seven, :suit :clubs} {:rank :seven, :suit :clubs} {:rank :seven, :suit :clubs} {:rank :seven, :suit :clubs}
   {:rank :eight, :suit :clubs} {:rank :eight, :suit :clubs} {:rank :eight, :suit :clubs} {:rank :eight, :suit :clubs}
   {:rank :nine, :suit :clubs} {:rank :nine, :suit :clubs} {:rank :nine, :suit :clubs} {:rank :nine, :suit :clubs}
   {:rank :ten, :suit :clubs} {:rank :ten, :suit :clubs} {:rank :ten, :suit :clubs} {:rank :ten, :suit :clubs}
   {:rank :jack, :suit :clubs} {:rank :jack, :suit :clubs} {:rank :jack, :suit :clubs} {:rank :jack, :suit :clubs}
   {:rank :queen, :suit :clubs} {:rank :queen, :suit :clubs} {:rank :queen, :suit :clubs} {:rank :queen, :suit :clubs}
   {:rank :king, :suit :clubs} {:rank :king, :suit :clubs} {:rank :king, :suit :clubs} {:rank :king, :suit :clubs}])

(def two-suits-deck
  [{:rank :ace, :suit :clubs} {:rank :ace, :suit :diamonds} {:rank :ace, :suit :clubs} {:rank :ace, :suit :diamonds}
   {:rank :two, :suit :clubs} {:rank :two, :suit :diamonds} {:rank :two, :suit :clubs} {:rank :two, :suit :diamonds}
   {:rank :three, :suit :clubs} {:rank :three, :suit :diamonds} {:rank :three, :suit :clubs} {:rank :three, :suit :diamonds}
   {:rank :four, :suit :clubs} {:rank :four, :suit :diamonds} {:rank :four, :suit :clubs} {:rank :four, :suit :diamonds}
   {:rank :five, :suit :clubs} {:rank :five, :suit :diamonds} {:rank :five, :suit :clubs} {:rank :five, :suit :diamonds}
   {:rank :six, :suit :clubs} {:rank :six, :suit :diamonds} {:rank :six, :suit :clubs} {:rank :six, :suit :diamonds}
   {:rank :seven, :suit :clubs} {:rank :seven, :suit :diamonds} {:rank :seven, :suit :clubs} {:rank :seven, :suit :diamonds}
   {:rank :eight, :suit :clubs} {:rank :eight, :suit :diamonds} {:rank :eight, :suit :clubs} {:rank :eight, :suit :diamonds}
   {:rank :nine, :suit :clubs} {:rank :nine, :suit :diamonds} {:rank :nine, :suit :clubs} {:rank :nine, :suit :diamonds}
   {:rank :ten, :suit :clubs} {:rank :ten, :suit :diamonds} {:rank :ten, :suit :clubs} {:rank :ten, :suit :diamonds}
   {:rank :jack, :suit :clubs} {:rank :jack, :suit :diamonds} {:rank :jack, :suit :clubs} {:rank :jack, :suit :diamonds}
   {:rank :queen, :suit :clubs} {:rank :queen, :suit :diamonds} {:rank :queen, :suit :clubs} {:rank :queen, :suit :diamonds}
   {:rank :king, :suit :clubs} {:rank :king, :suit :diamonds} {:rank :king, :suit :clubs} {:rank :king, :suit :diamonds}])

(def four-suits-deck
  [{:rank :ace, :suit :clubs} {:rank :ace, :suit :diamonds} {:rank :ace, :suit :hearts} {:rank :ace, :suit :spades}
   {:rank :two, :suit :clubs} {:rank :two, :suit :diamonds} {:rank :two, :suit :hearts} {:rank :two, :suit :spades}
   {:rank :three, :suit :clubs} {:rank :three, :suit :diamonds} {:rank :three, :suit :hearts} {:rank :three, :suit :spades}
   {:rank :four, :suit :clubs} {:rank :four, :suit :diamonds} {:rank :four, :suit :hearts} {:rank :four, :suit :spades}
   {:rank :five, :suit :clubs} {:rank :five, :suit :diamonds} {:rank :five, :suit :hearts} {:rank :five, :suit :spades}
   {:rank :six, :suit :clubs} {:rank :six, :suit :diamonds} {:rank :six, :suit :hearts} {:rank :six, :suit :spades}
   {:rank :seven, :suit :clubs} {:rank :seven, :suit :diamonds} {:rank :seven, :suit :hearts} {:rank :seven, :suit :spades}
   {:rank :eight, :suit :clubs} {:rank :eight, :suit :diamonds} {:rank :eight, :suit :hearts} {:rank :eight, :suit :spades}
   {:rank :nine, :suit :clubs} {:rank :nine, :suit :diamonds} {:rank :nine, :suit :hearts} {:rank :nine, :suit :spades}
   {:rank :ten, :suit :clubs} {:rank :ten, :suit :diamonds} {:rank :ten, :suit :hearts} {:rank :ten, :suit :spades}
   {:rank :jack, :suit :clubs} {:rank :jack, :suit :diamonds} {:rank :jack, :suit :hearts} {:rank :jack, :suit :spades}
   {:rank :queen, :suit :clubs} {:rank :queen, :suit :diamonds} {:rank :queen, :suit :hearts} {:rank :queen, :suit :spades}
   {:rank :king, :suit :clubs} {:rank :king, :suit :diamonds} {:rank :king, :suit :hearts} {:rank :king, :suit :spades}])

(deftest test-unshuffled-deck
  (is (= (unshuffled-deck 1) one-suit-deck))
  (is (= (unshuffled-deck 2) two-suits-deck))
  (is (= (unshuffled-deck 4) four-suits-deck)))

(deftest test-shuffled-deck
  (is (= (set (deck 1)) (set one-suit-deck)))
  (is (= (set (deck 2)) (set two-suits-deck)))
  (is (= (set (deck 4)) (set four-suits-deck))))

(run-tests)

Any idea why the test is not running? BTW, feel free to suggest improvements to the Clojure code.

Thanks, Ralph

like image 253
Ralph Avatar asked Apr 04 '10 14:04

Ralph


3 Answers

The key bit you're missing from your pom.xml (just cribbing from the clojure-contrib pom.xml) is an execution under the clojure-maven-plugin:

<plugin>
    <groupId>com.theoryinpractise</groupId>
    <artifactId>clojure-maven-plugin</artifactId>
    <version>1.3.2</version>
    <!-- Current Config -->
    <executions>
        <!-- ... -->
        <execution>
            <id>test-clojure</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
</plugin>

It may also be necessary to add something like this under <build/>:

<testResources>
    <testResource>
      <directory>src/test/clojure</directory>
    </testResource>
</testResources>
like image 77
ig0774 Avatar answered Oct 16 '22 00:10

ig0774


I would not expect the surefire plugin to run something else than Java tests so, with your current setup, there is indeed "No tests to run". I suggest to use the clojure-maven-plugin here. Also check Why using Maven for Clojure builds is a no-brainer.

like image 37
Pascal Thivent Avatar answered Oct 16 '22 00:10

Pascal Thivent


You can look to this example of pom.xml with clojure-maven-plugin & tests. By default, clojure-maven-plugin should generate test-runner automatically, as described in documentation.

And it's better to use latest version of clojure-maven-plugin - 1.3.2, in which there are several bugs were fixed

like image 42
Alex Ott Avatar answered Oct 15 '22 22:10

Alex Ott