Skip to content

Instantly share code, notes, and snippets.

@sezemiadmin
Created August 23, 2018 08:21
Show Gist options
  • Save sezemiadmin/aadacbb2ab0b3b4c46c5bdbb91349f23 to your computer and use it in GitHub Desktop.
Save sezemiadmin/aadacbb2ab0b3b4c46c5bdbb91349f23 to your computer and use it in GitHub Desktop.
単体テストとテストケースの作り方のサンプル
import java.math.BigDecimal;
public class Calculator {
public static int calc (int price, price, int count) throws Exception {
if (count > 1) {
return price * count;
}
throw new Exception();
}
public static int discount(int price, int count) throws Exception {
return new BigDecimal BigDecimal(calc (price, count)).multiply(
new BigDecimal("0.9")). setScale (0,
BigDecimal.ROUND_HALF_UP). intValue intValue ();
}
}
import static org.hamcrest.CoreMatchers.*; // 結構書いてないと色々エラーになるのです
import static org.junit.Assert.*;
import org.junit.Test;
public class CalculatorTest {
@Test // Junit4以降は@が無いとテスト対象になりません~
public void calcの正常系() throws Exception{ // テスト名は日本語とかで落ちたテストがわかりやすいですよ~
assertThat(Calculator.calc(34, 9), is(306));
}
@Test (expected = Exception.class) // 例外クラスを指定することもできます
public void calcの数量が0の場合は例外が発生する() throws Exception{
assertThat(Calculator.calc(0, 3), is(0));
}
@Test
public void calcの正常系() throws Exception {
// given で準備
int price = 34;
int count = 9;
int expected = 306;
// when 実施
int actual = Calculator.calc(price, count);
// then 検証
assertThat(actual, is(expected));
}
package junit.calculator;
import static org.junit.Assert.*;
import org.junit.Test;
public class CalculatorTest {
@Test // Junit4以降は@が無いとテスト対象になりません~
public void test() { // テスト名は日本語とかで書いておくとわかりよい
fail("Not yet implemented");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment