简体   繁体   中英

Junit testing with multiple if statements

Hey I am stuck in this homework junit exercise. I find it hard with the many if statements. Would be glad for some help. cheers. The idea with junit testing is that you call the methods and you test the variables.

package testing;

import java.util.Map.Entry;

public class HashMap<K,V> {
 static final int MAXIMUM_CAPACITY = 1 << 10;
 transient Entry<K,V>[] table;
 int threshold;
 final float loadFactor;

public HashMap(int initialCapacity, float loadFactor) {
  if (initialCapacity < 0)
    throw new IllegalArgumentException("Illegal initial capacity: " +
  initialCapacity);
  if (initialCapacity > MAXIMUM_CAPACITY)
    initialCapacity = MAXIMUM_CAPACITY;
  if (loadFactor <= 0 || Float.isNaN(loadFactor))
    throw new IllegalArgumentException("Illegal load factor: " + loadFactor);

  // Find a power of 2 >= initialCapacity
  int capacity = 1;
  while (capacity < initialCapacity)
   capacity <<= 1;
 this.loadFactor = loadFactor;
 threshold = (int)(capacity * loadFactor);
 table = new Entry[capacity];
}

public int getCapacity() {
  return table.length;
}
}

Well, in the real world, you would rather work the other way round: you would do TDD. Meaning: you think about an (ideally small) feature that your production code should have. Then you write a test for that, and you check that the test fails. Then you implement the feature, and now the test should pass.

Writing tests "after the facts" is most often less helpful. But in general: not a big deal. You can look at your code, and observe what is doing. Then you write tests to check that behavior.

Like:

@Test(expected=IllegalArgumentException.class)
public void testWithNegativeCapacity() {
  new HashMap<String, String>(-1, 0.5);
}

First you write tests that check that your code that validates inputs throws up as expected.

Then you would continue, for example like

@Test
public void testForExpectedCapacity() {
  assertThat(new HashMap<String, String>(10, 0.5).getCapacity(), is(whatever));
}

Where:

  • assertThat is one of the many "asserts" that you can use
  • is() is a hamcrest matcher that you use with assertThat, leading to readable test code
  • whatever would be the value you expect that the capacity is when calling that constructor with 10 and 0.5 (I am to lazy to compute the real expected result)

Finally: of course you can look at the implementation of the production code to derive tests. But you should also look at the contract of that code.

For example its documentation: try to understand what the code is supposed to do, and write tests that verify if these claims "when you do this, code does that" really hold.

Both sides are important, and only together you can get to a complete test suite. You want to ensure that the code does what it should do, and you want to verify that "how it is done" is correct!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM