1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

一定の基準をもったテスト

Last updated at Posted at 2013-08-18

基本ソース


import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ParameterizedTest {

	@Parameters
	public static final List<Object[]> parameters() {
		return Arrays.asList(new Object[][] {
				{ 1, 2, 3 },
				{ 2, 3, 5 },
		});
	};

	int a;
	int b;
	int c;

	public ParameterizedTest(int a, int b, int c) {
		this.a = a;
		this.b = b;
		this.c = c;
	}

	@Test
	public void test() {
		assertEquals(a + b, c);
	}

}

例外チェックする場合

package jp.mirageworld.webapps.util;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ParameterizedTest {

	@Parameters
	public static final List<Object[]> parameters() {
		return Arrays.asList(new Object[][] {
				{ 1, 2, 3, "X", NumberFormatException.class },
				{ 2, 3, 5, "0", null },
		});
	};

	int a;
	int b;
	int c;
	String n;
	Class<?> clazz;

	public ParameterizedTest(int a, int b, int c, String n, Class<?> clazz) {
		this.a = a;
		this.b = b;
		this.c = c;

		this.n = n;
		this.clazz = clazz;
	}

	@Test
	public void test() {
		try {
			assertEquals(a + b, c);
			Long.parseLong(n);
			assertNull(clazz);
		} catch (Exception e) {
			assertEquals(e.getClass(), clazz);
		}
	}

}

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?