Thanks for the ideas here. There are indeed a set of redundant spy
instances in the file sent. I will clean those up. I think I'm close to
getting this worked out...

On Tue, Jul 9, 2019 at 4:47 PM Ullrich Hafner <ullrich.haf...@gmail.com>
wrote:

> This topic reminds me of one of my architecture guidelines I’m presenting
> students in my testing lecture:
> An application should always provide a way to change (or inject) the
> current date (time). Seems that this is not only relevant in business
> applications ;-)
>
> I also faced a similar problem when trying to test the time series of the
> warnings charts (several builds over several days). I solved the problem by
> wrapping each build into an own object that has a time instance that can be
> changed in tests.
>
> Your code creates a lot of freestyle build spy instances that are actually
> never used. The EnhancedOldBuildDiscarder
> gets the builds using Jenkins API. So your spy is never used in the test.
> You need to replace the builds in the job with your spy instances.
>
> Am 09.07.2019 um 21:33 schrieb Gavin Mogan <gmo...@cloudbees.com>:
>
> So I'd personally try stubbing out calendar, and let jenkins take care of
> the rest. Purely pseudo code, untested by me.
>
>
> @PrepareForTest({Class1.class, Class2.class})
> class YourClassHere {
>
> yourFunctionHere() {
>
> PowerMockito.mockStatic(Calendar);
> mockCalendarInstance = mock(Calendar.class);
> PowerMockIto.when(Calendar.getInstance()).thenReturn(mockCalendarInstance);
>
> FreeStyleProject testProject = jRule.createFreeStyleProject("test"))
>
> foreach timestamps {
> mockCalendarInstance.setTime....
> jRule.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get()));
> }
>
>
> On Tue, Jul 9, 2019 at 10:38 AM 'Benjamin Beggs' via Jenkins Developers <
> jenkinsci-dev@googlegroups.com> wrote:
>
>> I've rewritten the test using JenkinsRule. The result is here:
>> https://github.com/jenkinsci/enhanced-old-build-discarder/blob/839210ca60694be6ad9b2152e671d39f82174ee5/src/test/java/org/jenkinsci/plugins/enhancedoldbuilddiscarder/EnhancedOldBuildDiscarderTest.java
>> .
>>
>> This is a much cleaner, less finicky implementation. However, my attempt
>> to mock the time stamps for the builds isn't working. Does anyone have an
>> idea for why this is?
>>
>> On Monday, July 8, 2019 at 6:58:44 PM UTC-4, Gavin Mogan wrote:
>>>
>>> You can probably use powermock to override the static
>>> Calendar.getInstance()
>>>
>>> But I don't easily know if that function is used to generate the
>>> internal build time, but you can probably track it down with a breakpoint
>>> in build.class
>>>
>>> On Mon, Jul 8, 2019 at 3:36 PM 'Benjamin Beggs' via Jenkins Developers <
>>> jenkin...@googlegroups.com> wrote:
>>>
>>>> I think a full implementation with Mockito is untenable. It seems I
>>>> would need to mock an excessive amount of function returns and the result
>>>> may still be inflexible and finnicky.
>>>>
>>>> I'll follow the JenkinsRule method you're talking about...I expect I
>>>> can still mock the date data. Thanks for the help here. :)
>>>>
>>>> On Monday, July 8, 2019 at 6:08:33 PM UTC-4, Gavin Mogan wrote:
>>>>>
>>>>> That shouldn't be the case if you use JenkinsRule, actually create the
>>>>> project, and run a quick echo pipeline a few times.
>>>>>
>>>>> I know from working on the java 11 stuff, that mockito has trouble
>>>>> mocking functions that are final, it'll silently not do so. Its going to
>>>>> save you a ton of headaches to use JenkinsRule.
>>>>>
>>>>> @Rule
>>>>> public JenkinsRule j = new JenkinsRule()
>>>>>
>>>>> p = j.createFreeStyleProject("name");
>>>>> jenkins.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0)); //
>>>>> build 1
>>>>> jenkins.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0)); //
>>>>> build 2
>>>>> jenkins.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0)); //
>>>>> build 3
>>>>>
>>>>> but i see you are messing with dates, so that might be trickier, i'm
>>>>> not sure.
>>>>>
>>>>> You can try subclassing FreeStyleProject, and overriding the function
>>>>> to return the value you want.
>>>>> If that works fine, then its how the function matching/overriding
>>>>> works in mockito, if it doesn't work, then its the weird final/static 
>>>>> stuff
>>>>> that mockito has trouble with. You might need to also use powermock and
>>>>> prepare for test.
>>>>>
>>>>> On Mon, Jul 8, 2019 at 12:40 PM 'Benjamin Beggs' via Jenkins
>>>>> Developers <jenkin...@googlegroups.com> wrote:
>>>>>
>>>>>> The issue here is with my build history instantiation. All build
>>>>>> retrieve calls return null regardless of method (getFirstBuild,
>>>>>> getLastBuild, getBuildByNumber, etc).
>>>>>>
>>>>>> On Monday, July 8, 2019 at 3:24:47 PM UTC-4, Benjamin Beggs wrote:
>>>>>>>
>>>>>>> I will investigate this further, thanks for pointing this
>>>>>>> implementation out to me.
>>>>>>>
>>>>>>> On Monday, July 8, 2019 at 1:52:40 PM UTC-4, Jesse Glick wrote:
>>>>>>>>
>>>>>>>> On Mon, Jul 8, 2019 at 1:39 PM 'Benjamin Beggs' via Jenkins
>>>>>>>> Developers
>>>>>>>> <jenkin...@googlegroups.com> wrote:
>>>>>>>> > Do you have an example of a similar implementation for unit
>>>>>>>> testing using jenkinsrule? I may try just porting the tests over to 
>>>>>>>> this.
>>>>>>>>
>>>>>>>> I would definitely advise using `JenkinsRule` over mocking
>>>>>>>> frameworks.
>>>>>>>> Slower, but much more realistic (and much more likely to continue
>>>>>>>> running after internal refactorings). Example:
>>>>>>>>
>>>>>>>>
>>>>>>>> https://github.com/jenkinsci/jenkins/blob/a6f5b2b1288d15cd2ea5c2fd9b8916e6397bf795/test/src/test/java/hudson/tasks/LogRotatorTest.java#L67-L82
>>>>>>>>
>>>>>>>
>>>>>> --
>>>>>> 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 jenkin...@googlegroups.com.
>>>>>> To view this discussion on the web visit
>>>>>> https://groups.google.com/d/msgid/jenkinsci-dev/610d3de0-c377-4c4c-bd2d-db00c84b6f41%40googlegroups.com
>>>>>> <https://groups.google.com/d/msgid/jenkinsci-dev/610d3de0-c377-4c4c-bd2d-db00c84b6f41%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>> .
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>>
>>>> --
>>>> 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 jenkin...@googlegroups.com.
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/jenkinsci-dev/529dd72d-c30c-44df-83b5-e4ffacfa7694%40googlegroups.com
>>>> <https://groups.google.com/d/msgid/jenkinsci-dev/529dd72d-c30c-44df-83b5-e4ffacfa7694%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>> --
>> 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-dev+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/jenkinsci-dev/e8303bf9-4523-4fc2-96cd-45affddef1d1%40googlegroups.com
>> <https://groups.google.com/d/msgid/jenkinsci-dev/e8303bf9-4523-4fc2-96cd-45affddef1d1%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> 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-dev+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jenkinsci-dev/CAAgr96%2Brwq8tgn8LhgXAH_V3%2BPhk8OJWFnQioFtuNp2zZptmpw%40mail.gmail.com
> <https://groups.google.com/d/msgid/jenkinsci-dev/CAAgr96%2Brwq8tgn8LhgXAH_V3%2BPhk8OJWFnQioFtuNp2zZptmpw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Jenkins Developers" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/jenkinsci-dev/_kNEe9ggaW0/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> jenkinsci-dev+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jenkinsci-dev/D2D18CD8-C489-47F5-A015-3768431752D2%40gmail.com
> <https://groups.google.com/d/msgid/jenkinsci-dev/D2D18CD8-C489-47F5-A015-3768431752D2%40gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-dev/CAMSuSKTBd4gfyTF09E2dsXVW0%2B4h%3DXGHdBr_MkazMFj4Yq5j_Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to