title: How to resolve circular dependencies for projects when you want to remove duplicated code?
body: <p>Imagine you have the projects:</p>
  
  <ul>
  <li>Project <em>testUtils</em> that contains multiple test helper for your projects, all projects depends upon it.</li>
  <li>Project <em>mobile</em> that contains a <strong>MobileService</strong> and <strong>MobileController</strong> that depends upon <em>testUtils</em></li>
  <li>Project 'customer' that contains a <strong>CustomerService</strong> and <strong>CustomerController</strong> that depends upon <em>mobile</em> and also <em>testUtils</em></li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/ERyP8.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ERyP8.jpg" alt="enter image description here"></a></p>
  
  <p>You create a <strong>MobileServiceDouble implements IMobileService</strong> in your <em>mobile</em> project that mocks the dependencies when testing your <strong>MobileController</strong>.</p>
  
  <p>Also you want to create some tests for your <strong>CustomerController</strong>. This class depends upon <strong>MobileController</strong>. You decide not to mock <strong>MobileController</strong>. Instead you mock <strong>MobileController</strong>'s dependencies: <strong>MobileService</strong>. You create a second <strong>MobileServiceDouble implements IMobileService</strong>.</p>
  
  <p>Your tests run fine and you decide to refactor duplicated code. So what's a good way to eliminate your duplicates?</p>
  
  <p>You can't put your <strong>MobileServiceDouble</strong> into your <em>testutils</em> because otherwise you would have a circular dependency: <em>mobile</em> &lt;--> <em>testutils</em>
  Your MobileServiceDouble implements IMobileService that is located in <em>mobile</em>.</p>
  
  <p><a href="https://i.stack.imgur.com/WRchr.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WRchr.jpg" alt="enter image description here"></a></p>
  
  <p>You could move the interface for your <strong>MobileService</strong>. But you don't want to have any production related interfaces in your <em>testutil</em> project.</p>
  
  <p><a href="https://i.stack.imgur.com/vLdE9.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vLdE9.jpg" alt="enter image description here"></a></p>
  
  <p>I think it's a very minimal example for a real world issue in many systems and I'm sure there are great ideas how to resolve it.</p>
  
  <p>What do you suggest?</p>
  
  <p><strong>Update 1</strong>:
  I change the dependencies a bit to point out that it's not a problem that exists because I decided to use the real MobileController within my tests:</p>
  
  <p>Imagine there are 3 packages:</p>
  
  <ul>
  <li>Project <em>ServiceRegistry</em></li>
  <li>Project <em>Mobile</em> depends upon <em>ServiceRegistry</em> and <em>TestUtils</em></li>
  <li>Project <em>Customer</em> depends upon <em>ServiceRegistry</em> and <em>TestUtils</em></li>
  <li>Project <em>TestUtils</em></li>
  </ul>
  
  <p><em>Mobile</em> and <em>Customer</em> are projects that have unit tests. So both want to mock for example the <strong>ServiceRegistryManager</strong> class of the <em>ServiceRegistry</em> project.</p>
  
  <p><a href="https://i.stack.imgur.com/GgruY.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GgruY.jpg" alt="enter image description here"></a></p>
  
  <p>The projects Customer and Mobile creates ServiceRegistryManagerFakes. Same szenario: how to refactor this duplication? I move the fake class to <em>TestUtils</em>.</p>
  
  <p><a href="https://i.stack.imgur.com/TjpkL.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TjpkL.jpg" alt="enter image description here"></a></p>
  
  <p>The result is a circular dependency. While I'm writing I'm thinking about a FakeTest project that could solve my issue:</p>
  
  <p><a href="https://i.stack.imgur.com/l8ZU1.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l8ZU1.jpg" alt="enter image description here"></a></p>
  
  <p>At this point my dependencies and my projects increased. It feels very fragile because if my ServiceRegistry needs a Fake class then I need to create another <em>testFake2</em> project:</p>
  
  <p><a href="https://i.stack.imgur.com/xTHmz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xTHmz.jpg" alt="enter image description here"></a></p>
  
  <p>If the dependency of my new Project <em>TestFakes2</em> adds a dependency to a class that depends upon my <em>ServiceRegistry</em> then I will run again into issues.</p>
  
  <p><a href="https://i.stack.imgur.com/oLaO4.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oLaO4.jpg" alt="enter image description here"></a></p>
  
  <p>So I always have to pay attention when I refactor my classes. I would have to create a lot of packages just because of circular dependencies. My target is to find a good best practice to minimize my effort regarding refactorings and project dependencies.</p>
  
  <p>Looking for more ideas :).</p>