Git Product home page Git Product logo

jasmine-junit-runner's Introduction

Jasmine Junit Runner

What's this?

Something like this:

describe("pure awesomeness", function() {
	it("should be amazing!", function() {
		expect(stuff).toEqual("amazing");
	});
	
	it("should be awesome", function() {
		expect(moreStuff).toBe("awesome");
	});
});

describe("coolness", function() {
	it("should be cooler than ice when freezed", function() {
		var coolness = CoolingRepository.beCool();
		coolness.freeze();
		expect(coolness.coolnessRatio).toBe(-100);
	});
	it("should be cool enough by default", function() {
		expect(CoolingRepository.beCool().coolnessRatio).toBe(-5);
	});
});

Being translated into something like this:

Junit Eclipse runner


Quite simple, it's a custsom Java Junit Runner that allows you to embed Javascript Unit tests (using Jasmine) in your Java-based projects. It fully integrates with your most beloved IDE, your most hated version control system and of course your most needed CI env.

So let's rephrase:

  • Run Javascript (the Jasmine - behavior driven - way) "specs" in Java
  • Talks like a duck-erhm, any other Junit Java test. Just use a custom annotation (see below)
  • Executes super-fast. No browser required. Hocus-pocus. (Rhino + Envjs magic)

Does this thing support ...

Generation of Junit XML Results?

Yes and no. Not explicitly using the Jasmine Junit XML Reporter, but since it's a Java Junit Result, your build process will do that for you. Maven surefire plugins will generate the needed result files, for Jenkins to pick up. Your stacktrace/failure message will be something like:

Expected x to be y (zz.js, #458)

Just like the default Jasmine HTML reporter. (So, to answer the question: yes!)

GUI Testing with Envjs?

Yes! It allows you to test your jQuery plugins or your spaghetti GUI+Logic code, neatly woven together. You can use jasmine-jquery matchers. I've modified jasmine.Fixtures to support Envjs+Rhino. This means you can test stuff like this:

beforeEach(function() {
  loadFixtures("myFixture.html");
});

it("should be visible and blue", function() {
  var div = $('#myDivInFixtureHtml');
  expect(div).toBeVisible();
  expect(div.css('color')).toBe('blue');
});

Fixtures are automatically cleaned up. See src/test/javascript/lib/jasminedir/jasmine-jquery-rhino.js

But wait, CSS Style Parsing does not work in Envjs 1.2, how come this does?

See env.utils.js. Cover your eyes - hacks present.

Debugging 'n stuff?

Yes! When the debug mode flag in @JasmineSuite has been set to true, you can use the Rhino Debugger to set breakpoints. After pressing "GO", the tests will run and you can inspect stuff and step through the code.

What about integrated debugging inside my IDE?

Tough luck. I've tried to get JSDT working but no avail. You can still use Firebug to debug when generating a specRunner HTML file (see below).

Excellent! What Do I need to do?

  1. Fork this project.
  2. Create some Jasmine specs, place them in some folder.
  3. Create a Junit test class, annotate it with @RunWith(JasmineTestRunner.class)
  4. Fill in the blanks using @JasmineSuite

More options

@JasmineSuite allows you to set these options:

  • debug: use the built-in Rhino debugger (gives you the chance to set a breakpoint before firing the test suite)
  • jsRootDir: the javascript install root dir. Jasmine and other should be installed here (see source)
  • sourcesRootDir: your production JS files root dir.
  • specs: one or more spec file to run. You may also use the glob syntax described here.For example to load all javascript files in subdir use subdir/*.js. Default behavior: use java Class name (replaces Test with Spec, see example).
  • sources: one or more JS production file which your spec needs (included before specs, d'uh). You can use the same glob syntax as the specs option.
  • generateSpecRunner: (the HTML output, useful for firefox/firebug debugging etc)
  • specRunnerSubDir: a subsidiary path to the default runner root directory where generated spec runners will be placed
  • envJs: load EnvJS support (defaults to true)

Requirements

Currently, Jasmine Junit Runner relies on Rhino 1.7R2 (+ es5-shim) & Envjs 1.2 to interpret JS code. It also uses Jamsine 1.0.2 to read your spec files. All js libs are located in test/javascript/lib .

Dependencies Overview

See the pom.xml (Maven2) - you can build the whole thing using:

mvn clean install

  • Rhino 1.7R2 + es5-shim 0.0.4 (not needed if you'll be using 1.7R3)
  • Envjs 1.2 + required hacks in env.utils.js
  • Jasmine 1.0.2
  • Java libs: commons-io and commons-lang (test libs: mockito and fest assert)

Examples

Running a spec file as a Junit test

Use the default spec naming convention

If you do not specify specs with the annotation, the runner will auto-pick the spec name using your test class. The below test will load myAwesomeSpec.js from the specs dir (jsRootDir + '/specs/').

@RunWith(JasmineTestRunner.class)
@JasmineSuite(sources = { 'jQuery.js', 'myAwesomeCode.js' } )
public class MyAwesomeTest {
}

your awesome production code relies on jQuery (of course it does), so you'll have to include it.

Your spec file might look like this:

describe("my awesome code", function() {
	it("will always run", function() {
		expect(stuff.DoCoolThings()).toBe("awesome");
	});
});

Using Junit's @Before and @After_

It's possible to do some extra work before and after each spec run:

@RunWith(JasmineTestRunner.class)
@JasmineSuite
public class MyAwesomeTest {

  @Before
  public void beforeStuff(RhinoContext context) {
    context.evalJS("var prefabVar = { cool: 'yeah!' };");
  }
  
  @Before
  public void beforeStuffNoContext() {
    System.out.println("I'm gonna blow! Or Will I?");
  }
  
  @After
  public void afterStuff() {
    // say cool things
  }

}

What's happening?

  • You can define n number of PUBLIC methods annotated with @Before or @After
  • You can, but don't have to, take the RhinoContext object as the only parameter. This allows you to set stuff up in JS space before running the spec.

Generating a spec runner

Your awesome test (example 1) would for instance generate this html file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>Jasmine Test Runner</title>
		<link rel="stylesheet" type="text/css" href="./../lib/jasmine-1.3.1/jasmine.css">
		<script type="text/javascript" src="./../lib/jasmine-1.3.1/jasmine.js"></script>
		<script type="text/javascript" src="./../lib/jasmine-1.3.1/jasmine-html.js"></script>
		
		<script type='text/javascript' src='./../../../main/webapp/js/jquery.js'></script>
		<script type='text/javascript' src='./../../../main/webapp/js/myawesomecode.js'></script>
		
		<script type='text/javascript' src='./../specs/myawesomespec.js'></script>
	</head>
	<body>

		<script type="text/javascript">		
			jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
			jasmine.getEnv().execute();
		</script>
	</body>
</html>

You can inspect the output using firefox, or debug in your spec file using firebug.

jasmine-junit-runner's People

Contributors

blalor avatar edeweerd1a avatar jansabbe avatar jdobson avatar olabini avatar stbutler11 avatar torstenhein avatar woutergroeneveldkul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

jasmine-junit-runner's Issues

envJsOptions.js not found

I ran "mvn package" and I got the following error:

Running be.klak.env.EnvUtilsTest
[ Envjs/1.6 (Rhino; U; Windows 7 x86 6.1; en-US; rv:1.7.0.rc2) Resig/20070309 PilotFish/1.2.13 ]
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.721 sec <<< FAILURE!
initializationError(be.klak.env.EnvUtilsTest) Time elapsed: 0.005 sec <<< ERROR!
org.mozilla.javascript.EvaluatorException: Couldn't read source file "src/test/javascript/envJsOptions.js": src\test\javascript\envJsOptions.js (The system cannot find the path specified). (script#1)
at org.mozilla.javascript.tools.ToolErrorReporter.runtimeError(ToolErrorReporter.java:111)

Submit to Maven Central?

Hi, Wouter! I discovered your Jasmine JUnit code a couple of days ago and I'm very impressed. I have a couple of pull requests I'm going to send your way that make using the library much easier by packaging all of the resources into the jar, rather than copying the Jasmine and EnvJS stuff into each dependent project. I'm wondering if you'll be deploying this project to the Maven Central repository. I think it's in a very usable state!

Code Coverage

Are you interested in trying to incorporate code coverage with JSCover2? There's a rough draft of how it can be done here.

Upgrade to Jasmine 2.0

Hi,

I found your jasmine runner pretty interesting. I tried upgrading the jasmine version to 2.0, but something went wrong. Have someone ever tried it before?

Thank you

UnknownHostException for file system links to sources

Hi jefklak,

happy to find your project here as an alternative to the jasmine-maven-plugin. I tried to setup everything into my project. I configured my Test class - I think correctly:

@RunWith(JasmineTestRunner.class)
@JasmineSuite(sources = { "lib/jquery-1.7.1.js", "lib/underscore.js", "lib/require-jquery.js", "lib/backbone.js","lib/bootstrap.js", "main.js" }, specs = {"my.spec.js"}, generateSpecRunner = true, sourcesRootDir = "src/main/javascript")
public class MyTest {

...

}

As you can see my specs are depending on some 3rd party stuff.

When I run it, the CLI tells me the following:

[ Envjs/1.6 (Rhino; U; Windows 7 x86 6.1; en-US; rv:1.7.0.rc2) Resig/20070309 PilotFish/1.2.13 ]
failed to open file file://C/:/projectRoot/api/data.js JavaException: java.net.UnknownHostException: C
js: syntax error
js:

JavaException: java.net.UnknownHostException: C


js: .^
....

Now there are two problems with that, which I don't know how to resolve:

  1. the path is wrong. api/data.js is actually situated in src/main/javascript which I actually configured in using the JasmineSuite interface. How come the file path is not reflected. It should be file://C/:/projectRoot/src/main/javascript/api/data.js
  2. I am not sure but why is the test runner throwing an UnknownHostException for file system resources. Am I missing something. It shouldn't try to connect using java.net, right?

I really hope you can point me to something. Let me know if you need my contributions or the like?
Best regards and thank you in advance!

Building the project

First of all, I should say I'm very excited at the prospect of being able to run jasmine tests along with junit tests. Thank you for developing this plugin!

I'm trying to build the project locally (from master) and encountering some test failures. I'm getting this exception:

useJasmineRunnerOnJasmineTestRunnerBeforeAndAfterClass(be.klak.junit.jasmine.JasmineTestRunnerBeforeAndAfterTest)
  Time elapsed: 0.127 sec  <<< ERROR!
java.lang.RuntimeException: Exception while firing After method: runMijAfterOok
        at be.klak.junit.jasmine.JasmineTestRunner.fireMethodsWithSpecifiedAnnotationIfAny(JasmineTestRunner.java
:206)
        at be.klak.junit.jasmine.JasmineTestRunner.run(JasmineTestRunner.java:170)
        at be.klak.junit.jasmine.JasmineTestRunnerBeforeAndAfterTest.useJasmineRunnerOnJasmineTestRunnerBeforeAnd
AfterClass(JasmineTestRunnerBeforeAndAfterTest.java:21)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
        at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at be.klak.junit.jasmine.JasmineTestRunner.fireMethodsWithSpecifiedAnnotationIfAny(JasmineTestRunner.java
:200)
        ... 33 more
Caused by: java.lang.AssertionError: unexpected element(s):<[0]> in <[0, 1]>
        at org.fest.assertions.Fail.failure(Fail.java:228)
        at org.fest.assertions.Assert.failure(Assert.java:149)
        at org.fest.assertions.ItemGroupAssert.failureIfUnexpectedElementsFound(ItemGroupAssert.java:107)
        at org.fest.assertions.ItemGroupAssert.assertContainsOnly(ItemGroupAssert.java:79)
        at org.fest.assertions.ObjectGroupAssert.containsOnly(ObjectGroupAssert.java:65)
        at be.klak.junit.jasmine.classes.JasmineTestRunnerBeforeAndAfterClass.runMijAfterOok(JasmineTestRunnerBef
oreAndAfterClass.java:30)
        ... 38 more

My machine is running windows 7. I'm getting this error after downloading the repo and executing:

mvn clean install

Please advise.

Alternatively, is there a mirror that this dependency can be downloaded from so that I don't have to build locally?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.