Junit for UI & descriptor classes

18 views
Skip to first unread message

Nikhil Bhoski

unread,
Apr 16, 2019, 6:35:14 AM4/16/19
to Jenkins Developers
I want to test the doCheck() blocks written in the Descriptor class. Any suggestion that how could i write Unit tests (Junits) too test these doCheck() blocks . or I should use some UI testing framework like selenium ? 

Mark Waite

unread,
Apr 16, 2019, 7:35:33 AM4/16/19
to jenkinsci-dev
It depends on the operations to be tested in the doCheck() method.  My order of priority has been to 
  1. extract those checks into package protected methods that I called directly from a unit test, without relying on something to call the doCheck() method.  Frequently, though, that is not enough, because the checks which are being performed are  dependent on information that is only available with a running Jenkins instance
  2. extract those checks into package protected methods that I call from a test that configures a running Jenkins instance using a JenkinsRule.
  3. use the HTMLUnit related utilities that are provided with the Jenkins test harness
  4. use Selenium or some other UI driving test harness
I'm not experienced with items 3 or 4 in that list, though I know that there are many tests in the Jenkins project that use Selenium.

You might also refer to 

On Tue, Apr 16, 2019 at 4:35 AM Nikhil Bhoski <nikhil...@gmail.com> wrote:
I want to test the doCheck() blocks written in the Descriptor class. Any suggestion that how could i write Unit tests (Junits) too test these doCheck() blocks . or I should use some UI testing framework like selenium ? 

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/ff26900f-91ef-4da6-bdc7-c43c793d1bc3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Thanks!
Mark Waite

Jesse Glick

unread,
Apr 16, 2019, 9:08:07 AM4/16/19
to Jenkins Dev
On Tue, Apr 16, 2019 at 7:35 AM Mark Waite <mark.ea...@gmail.com> wrote:
> extract those checks into package protected methods that I call from a test that configures a running Jenkins instance using a JenkinsRule.

Or just call them directly, since they would already be `public` and
you can assert the contents of a `FormValidation` easily enough. (Look
up the descriptor instance without casts via
`Jenkins.getDescriptor(Class)`.)

Generally speaking, Stapler is designed to let you test web methods as
plain Java calls whenever reasonable, by defining specialized subtypes
of `HttpResponse` that can be used as return values, and by accepting
`@QueryParameter` and `@AncestorInPath` arguments.

Using HtmlUnit to check form validation would be relatively painful,
and overkill.

Ullrich Hafner

unread,
Apr 16, 2019, 9:45:52 AM4/16/19
to Jenkins Developers
If you need some examples:

Note that all those methods have been extracted into a separate class so that I can use them for different steps, this is not required in your case:

Typically all those validation methods can be tested with plain unit tests, so Jenkins test harness or selenium (i.e. Jenkins Acceptance Test Harness) is not needed. You just need to be carefully about not using Jenkins.getInstance() directly otherwise unit testing is not possible anymore.   


--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages