Test sparametryzowany.

0

Witam.
Muszę wykonać test sparametryzowany dla klasy która dodaje dwa inty:

public class dodawanie {
    
    public int dod(int x, int y)
    {
        return x + y;
    }
    
     public static void main(String [] args)
    {
        dodawanie d = new dodawanie();
    int x = 10; int y = 5;
    System.out.println("Wynik="+d.dod(x, y));
            }
}

Mógłby mi ktoś pomóc bo nie mam pojęcia jak to wykonać. Środowisko NetBeans.

1

W JUnit:

@Test
public void testDod2i2() {
  testDod(2,2,4);
}

@Test
public void testDod2i3() {
  testDod(2,3,5);
}

private void testDod(int i, int j, int w) {
   assertEquals((long) w, (long) dod(i,j));
}

Alternatywnie: http://junit.sourceforge.net/javadoc/org/junit/runners/Parameterized.html

1

Najłatwiej użyć TestNG:

import static org.testng.Assert.assertEquals;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class MyTest {

	@Test(dataProvider = "testData")
	public void shouldDodajAddTwoValues(int a, int b, int result) {
		assertEquals(Kalklator.dodaj(a, b), result);
	}

	@DataProvider
	public Object[][] testData() {
		return new Object[][] { //
		//
				new Object[] { 1, 1, 2 }, //
				new Object[] { 2, 2, 4 } //
		};
	}
}

class Kalklator {

	public static int dodaj(int a, int b) {
		return a + b;
	}
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1