Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I completely second Tomasz Nurkiewicz answer, so I'll say that rather than repeating everything he said.</p> <p>A couple more points:</p> <p>Don't forget to test error cases. You can consider something like that:</p> <pre><code>@Test public void throwExceptionWhenConditionOneExist() { // setup // ... try { classUnderTest.doSomething(conditionOne); Assert.fail("should have thrown exception"); } catch (IllegalArgumentException expected) { Assert.assertEquals("this is the expected error message", expected.getMessage()); } } </code></pre> <p>Also, it has GREAT value to start writing your tests before even thinking about the design of your class under test. If you're a beginner on unit-testing, I cannot emphasize enough learning this technique at the same time (this is called TDD, test-driven development) which proceeds like that:</p> <ul> <li>You think about what user case you have for your user requirements</li> <li>You write a basic first test for it</li> <li>You make it compile (by creating needed classes -including your class under test-, etc.)</li> <li>You run it: it should fail</li> <li>Now you implement the functionality of the class under test that will make it pass (and <strong>nothing more</strong>)</li> <li>Rinse, and repeat with a new requirement</li> </ul> <p>When all your requirements have passing tests, then you're done. You NEVER write anything in your production code that doesn't have a test before (exceptions to that is logging code, and not much more).</p> <p>TDD is invaluable in producing good quality code, not over-engineering requirements, and making sure you have a 100% functional coverage (rather than line coverage, which is usually meaningless). It requires a change in the way you consider coding, that's why it's valuable to learn the technique at the same time as testing. Once you get it, it will become natural.</p> <p>Next step is looking into Mocking strategies :)</p> <p>Have fun testing.</p>
    singulars
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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