17. 17Pこれにテストケースを仕掛けてみると・・・
public
class
A
{
public
void
x()
{
B
b
=
new
B();
b.y();
}
}
public
class
B
{
public
void
y()
{
C
c
=
new
C();
c.z();
}
}
public
class
C
{
public
void
z()
{
DB操作とか
}
}
TestCaseA
(A,
B,
Cの複合テスト)
Class AにテストケースつくるとB, Cも必ず
くっついて来て単独テストにならない。
TestCaseB
(BとCの複合テスト) TestCaseC
(これはまだ単独)
19. 19PDI(JavaでSpringFramworkの場合)
public
class
Aimpl
implements
A
{
@Autowired
private
B
b;
public
void
x()
{
b.y();
}
}
public
class
Bimpl
implments B
{
@Autowired
private
C
c;
public
void
y()
{
c.z();
}
}
public
class
Cimpl
implements
C
{
public
void
z()
{
DB操作とか
}
}
実装クラスとインタフェースを分離離、コール先のインタフェースのみ認識識
し、実装インスタンス(Dependency)は外部から注⼊入(Injection)。
20. 20Pこれにテストケースを仕掛けてみると・・・
public
class
Aimpl
implements
A
{
@Autowired
private
B
b;
public
void
x()
{
b.y();
}
}
今まではA単独のテストができなかったが、依存するBをモック化するこ
とでテストしたい処理理だけに対して確認を⾏行行えるようになる。
class
TestCaseA {
def testA()
{
def a
=
new
Aimpl()
//モック注入
a.b =
Mock(B.class)
//テスト実行、assert
a.x()
}
}