Pressure
Testing
Errors
Direct
Inverse
Test-Driven Development by Example, Kent Beck
Wikipedia
public class Dummy {
public void dummyMethod() {
return DummyRepository.getInstance().someMethod();
}
}
public class Dummy {
private repository = DummyRepository.getInstance();
public void setRepository(DummyRepository value) {
this.repository = value;
}
public void dummyMethod() {
return this.repository.someMethod();
}
}
public class SomeClass {
public void someMethod(int param) {
if (param > 0 || param < 0) {
return param * 2;
}
return 10;
}
}
public class SomeClassTest {
private SomeClass someClass = new SomeClass();
@Test
public void someMethodGreaterThanZero() throws Exception {
int response = this.someClass.someMethod(10);
assertEqual(20, response);
}
@Test
public void someMethodLessThanZero() throws Exception {
int response = this.someClass.someMethod(-10);
assertEqual(-20, response);
}
}
Line coverage
Branch coverage
def "should throw RestException when FECHA_NAC not exists"() {
given:
Variables variables = new Variables();
variables.add(new VariableItem("FECHA_VUELTA", "10/4/2010"));
variables.add(new VariableItem("FECHA_IDA", "10/4/2010"));
repository.generateEstimation(_ as Variables) >> new Estimation()
when:
this.estimationsService.generateEstimation(variables);
then:
notThrown(RestException)
}
@Test(expected = RestException.class)
public void shouldThrowRestExceptionWhenFECHA_NAC_NotExists() throws Exception {
Variables variables = new Variables();
variables.add(new VariableItem("FECHA_VUELTA", "10/4/2010"));
variables.add(new VariableItem("FECHA_IDA", "10/4/2010"));
when(estimationsRepository.generateEstimation(variables)).thenReturn(new Estimation());
this.estimationsService.generateEstimation(variables);
}
def "Encrypt"() {
expect:
CustomHash.encrypt(a) == b
where:
a || b
'abc' || 'YWJj'
'' || ''
null || ''
}
@Test(expected = RestException.class)
public void shouldThrowRestExceptionWhenFECHA_NAC_NotExists() throws Exception {
Variables variables = new Variables();
variables.add(new VariableItem("FECHA_VUELTA", "10/4/2010"));
variables.add(new VariableItem("FECHA_IDA", "10/4/2010"));
when(estimationsRepository.generateEstimation(variables)).thenReturn(new Estimation());
this.estimationsService.generateEstimation(variables);
}