2

is there any guideline for how extensive the expected has to be? Let's assume I have the following constructor for a simple card deck.

public class deck {

public card[] deckCards = new card[52];

public deck() {

    int i = 0;

    for(int suit = 0; suit<4; suit++) {
        for(int rank=0; rank<13; rank++) {
            this.deckCards[i] = new card(rank,suit);
            i = i+1;
        }
    }
}}

Now, if I want to do a Junit-Test, then I'm probably going to use assertArrayEquals(), which means that I need to create the "expected array" first. In my case this would be 52 cards (4 suits times 13 ranks). Now, this would be quite tedious to write down. How am I supposed to test such long arrays (what if it's even longer)?

On a gut level I would test

  1. if the array has 52 non-null values.
  2. test the border cases, which means "A heart & 2 heart", "A clubs & 2 clubs", "A diamond & 2 diamond", "A spade & 2 spade".

So, is it really tedious manual work, to write down the "expected", or are there any conventions on how we approach that?

cobby
  • 484
  • 3
  • 18
  • You usually unit test the _public_ interface of your class; in your example, there are no public methods exposed. How would/could you test this class? – Mick Mnemonic Aug 03 '15 at 21:23
  • Tests have a cost, for sure. In your example, if you test border cases + size, nothing ensure the whole desk is correct. IMHO, test coverage is a matter of acceptance, you need to put the cursor somewhere between no tests and 100% coverage keeping in mind the cost/risk factor –  Aug 03 '15 at 21:24
  • Related: [What is a reasonable code coverage % for unit tests (and why)?](http://stackoverflow.com/questions/90002/what-is-a-reasonable-code-coverage-for-unit-tests-and-why) and [Fowler on Test coverage](http://martinfowler.com/bliki/TestCoverage.html). – Mick Mnemonic Aug 03 '15 at 21:27
  • What do you mean "...test such long arrays(what if it's even longer)?". Are we still talking about decks of cards? If not, then what? Context is important. In general, it is best to test all of the common public interfaces to some module, i.e. assert that what goes in (via a public interface) and what comes out is correct. – James Wierzba Aug 03 '15 at 21:39

0 Answers0