JavaScript Eco-System for Java Developers

DZone 's Guide to

JavaScript Eco-System for Java Developers

Check out the JavaScript ecosystem from the perspective of a Java developer. How do the tools in Java relate to those in JavaScript?

· Web Dev Zone ·
Free Resource

After being a Java developer for some time, it took me a little while to get my head around the plethora of tools available in JavaScript ecosystem. As a first step, I started drawing parallels between the two ecosystems to perform a given task. In JavaScript, the focus has been on developing tools to perform tasks at a much granular level, so my first finding was that for a task that is done in Java by one tool/technology, JavaScript would have a combination of multiple toolsets. Putting a disclaimer here: Things might not match exactly to a tee but my intention here is to provide this information as a jumpstart on the tools available in JavaScript ecosystem. With that said, let’s move to the first item in the list Node.js.

Node.js (Java: Java.exe)

Node.js is an event-driven, server-side JavaScript environment. Node runs JavaScript using the V8 engine developed by Google for use in their Chrome web browser. Leveraging V8 allows Node to provide a server-side run-time environment that compiles and executes JavaScript at lightning speeds. The major speed increase is due to the fact that V8 compiles JavaScript into native machine code, instead of interpreting it or executing it as bytecode. One thing to keep in mind is that “Node.js is not a JavaScript framework”. So in short, Node.js is a packaged compilation of Google’s V8 JavaScript engine, which itself is primarily written in JavaScript. With Node.js, you now have a run-time environment ready for JavaScript (You can compare this with Java). Next step is how to get the packages that can be used for developing web apps on Node platform, which leads us to NPM as the package manager for Node.

NPM (Java: Maven)

NPM (Node Package Manager) is the default package manager for Node.js. By default, NPM modules are retrieved over the Internet from the public package registry maintained on http://npmjs.org and modules may be installed through "npm install". This is similar to Maven or Ivy working on getting different dependencies for your project in the Java world. Once you have all the building blocks to create your web application, you need a way to compile and bundle them for distribution. Maven/Ivy resolve these dependencies for Java Libraries.

Gulp/Grunt (Java: Maven/Ant)

Gulp or Grunt are the build system to automate the tasks needed for JavaScript code. This could mean compiling CoffeeScript to JavaScript, SASS to CSS, minification of HTML/JS or concatenation of JS files etc. You can relate this to Maven or Ant providing different targets to compile and build Java code. Gulp is stream build system gaining much popularity for JavaScript. Gulp can be downloaded and installed using npm for Node as a module. Maven can invoke Gulp build using plugins to keep Maven as an overall build system and Gulp taking care of JavaScript piece.Developers start writing code in JavaScript and they now need code analyzer and a mechanism to write and run tests.

JSHint (Java: PMD/Fortify)

JSHint is a community-driven tool to detect errors and potential problems in JavaScript code and to enforce your team's coding convention. A command-line version of JSHint, distributed as a Node.js module, makes it possible to automate one's "linting" process and integrate JSHint into the website's development workflow. Java developers use PMD or Fortify for static code analysis.

Jasmine/ Mocha + Chai + Sinon.js (Java: JUnit/Mockito)

With code, you have to write unit tests and it can be achieved through Jasmine or Mocha + Chai + Sinon.js. Jasmine is a behavior-driven development framework for testing JavaScript code and it does not depend on any other JavaScript framework. It provides the capability to write tests and check code using assertions in addition to providing ways to spy/stub or mocks for JavaScript. Mocha (write test) + Chai( assertions) + sinon.JS (spy/stub/mock) provide similar features for JavaScript code. A Java developer would end up using JUnit (test + assertions) and Mockito/Easymock (stub/mocks) for testing Java code. With the test cases ready, they have to be executed somewhere and code coverage has to be captured.

Karma (Java: JUnit/Clover)

Karma is a test runner for JavaScript that runs on Node.JS. Using Karma you can run tests using one of many popular JavaScript testing suites (Jasmine, Mocha, etc.) and have those tests executed not only in the browsers (Chrome, Firefox, PhantomJS) of your choice, but also on the platform of your choice (desktop, phone, tablet.) Karma is highly configurable, integrates with popular continuous integration packages (Jenkins, Travis, etc.) and has excellent plugin support. It also has reporters that give you a complete listing of each test that is run, in the order that they are executed, and whether they pass or fail. There are other reporters available as plugins (growl, JUnit, Teamcity, and Coverage) that can help make your testing process easier and more complete. Code coverage is provided using Istanbul coverage tool. The equivalent for Java code would be JUnit (test runner) and Clover (Coverage) Unit tests are the first line of defense for catching bugs, but sometimes issues come up with integration between components which can't be captured in a unit test. End to end tests are made to find these problems. 

Protractor (Java: Arquillian/QTP)

Protractor is an end to end test runner which simulates user interactions that will help you verify the health of your web application. Protractor is a Node.js program, and runs end to end tests that are also written in JavaScript and run with Node.JS. Protractor uses WebDriver to control browsers and simulate user actions. Protractor uses Jasmine for its test syntax. As in unit testing, a test file is comprised of one or more it blocks that describe the requirements of your application. it blocks are made of commands and expectations. Commands tell Protractor to do something with the application such as navigate to a page or click on a button. Expectations tell Protractor to assert something about the application's state, such as the value of a field or the current URL. Java developers would have used Arquillian/QTP etc. for testing their web applications.

PhantomJS (Java: HTMLUnit)

PhantomJS is a headless WebKit scriptable with a JavaScript API. You can run functional tests with frameworks such as Jasmine, QUnit, Mocha etc. without the need for a browser like Chrome/Firefox etc. PhantomJS is NOT written as Node.js module. PhantomJS itself is not a test framework, it is only used to launch the tests via a suitable test runner (e.g. Jasmine using phantom-jasmine or Mocha using mochaphantomjs). Using PhantomJS with a CI system such as Jenkins or TeamCity does not require special setup. HtmlUnit is used as a headless browser in Java.

JavaScript/Java Toolchain Breakdown

Need JavaScript Java
Runtime Environment NodeJS Java
Package Dependency Management NPM Maven or Ivy
Build Tool Gulp or Grunt Maven or Ant
Code Analyzer JSHint
PMD, Fortify etc.
Unit Test (Test + Assertion + Mock)
Jasmine or Mocha(Test) +Chai(Assertion) +SinonJS(Stub/Mock)
Junit, Mockito etc.
Test Runner/Code Coverage Karma (Runner) + Istambul (Coverage)
Junit(Runner) + Clover(Coverage)
Integration Test Protractor Arquillian or QTP
Headless Test PhantomJS HtmlUnit

angular, java, javascript

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}