Skip to content

Test cases for TestDefaultDeviceCodeService#1368

Open
Sangeeta19 wants to merge 9 commits intomitreid-connect:masterfrom
Sangeeta19:master
Open

Test cases for TestDefaultDeviceCodeService#1368
Sangeeta19 wants to merge 9 commits intomitreid-connect:masterfrom
Sangeeta19:master

Conversation

@Sangeeta19
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown
Member

@jricher jricher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far, looking to see the rest of the pieces filled out.

@codecov-io
Copy link
Copy Markdown

codecov-io commented Feb 23, 2018

Codecov Report

Merging #1368 into master will increase coverage by 0.7%.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff             @@
##             master    #1368     +/-   ##
===========================================
+ Coverage     24.67%   25.37%   +0.7%     
- Complexity      888      914     +26     
===========================================
  Files           209      209             
  Lines         11673    11676      +3     
  Branches       2116     2116             
===========================================
+ Hits           2880     2963     +83     
+ Misses         8287     8199     -88     
- Partials        506      514      +8
Impacted Files Coverage Δ Complexity Δ
...mitre/openid/connect/config/JsonMessageSource.java 83.87% <0%> (+83.87%) 16% <0%> (+16%) ⬆️
.../oauth2/service/impl/DefaultDeviceCodeService.java 96.87% <0%> (+96.87%) 10% <0%> (+10%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e2d94f4...c12f15c. Read the comment docs.

1) createNewDeviceCode()
2) approveDeviceCode()
unit test - public void clearExpiredDeviceCodes()
expiredDeviceCodes1 = Sets.newHashSet(deviceCode1, deviceCode2);
expiredDeviceCodes2 = Sets.newHashSet();

Mockito.when(repository.getExpiredCodes()).thenReturn(expiredDeviceCodes1, expiredDeviceCodes1,expiredDeviceCodes2);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're getting the test to pass here through some side effects. If the underlying paging system were to change, it might suddenly fail because of these side effects.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Justin,
Instead of invoking Mockito.veriyf() with hard code values like 2 and 4, invoking with size of set expiredDeviceCodes1 will solve the issue.

Is my understanding of the your comment is correct?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will the below code snippet works in case if it is all about size:

Set expiredDeviceCodes1 = Sets.newHashSet(deviceCode1,deviceCode2);
Set expiredDeviceCodes2 = Sets.newHashSet();
Mockito.when(repository.getExpiredCodes())
.thenReturn(expiredDeviceCodes1,expiredDeviceCodes2);
service.clearExpiredDeviceCodes();

Mockito.verify(repository,times(expiredDeviceCodes1.size()))
.remove(Matchers.any(DeviceCode.class));
Mockito.verify(repository,times(expiredDeviceCodes1.size())).getExpiredCodes();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not about size, it's about how you're making your repository respond. The size calls are an improvement, but that's not the main issue. You're having your mocked repository return a full list twice and an empty list regardless of whether or not the expirations have been performed on it in the mean time. In other words, it's passing because the service thinks it's doing the right thing but it would still pass if something else a little weird happened.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By adding Sysout to method fetchPage() and doOperation(DeviceCode item) in implementation class, realized that for each call of fetchPage() : doOperation(DeviceCode item) calls are dependent on size of expiredCodesList, so all the items are removed.

Then after fetchPage() is called again , now empty set will be returned (from mocked repository) as it got emptied out in the last call.

This forced me to mock repository with list with elements for first call and zero elements in second call.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that's only true if things are emptied out as expected with each page.

Copy link
Copy Markdown
Member

@jricher jricher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build still failing and the side effect of the clear tests hasn't been fixed

@Sangeeta19
Copy link
Copy Markdown
Author

Justin, I tried and I could not come up with the better way of mocking the repository to test the clearExpiredDeviceCodes(). Could you please help me.

Sangeeta19 added 3 commits May 1, 2018 21:48
-with thenReturn() and doAnswer()
-with thenReturn() and doAnswer()
Update TestDefaultDeviceCodeService.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants