gitを使用したソースコードのバージョン管理
プロジェクトを右クリックして[チーム]-[プロジェクトの共用] を選択する。
GitとSVNがあるのでGitを選択して「次へ」をクリックする。
リポジトリーの「作成」をクリック。
適当なフォルダを選択して「完了」をクリック。
.gitignore ファイルを設定する。
.gitignoreファイルには、Gitの管理対象外にするファイルを記述する。
例えばクラスファイルなどはコンパイルで自動生成されるので、管理不要である。
.gitignoreファイルを表示するには、パッケージエクスプローラの右にある「▽」をクリックする。
メニューで「フィルター」を選択して「.*リソース」のチェックを外す。
/bin/ /.settings .project .classpath *.class *.jar
Gitにファイルを登録することを「コミット」と呼ぶ。
自動販売機の実装に戻る。
前回、テストがエラーになる状態で終わっていたので、テストを実行してみる。
10円を投入して投入金額の合計を取得すると0円なのでエラーになっている。
package jp.abc;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;
class VendingMachineTest {
@Test
void お金を投入する() {
VendingMachine vm = new VendingMachine();
vm.put(10);
int total = vm.getTotal();
assertThat(total, is(10));
}
}
fakeでgetTotal()で10を返すようにすればテストにパスする。
package jp.abc;
public class VendingMachine {
public void put(int i) {
}
public int getTotal() {
return 10;
}
}
テストを追加する。
package jp.abc;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;
class VendingMachineTest {
@Test
void お金を投入する() {
VendingMachine vm = new VendingMachine();
vm.put(10);
int total = vm.getTotal();
assertThat(total, is(10));
vm.put(100);
total = vm.getTotal();
assertThat(total, is(110));
}
}
テストにパスするように実装を行う。
テストにパスしたタイミングでソースコードをGitに登録する。
登録することをコミットという。
プロジェクトを右クリックし、[チーム]-[コミット]を選択する。
コミットメッセージを入力する。
登録したいファイルがステージングにあることを確認して[コミット]をクリックする。
以下、投入できないお金と返金を追加したテスト。
package jp.abc;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;
class VendingMachineTest {
@Test
void お金を投入する() {
VendingMachine vm = new VendingMachine();
vm.put(10);
int total = vm.getTotal();
assertThat(total, is(10));
vm.put(100);
total = vm.getTotal();
assertThat(total, is(110));
}
@Test
void 投入できないお金() {
VendingMachine vm = new VendingMachine();
vm.put(1);
int total = vm.getTotal();
assertThat(total, is(0));
}
@Test
void 返金する() {
VendingMachine vm = new VendingMachine();
vm.put(1000);
int total = vm.getTotal();
assertThat(total, is(1000));
int refund = vm.refund();
total = vm.getTotal();
assertThat(total, is(0));
assertThat(refund, is(1000));
}
}
1円を投入されても合計金額に加算しない。
package jp.abc;
public class VendingMachine {
private int total;
public VendingMachine() {
total = 0;
}
public void put(int i) {
if (i == 1) return;
total += i;
}
public int getTotal() {
return total;
}
public int refund() {
return 0;
}
}