2

Hi I am new to AngularJS and testing JS. I need to run my tests with maven. I have created a controller and a service for a small app and they are working. Now i want to write tests (yes, broke the rule to write tests while developing) for the JS code I have. I have so far managed to invoke the tests with maven.

I am using jasmine-maven-plugin.

<plugin>
    <groupId>com.github.searls</groupId>
    <artifactId>jasmine-maven-plugin</artifactId>
    <version>1.3.1.3</version>
    <executions>
        <execution>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <jsSrcDir>src/main/webapp/js</jsSrcDir>
        <jsTestSrcDir>src/test/webapp/js</jsTestSrcDir>
        <specIncludes>
            <include>*Spec.js</include>
        </specIncludes>
    </configuration>
</plugin>

When run mvn test I see the following

[INFO] --- jasmine-maven-plugin:1.3.1.3:test (default) @ pod-manager-web-ang --- 2013-10-14 14:57:34.322:INFO:oejs.Server:jetty-8.1.13.v20130916 2013-10-14 14:57:34.378:INFO:oejs.AbstractConnector:Started [email protected]:56475 [INFO] Executing Jasmine Specs

Then a failure with the message below

[ERROR] Caused by: net.sourceforge.htmlunit.corejs.javascript.EcmaError: ReferenceError: "angular" is not defined. (http://localhost:56475/src/controllers/controller.js#3)
[ERROR] at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3603)
[ERROR] at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3587)
[ERROR] at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3657)
[ERROR] at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.name(ScriptRuntime.java:1685)

The question is where should I place the angular.js and other angular JS files for these tests to run?

1 Answer 1

6

Use preloadSources to load Angular, jQuery and other needed libraries.

<configuration>
    <preloadSources>
        <source>${project.basedir}/src/main/webapp/vendor/jquery/1.10.2/jquery.js</source>
        <source>${project.basedir}/src/main/webapp/vendor/angular/1.0.5/angular.js</source>
    </preloadSources>
    <jsSrcDir>src/main/webapp/js</jsSrcDir>
    <jsTestSrcDir>src/test/webapp/js</jsTestSrcDir>
    <specIncludes>
        <include>*Spec.js</include>
    </specIncludes>
</configuration>
Sign up to request clarification or add additional context in comments.

6 Comments

Hi kseb, Thanks for the response, why would i need jquery files, right now my code is working fine without them. Is this something i would need for running the unit tests?
I needed jQuery because of limited jqLite implementation. I was using more things from jQuery. So for me it was important to have it included. Your need may be different though.
Using the preloadSources tag is the correct option to my original question. But i still couldn't get the tests run headless, i am able to run them using "mvn jasmine:bdd" command. I have not included jquery as i don't need them.
Look at the example on plnkr.co/edit/naLJ3mD3UjoTqcF9nG8O . There is my working version of pom.xml with jasmine tests integration. Tests are firing after executing in a console: mvn clean install. I hope it helps.
I got it working by using the PhantomJS driver, <webDriverClassName>org.openqa.selenium.phantomjs.PhantomJSDriver</webDriverClassName>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.