K125:
私は店の名前ビデオタイプの配列が含まれVideoStoreクラスのテスト関数にしようとしています。私は最初の4つの試験をパスのJUnitテスト、他のスローにNullPointer例外としてテストクラスを実行すると。私はそれらを個別に実行すると、各テストは合格します。私は私のテストクラスを与えています。
私はすでに代わりに@Before注釈の@BeforeClassを使用して試してみました。私はまた別途東@Test機能でインスタンス化しようとしています。
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import tm2.VideoStore;
public class VideoTest {
VideoStore vs;
@Before
public void before() {
vs = new VideoStore();
vs.addVideo("LifeOfGuy");
}
@Test
public void testAddVideo() {
assertEquals("LifeOfGuy",vs.store[0].videoName);
}
@Test
public void testDoCheckout() {
vs.doCheckout(vs.store[0].videoName);
assertTrue(vs.store[0].checkout);
}
@Test
public void testDoReturn() {
vs.doReturn("LifeOfGuy");
assertFalse(vs.store[0].checkout);
}
@Test
public void receiveRating() {
vs.receiveRating("LifeOfGuy", 5);
assertEquals(5,vs.store[0].rating);
}
}
VideoStoreクラス:
public class VideoStore {
public Video[] store = new Video[10];
static int count = 0;
public void addVideo(String name) {
store[count++] = new Video(name);
}
public void doCheckout(String name) {
for(int i=0; i<count; i++) {
if((store[i].videoName).equals(name)) {
store[i].doCheckout();
break;
}
}
}
public void doReturn(String name) {
for(int i=0; i<count; i++) {
if((store[i].videoName).equals(name)) {
store[i].doReturn();
break;
}
}
}
public void receiveRating(String name, int rating) {
for(int i=0; i<count; i++) {
if((store[i].getName()).equals(name)) {
store[i].receiveRating(rating);
}
}
}
void listInventory() {
System.out.println("----------------------------------------");
System.out.println("Video Name | Checkout Status | Rating ");
for(int i=0; i<count; i++) {
System.out.println(store[i].videoName+" | "+store[i].getCheckout()+" | "+store[i].getRating());;
}
System.out.println("----------------------------------------");
}
}
JUnitの結果:---- 2. testDoCheckout java.lang.NullPointerExceptionが3 testDoReturnのjava.lang.NullPointerException 4. testreceiveRatingのjava.lang.NullPointerExceptionを渡すtestAddVideo 4/4エラー3つの障害0 1を実行します。
それぞれが個別に渡します
ヨアキムダニエルソン:
あなたのcount
それはそれぞれのテストのために増加し、新しいビデオは、各テストのために、アレイ内の別の場所に追加されますので、変数は静的です
それは非静的ください
private int count = 0;