Skip to content
This repository has been archived by the owner on Mar 21, 2022. It is now read-only.

simplify DefaultDockerClientUnitTest #753

Merged
merged 3 commits into from
May 22, 2017

Conversation

mattnworb
Copy link
Member

refactor this test to use a MockWebServer rather than mocking the
Jersey/jax-rs Client interface used by the DefaultDockerClient.

Using a (Mockito) mock Client is problematic for a test since it has
such a rich and fluent interface - the test needs to know exactly which
methods are called on the Client.

Ultimately, this is not what a unit test of DefaultDockerClient should
care about - a good test would only care about what HTTP requests it
sends to the Docker Remote API and how it behaves when it gets certain
HTTP responses back.

The MockWebServer allows us to control the HTTP responses and capture
the HTTP requests much easier than using a Mockito mock of the
particular HTTP client library used by DefaultDockerClient.

With this approach, it would be possible to one day change
DefaultDockerClient to use a different HTTP client library without
having to change one line of this test. The same cannot be said of using
Mockito.

This change was initially driven by a desire to make PR #749 easier to
test, to check that it sends expected authentication headers, but I
think it will be useful and help improve testing in docker-client across
the board.

https://github.com/square/okhttp/tree/master/mockwebserver

@mattnworb mattnworb requested review from davidxia and mavenraven May 20, 2017 15:30
refactor this test to use a MockWebServer rather than mocking the
Jersey/jax-rs Client interface used by the DefaultDockerClient.

Using a (Mockito) mock Client is problematic for a test since it has
such a rich and fluent interface - the test needs to know exactly which
methods are called on the Client.

Ultimately, this is not what a unit test of DefaultDockerClient should
care about - a good test would only care about what HTTP requests it
sends to the Docker Remote API and how it behaves when it gets certain
HTTP responses back.

The MockWebServer allows us to control the HTTP responses and capture
the HTTP requests much easier than using a Mockito mock of the
particular HTTP client library used by DefaultDockerClient.

With this approach, it would be possible to one day change
DefaultDockerClient to use a different HTTP client library without
having to change one line of this test. The same cannot be said of using
Mockito.

This change was initially driven by a desire to make PR #749 easier to
test, to check that it sends expected authentication headers, but I
think it will be useful and help improve testing in docker-client across
the board.

https://github.com/square/okhttp/tree/master/mockwebserver
@mattnworb mattnworb force-pushed the mattbrown/unit-test-mockhttpserver branch from 4b410f4 to e4aaf61 Compare May 20, 2017 15:32
there is no need to customize how the Jersey Client is built
@mattnworb
Copy link
Member Author

with this, the unit test becomes much simpler to read, with less code, and is also testing more of what we actually care about - HTTP request/response content - instead of which specific methods are called on the jax-rs Client.

cc @johnflavin

hamcrest-jackson requires Java 8; but we still build docker-client for
Java 7.

Remove the usage of this library in the test until we upgrade
docker-client to Java 8.
@mattnworb mattnworb force-pushed the mattbrown/unit-test-mockhttpserver branch from 45aa52b to 0f021bb Compare May 22, 2017 17:34
@codecov-io
Copy link

Codecov Report

Merging #753 into master will increase coverage by 2.89%.
The diff coverage is 100%.

@@             Coverage Diff              @@
##             master     #753      +/-   ##
============================================
+ Coverage     65.59%   68.48%   +2.89%     
+ Complexity      656      655       -1     
============================================
  Files           153      153              
  Lines          2883     3268     +385     
  Branches        335      340       +5     
============================================
+ Hits           1891     2238     +347     
- Misses          845      880      +35     
- Partials        147      150       +3

@mattnworb
Copy link
Member Author

@davidxia @mavenraven

Copy link
Contributor

@davidxia davidxia left a comment

Choose a reason for hiding this comment

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

amazing. much better

Assert.assertEquals(1, valueArgument.getValue());
assertThat(recordedRequest.getHeader("int"), is("1"));
assertThat(recordedRequest.getHeader("string"), is("2"));
// TODO (mbrown): this seems like incorrect behavior - the client should send 3 headers with
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this an issue with our code or upstream?

Copy link
Member Author

Choose a reason for hiding this comment

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

it is an existing bug in this Builder's "Add header" method which I didn't want to fix as a part of this, but I think should be fixed at some point

@mattnworb mattnworb merged commit 6e24184 into master May 22, 2017
@mattnworb mattnworb deleted the mattbrown/unit-test-mockhttpserver branch May 22, 2017 18:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants