Always do a strong assertion against the returned state from a unit. Avoid assertion like the collection size shall be greater than zero. A better assertion could be checking the size of the collection and content of the collection.
Complete source code can be found here.
package com.ourownjava.unit.under.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.List; import org.junit.Before; import org.junit.Test; import com.ourownjava.model.Apple; /** * * @author Sanju Thomas * */ public class AppleServiceTest { private AppleService appleService; @Before public void setUp(){ appleService = new AppleService(); } @Test public void shouldGet100ApplesBadAssertion(){ final List apples = appleService.getApple(100); assertNotNull(apples); } @Test public void shouldGet100ApplesBadAssertion1(){ final List apples = appleService.getApple(100); assertTrue(apples.size() > 0); } @Test public void shouldGet100ApplesGoodAssertion(){ final List apples = appleService.getApple(100); assertEquals(100, apples.size()); } } package com.ourownjava.unit.under.test; import java.util.ArrayList; import java.util.List; import com.ourownjava.model.Apple; /** * * @author Sanju Thomas * */ public class AppleService { public List getApple(final Integer numberOfApples){ final List apples = new ArrayList(); for(int i = 0; i < numberOfApples; i++){ final Apple apple = new Apple(); apples.add(apple); } return apples; } } package com.ourownjava.model; /** * * @author Sanju Thomas * */ public class Apple {}
Информация Derila kupiti v Sloveniji на нашем сайте.