It may be accomplished.
Except it could’t.
There are two key use instances, they usually’re fairly totally different.
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
Within the different instances, it’s a deeply gray space.
How Do I Know?
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.
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
There are a few challenges:
- Key phrases and constructs – e.g.
- Features – i.e. some library features aren’t obtainable on all browsers/runtimes, requiring us so as to add
polyfillsto 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.
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
- 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.
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 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
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…
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.
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.