Saturday, June 10, 2023
HomeJavaUnit Testing: Java AND JavaScript? - Java Code Geeks

Unit Testing: Java AND JavaScript? – Java Code Geeks

that factor the place recruiters suppose that Java and JavaScript are in all probability the identical factor? Nicely, they’re actually not… until…


Typically when working with a Java undertaking, you sail too near additionally relying on JavaScript. In these instances, the unit testing can get fairly messy. There are few methods through which it doesn’t get messy.

There are just a few workarounds for this, but it surely will get particularly nasty when the JavaScript that the undertaking makes use of is meant to be run within the JVM, through which case it’s neither Java nor JavaScript and might solely be unit examined in a particular and restricted approach.

It may be accomplished.

Except it could’t.

When do Java and JavaScript Mix?

There are two key use instances, they usually’re fairly totally different.

  • We’re utilizing server-side rendered pages and people pages comprise JavaScript
  • We’ve got a JavaScript script engine, equivalent to Nashorn or Rhino, to run some dynamic code utilizing JavaScript because the scripting language

There’s an alternate use case which I feel is a really totally different animal.

  • We use a framework like Angular, React or Vue to construct an SPA to take a seat on prime of a Java set of APIs

On this latter case, whereas the whole lot could also be in the identical repo (or not), we’ve basically obtained a transpiling, packaging course of, and a spot the place pure JavaScript (or TypeScript) unit assessments can and needs to be created, and it’s genuinely impartial of the Java construct.

Within the different instances, it’s a deeply gray space.

How Do I Know?

I’ve solved the above issues in actual tasks. I even contributed to HtmlUnit, which I used as a part of fixing the JavaScript in net pages drawback. I tried to submit a repair to Mocha to assist me create a Java/JavaScript unit check framework, however that didn’t work out ultimately.

The purpose is. I’ve realized some of these items from bitter expertise.

It’s form of not meant to be, but it nonetheless occurs.

JavaScript in Java Server-side-rendered Internet Pages

Earlier than we had a cleaner separation of entrance finish into SPAs, we would use a Java server to render the HTML of the web page on every request to the server. We nonetheless can. I did a undertaking this fashion final 12 months.

The request involves the Java server and the server then executes a template to render the proper HTML, together with some .js recordsdata and probably including some inline JavaScript, as essential. The template was Velocity or JSP, however in trendy Spring functions would possibly usually be Thymeleaf. All of them work in comparable methods in observe.

There are a few challenges:

  • With no transpiling framework for the JavaScript that seems inline on the web page – bindings of controls or whatnot – then we have now to imagine the worst when it comes to the runtime capabilities of JS on the goal browser
  • Typically we need to write a check that claims “when the info’s like this, then the web page does that” and people assessments want the JavaScript to run in addition to the server aspect rendering

It’s value declaring that the unit assessments to see that the web page actually has the proper stuff on it most frequently depend upon ready for the DOM to be lastly rendered after the JavaScript ran. In observe this will take seconds. The repair I submitted to HtmlUnit was to assist pace this up as a lot as doable. My assessments nonetheless ran gradual.

JavaScript Language Degree

JavaScript is an actual mess of a language. Relying on the runtime there are two dimensions of issues we could not have:

  • Key phrases and constructs – e.g. const or =>
  • Features – i.e. some library features aren’t obtainable on all browsers/runtimes, requiring us so as to add polyfills to switch them

Whereas the most recent node runtime, which we would use for unit testing could have EVERYTHING, the goal browser won’t. So we in some way want to check within the lowest-common denominator runtime.

Equally, when operating JavaScript within the JVM, we can not simply presume that the dynamic scripting engine will assist the whole lot that the JavaScript language permits. We will assume a superb 90%+, however we need to discover 100% of points earlier than ever deploying our software program, so unit testing of JavaScript for the JVM runtime must be examined in that runtime.

Operating Server Aspect JavaScript in Java

Even the title makes this appear to be a nasty concept. Nonetheless, it’s not totally a nasty factor. Basically we do it in order that:

  • We get the power to deploy dynamic scripts
  • We will combine Java objects and performance from the server’s context with our scripts

As such, the final case for testing JavaScript that runs within the JVM is one thing like this:

  • Create all of the Java context that the script consumes
  • Guarantee all libraries that the script imports are on the classpath
  • Execute the script from a unit check
  • Assert the output of the script in Java on the finish of the execution

This presumes that all the above is feasible. It’s usually doable, with software program we wrote. I’ve accomplished it.

One assumption is likely to be that the script returns an assertable worth. Possibly it does. Usually they don’t simply do this. Some scripts additionally act on the context, and we have to assert what occurred to the exterior world. In these instances, we have now to spy or mock that exterior world earlier than operating the script. Simpler with our personal code, more durable with another person’s.

In different phrases, it’s someplace from reasonably to very not doable.

Can’t You Simply Unit Check the Pure JavaScript?

So what if we stated to ourselves, in each of the above eventualities, that we needed to have some JavaScript libraries that have been pure and remoted from the messy implementation of the skin world, and might be unit examined in their very own proper?

The straightforward reply is positive we are able to. However. This assumes that it’s doable and fascinating for such a library to be imported into the goal run time.

  • For an online web page, it turns into one other static file to import from the JavaScript assets – i.e. extra web page load requests – that is likely to be okay, it would incur PageSpeed penalties and even decelerate another kinds of check automation
  • For a dynamic script to run in a JVM course of, it presumes you’ll be able to have further library recordsdata, moderately than a monolithic dynamic script (spoilers, it’s often solely the latter)

In these cases, the one drawback left to resolve is the language stage undertaking. Let’s be clear, although. Once I’ve used lodash or jquery in an online web page that’s rendered from Java, I’ve trusted their very own unit testing and transpilation.

Nonetheless, once I’m making an attempt to check in-page management bindings, or once I’m making an attempt to check a scriptlet that runs inside a Java low-code course of, I don’t essentially have the power to “simply import a effectively examined library”.

Oh… after which there was that point that I might use a effectively examined library, and there was one other drawback…

Java Objects Don’t Behave Like JavaScript Objects

So, even when you’ll be able to have a pure JavaScript check on a pure JavaScript operate you could import into your operating JVM-based JavaScript runtime, you’ll be able to’t 100% belief it.

It seems that if that script goes to run on objects that originated in Java, then there are instances (largely round detecting the kind of object you’re interacting with) the place the runtime behaves in another way with Java-originated objects, in contrast with JavaScript-originated objects.

In different phrases, you are able to do a pure JavaScript check (I’ve accomplished it) however you ALSO should retest utilizing Java and the scripting engine (I’ve accomplished that too), and also you’ll get barely totally different edge instances.

It’s a Lure?

This entire topic is straightforward in concept, and difficult in observe.

It’s not a lure.

It’s simply actually actually nuanced.

The very best unit assessments are exact, fast-running, and related. This implies they should run in the identical approach (or as close to as damn-it) to actual runtime, whereas nonetheless permitting us to slice away exterior dependencies.

JavaScript is kind of a imprecise language, so it is advisable to specialise the runtime of the check to the exact runtime it’ll be used with. Java-hosted JavaScript, supposed to work together with Java objects and libraries, is neither Java nor JavaScript.

In lots of instances, testing Java-hosted JavaScript is a case of sucking up the truth that it have to be carried out as an integration check.

In lots of instances, testing Java server-side-rendered pages is a case of tolerating the lengthy delays brought on by ready for the DOM in HtmlUnit, or perhaps a actual browser, to really render the web page after which probing it.

There are occasional straightforward wins inside this topic, however they can not usually be 100% trusted.

Good luck!



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments