16. Nice!
●
We can check state
●
More meaningful units, the CDI beans!
●
A one unit integration test
●
Mocks can replace beans easily
●
Custom builds
●
Descriptors and classes
16
17. But...
●
Still a class per mock
●
Various @Deployments
●
The single implementation dilema
●
We can't use mock frameworks anymore!
17
20. Write a portable extension!
●
Choose certain injection points
●
Resolve beans to custom mocks
●
Define mocks at runtime
●
Power of mock frameworks
●
Thanks to our clever Expert Group!
20
21. A “real” case
@Named
public class MyBean {
@Inject
private MyDependency myDependency;
public String calculate(int a, int b) {
return "multiplication is " + myDependency.multiply(a, b);
}
}
@Named
public class MyDependency {
public int multiply(int a, int b) {
return a*b;
}
}
21
22. A code is worth.. you know
public class MyTest extends WeldMockTest {
@Test
public void testMock() {
mockContext.addExpectations(new Expectations<MyDependency>() {
@Override
public void defineExpectations(MyDependency mock) {
when(mock.multiply(2, 3)).thenReturn(5);
}
});
MyBean bean = getBean(MyBean.class);
String calculate = bean.calculate(2, 3);
assertThat(calculate, is("multiplication is 5"));
calculate = bean.calculate(2, 4);
assertThat(calculate, is(not("multiplication is 8")));
}
}
22
23. And a spy
public class MyTest extends WeldMockTest {
@Test
public void testSpy() {
mockContext.addExpectations(DoubleType.SPY,
new Expectations<MyDependency>() {
@Override
public void defineExpectations(MyDependency mock) {
when(mock.multiply(2, 3)).thenReturn(5);
}
});
MyBean bean = getBean(MyBean.class);
String calculate = bean.calculate(2, 3);
assertThat(calculate, is("multiplication is 5"));
calculate = bean.calculate(2, 4);
assertThat(calculate, is("multiplication is 8"));
}
}
23
24. What we get
●
It's an extension
●
Arquillian friendly
●
Can easy the pain of @Deployments
●
Mock frameworks and all their greatness!
●
Choose precisely where to meddle
●
Fine grained integration test
●
Get a hold of your test code base!
24