Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a classic case of testing showing up a potential flaw in design. You cannot mock <code>JodaTime</code> because you have a hard-wired dependency to these classes in your class-under-test.</p> <p>Have a look at the <a href="http://en.wikipedia.org/wiki/Solid_%28object-oriented_design%29" rel="noreferrer">SOLID principles</a> to understand why this could be a problem (especially in this case the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle" rel="noreferrer">Dependency Inversion Principle</a>). If you injected <code>JodaTime</code> somewhere as a dependency, then in your unit test you would be able to replace a real instace of it with a <a href="http://martinfowler.com/articles/mocksArentStubs.html" rel="noreferrer">mock, stub or spy</a> as appropriate.</p> <p><strong>However:</strong> <code>JodaTime</code> is something that is highly unlikely to be injected with anything else in the production environment, no matter how long it is live for. Instead, in this case you would probably be better served with the <a href="http://codebetter.com/jeremymiller/2006/12/03/composed-method-pattern/" rel="noreferrer">Composed Method Design Pattern</a>. Here, you would extract whatever calculation/algorithm you use to generate the <code>printjobName</code> to another method (I can't see how you do it here because your code snippet never assigns a value to that variable). Then you can spy (partial mock) your class under test to only mock that method and return a fixed value, regardless of the real date time that <code>JodaTime</code> is delivering, for instance:</p> <pre><code>public class PrintProcessor { ... public String getPrintJobName(Shipper shipper) { String printJobName = null; String timeHash = this.getTimeHash(); if (this.isBeforeFourPM()) { switch(shipper) { printJobName = // Do something with timeHash to generate name } } else { ... } return printJobName; } public boolean isBeforeFourPM() { return (jodaTime.getCurrentDateTimeEST().isBefore(jodaTime.getFourPM_EST()) || jodaTime.getCurrentDateTimeEST().isAfter(jodaTime.getSevenPM_EST())); } public String getTimeHash() { ... // Do something to hash the time value in to a String } } </code></pre> <p>Now you can write in your test:</p> <pre><code>@Test public void testGetPrintJobNameBeforeFourPM() { PrintProcessor concretePrintProcessor = new PrintProcessor(); PrintProcessor printProcessor = spy(concretePrintProcessor); doReturn(true).when(printProcessor).isBeforeFourPM(); String printJobName = printProcessor.getPrintJobName(Shipper.X); assertEquals("XNCRMNCF", printJobName); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload