Upload
truls-jorgensen
View
579
Download
2
Embed Size (px)
Citation preview
testdrevet javautvikling
på objektorienterteskinner
Anders Sveen og Truls JørgensenJavaZoneX
Java er ikke morsomt eller sexy
Java7 er ikke morsomt eller sexy
hva du gjør er viktigere
rask oppstart
kjapp feedback
mindre kompleksitet
lett å integrere med tester
lite påtrengende
enkelt å utvide
enkelt å gå rundt
open source
en følelse av å være
produktiv
enkelt å legge til nye
features
endringsdyktig
objektorientertkode
isolasjon
ansvarsdeling
if (soknad.getStatus().equals(VENTER)) {
soknad.setGodkjentDato(new Date());
soknad.setStatus(GODKJENT);
}
VS
if (soknad.kanGodkjennes()) {
soknad.godkjenn();
}
@Test
public skalTesteGodkjenning() {
}
@Test
public skalTesteGodkjenning() {
PersonRepository personRepo =
mock(PersonRepository.class);
when(personRepo)
.getPerson(any(Long))
.returnValue(TestDataProvider
.standardPerson().build());
}
@Test
public skalTesteGodkjenning() {
PersonRepository personRepo =
mock(PersonRepository.class);
when(personRepo)
.getPerson(any(Long))
.returnValue(TestDataProvider
.standardPerson().build());
SoknadService service =
new SoknadServiceImpl(personRepo);
Soknad soknad = TestDataProvider
.standardSoknad()
.status(GODKJENT).build();
}
@Test
public skalTesteGodkjenning() {
PersonRepository personRepo =
mock(PersonRepository.class);
when(personRepo)
.getPerson(any(Long))
.returnValue(TestDataProvider
.standardPerson().build());
SoknadService service =
new SoknadServiceImpl(personRepo);
Soknad soknad = TestDataProvider
.standardSoknad()
.status(GODKJENT).build();
assertFalse(service.godkjennSoknad(soknad));
}
@Test
public void skalTesteGodkjenning() {
Soknad soknad =
TestDataProvider
.standardSoknad()
.godkjent().build();
assertFalse(soknad.kanGodkjennes());
}
public class OrganizationNumber {
@NotBlank
@Digits(integer=9, fraction=0)
@Min(value=100000000)
@ValidOrganizationNumber
private String value;
}
@Valid
private OrganizationNumber organizationNumber;
public class OrganizationNumber {
@NotBlank
@Digits(integer=9, fraction=0)
@Min(value=100000000)
@ValidOrganizationNumber
private String value;
}
@Valid
@NotNull(groups=GodkjennSoknadContext.class)
private OrganizationNumber organizationNumber;
@RequestMapping(method =
RequestMethod.POST)
public String registerOrganization(
@Valid @ModelAttribute
Organization organization,
Errors errors) {
...
}
trygghettil å gjøre endringer
lettå skrive tester
Organization org =
OrganizationTestDataProvider
.defaultOrganization();
Organization org =
OrganizationTestDataProvider
.defaultOrganization()
.name(”¤Invalid")
.build();
Organization org =
OrganizationTestDataProvider
.defaultOrganization()
.withoutEmployees()
.build();
Organization org =
OrganizationTestDataProvider
.defaultOrganization ()
.withoutEmployees()
.build(entityManager);
@RequestMapping(method = RequestMethod.POST)
public String registerRequestInformation(
@Valid @ModelAttribute
InformationRequest informationRequest,
Errors errors) {
}
@RequestMapping(method = RequestMethod.POST)
public String registerRequestInformation(
@Valid @ModelAttribute
InformationRequest informationRequest,
Errors errors) {
this.orgRepo
.addInformationRequest(informationRequest);
}
@RequestMapping(method = RequestMethod.POST)
public String registerRequestInformation(
@Valid @ModelAttribute
InformationRequest informationRequest,
Errors errors) {
if (errors.hasErrors()) {
return VIEW_INFORMATION_REQUEST_FORM;
}
this.orgRepo
.addInformationRequest(informationRequest);
}
@RequestMapping(method = RequestMethod.POST)
public String registerRequestInformation(
@Valid @ModelAttribute
InformationRequest informationRequest,
Errors errors) {
if (errors.hasErrors()) {
return VIEW_INFORMATION_REQUEST_FORM;
}
this.orgRepo
.addInformationRequest(informationRequest);
return redirectTo(URL_INFORMATION_REQUEST);
}
@RequestMapping(method = RequestMethod.GET,
value = ”ir/{requestId}/received”)
public String registerRequestAsReceived(
@PathVariable Long requestId ) {
InformationRequest informationRequest =
this.orgRepo
.getInformationRequest( requestId );
informationRequest.received();
return redirectTo(
url("/", URL_INFORMATION_REQUEST));
}
@RequestMapping(method = RequestMethod.GET,
value = ”ir/{requestId}/received”)
public @ResponseBody InformationRequest
registerRequestAsReceived(
@PathVariable Long requestId) {
InformationRequest informationRequest =
this.orgRepo
.getInformationRequest(requestId);
return informationRequest;
}
oppsummert:
github.com/anderssv/jzx-weboo
http://nat.truemesh.com/archives/000714.html
http://martinfowler.com/bliki/ObjectMother.html
http://docs.codehaus.org/display/JETTY/Embedding+Jetty
http://jcp.org/en/jsr/detail?id=303
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html
Testdata builders
Object mother
Jetty embedded
JSR 303
Spring MVC
14.-15. novemberRadisson Blu Plaza, Oslo
http://smidig2011.no