How to unit test Java code that dynamically displays html and css?

I have a Java class that gets the form definition from the database, then displays html, javascript and css according to the form definition, using the set "appendable.append (...). Append (...)" to create the html- fragments that are subject to errors. Jsp and the general structure of templates (for example, FreeMarker) are not an option here, since all javascript, css, html elements are dynamic (depending on the form definition). and for some reason GWT is also not an option.

The direct modulation method that this renderer is testing is to hardcode the expected html code and then compare it with the actual output, but then the test is very fragile.

What is the best way to unit test this kind of html rendering please?

Thanks to everyone in advance.

+3
source share
3 answers

If you expect HTML with hard code, your tests may be fragile, but they will probably catch most of your errors. Another approach is to check for certain key or important tags in the HTML output. This approach is much more flexible, but may miss some errors. To decide what you should use, consider how often you expect HTML structure changes.

Here you can find the balance. The more specific your test, the more fragile it will be. But if you are not specific enough, your test will not catch any errors. In order to understand exactly how it should be, practice is required.

, , , , " ". , , . .

+3

. -, , . , . -, , , , . , . . http://www.w3.org/QA/Tools/.

+2

, , , " " .

there is a validation library that works with junit to do this, which will greatly simplify the process called “Approving Tests” http://www.approvaltests.com

It also helps protect against fragility by using reporters who will open your results and / or your golden master either in the diff reporter or in a web browser.

The call you are looking for is:

Approvals.VerifyHtml(yourHtml)

and you will want to decorate your test with one of

@UseReporter(DiffReporter.class)
@UseReporter(FileLauncherReporter.class)
@UseReporter({DiffReporter.class, FileLauncherReporter.class})
0
source

All Articles