Hva var egentlig sprsmlet Johannes Brodwall Fit Nesse
Hva var egentlig spørsmålet? Johannes Brodwall
Fit. Nesse eksempel #1
En test @Test public void femere. Skal. Gi. Antall() throws Exception { assert. Equals(0, score. For. Kast("femere", 1, 2, 2, 2, 1)); assert. Equals(10, score. For. Kast("femere", 1, 2, 5, 5, 1)); assert. Equals(15, score. For. Kast("femere", 5, 2, 5, 5, 1)); } @Test public void seksere. Skal. Gi. Antall() throws Exception { assert. Equals(0, score. For. Kast("seksere", 1, 2, 2, 2, 1)); assert. Equals(12, score. For. Kast("seksere", 1, 2, 6, 6, 1)); assert. Equals(18, score. For. Kast("seksere", 6, 2, 6, 6, 1)); } @Test public void fullt. Hus() throws Exception { assert. Equals(0, score. For. Kast("fullt_hus", 1, 2, 3, 4, 5)); assert. Equals(11, score. For. Kast("fullt_hus", 1, 3, 3, 1, 3)); assert. Equals(28, score. For. Kast("fullt_hus", 5, 6, 6, 5, 6)); } © Steria
En test (vel. . . tre, egentlig) @Test public void femere. Skal. Gi. Antall() throws Exception { assert. Equals(0, score. For. Kast("femere", 1, 2, 2, 2, 1)); assert. Equals(10, score. For. Kast("femere", 1, 2, 5, 5, 1)); assert. Equals(15, score. For. Kast("femere", 5, 2, 5, 5, 1)); } @Test public void seksere. Skal. Gi. Antall() throws Exception { assert. Equals(0, score. For. Kast("seksere", 1, 2, 2, 2, 1)); assert. Equals(12, score. For. Kast("seksere", 1, 2, 6, 6, 1)); assert. Equals(18, score. For. Kast("seksere", 6, 2, 6, 6, 1)); } @Test public void fullt. Hus() throws Exception { assert. Equals(0, score. For. Kast("fullt_hus", 1, 2, 3, 4, 5)); assert. Equals(11, score. For. Kast("fullt_hus", 1, 3, 3, 1, 3)); assert. Equals(28, score. For. Kast("fullt_hus", 5, 6, 6, 5, 6)); } © Steria
Hva var egentlig spørsmålet? Johannes Brodwall Chief Scientist, Steria
Hva er krav?
”Hvorfor skal kunden ha noe løst? ” p 8 | 07/12/2020 Presentation title © Steria
Behovsforståelse p 9 | 07/12/2020 Presentation title © Steria
”Hva skal løse når? ” p 10 | 07/12/2020 Presentation title © Steria
Planlegging p 11 | 07/12/2020 Presentation title © Steria
”Hvordan skal systemet løse hver enkelt krav? ” p 12 | 07/12/2020 Presentation title © Steria
Kravspesifikasjon p 13 | 07/12/2020 Presentation title © Steria
Eksempel: Signering
Produkteier Utvikler Tester p 15 | 07/12/2020 Presentation title © Steria
Som en betaler, Ønsker jeg å signere mine oppdrag, Slik at ingen kan utgi seg for å være meg Produkteier Utvikler Tester p 16 | 07/12/2020 Presentation title © Steria
Hva med både ok signerte og feilsignerte oppdrag i en fil? Produkteier Utvikler Tester p 17 | 07/12/2020 Presentation title © Steria
Ummm. . . (hjelp? ) Produkteier Utvikler Tester p 18 | 07/12/2020 Presentation title © Steria
© Steria
Gitt at en fil med oppdrag 1 og 2 Og oppdrag 1 er korrekt signert Og oppdrag 2 er ikke er korrekt signert Når filen valideres Så skal oppdrag 1 behandles normalt Og kunden skal motta en kvittering med teksten ”oppdrag 2 avvist” Produkteier Utvikler Tester p 20 | 07/12/2020 Presentation title © Steria
Fit. Nesse eksempel #1 © Steria
Fit. Nesse eksempel #1 © Steria
Cu cum (rs pe ber c) Gitt at. . Når. . Så. . . Produkteier Utvikler Tester p 23 | 07/12/2020 Presentation title © Steria
Et faktisk eksempel til
Fra mitt nåværende prosjekt: ”Ta arbeidsoppgave” Gitt jeg går til arbeidslistebilde ”teamets arbeidsliste” Og jeg velger oppgaven med ”Fødselsnummer” lik ” 11111” Og jeg trykker på ”Plukk oppgave” Når jeg går til arbeidslistebilde ”min arbeidsliste” Så skal oppgavelisten inneholde 1 oppgave Og oppgavens ”Fødselsnummer” skal være ” 1111” © Steria
Fra mitt nåværende prosjekt: ”Ta arbeidsoppgave” Gitt jeg går til arbeidslistebilde ”teamets arbeidsliste” Og jeg velger oppgaven med ”Fødselsnummer” lik ” 11111” Og jeg trykker på ”Plukk oppgave” Når jeg går til arbeidslistebilde ”min arbeidsliste” Så skal oppgavelisten inneholde 1 oppgave Og oppgavens ”Fødselsnummer” skal være ” 1111” © Steria
Eksempel: Yatzy
Fullt hus skal gi 25 poeng Produkteier Utvikler Tester p 28 | 07/12/2020 Presentation title © Steria
© Steria
Fullt hus skal gi 25 poeng Produkteier Utvikler Tester p 30 | 07/12/2020 Presentation title © Steria
Regnes det som fullt hus om fem dice har samme pipcount Produkteier Utvikler Tester p 31 | 07/12/2020 Presentation title © Steria
WTF? !? Produkteier Utvikler Tester p 32 | 07/12/2020 Presentation title © Steria
For eksempel fem 4 -ere er ikke fullt hus Produkteier Utvikler Ok! Tester p 33 | 07/12/2020 Presentation title © Steria
© Steria
Utviklers tester
Gjenspeiler funksjonelle tester
HUSKELAPP TIL JOHANNES: GÅ TIL ECLIPSE NÅ
Gode enhetstester er også krav
JUnit eksempel #3: Repository @Test public void should. Retrieve. Same. Instance. For. Same. Key() throws Exception { Category inserted = new Category("A"); Serializable id = repository. insert(inserted); repository. flush. Changes(); Category retrieved 1 = repository. retrieve(Category. class, id); Category retrieved 2 = repository. retrieve(Category. class, id); Category retrieved 3 = repository. find(Category. class). iterator(). next(); retrieved 1. set. Category. Name("Z"); assert. Equals(retrieved 1. get. Category. Name(), retrieved 2. get. Category. Name()); assert. Equals(retrieved 1. get. Category. Name(), retrieved 3. get. Category. Name()); } © Steria
JUnit eksempel #3: Repository @Test public void should. Retrieve. Same. Instance. For. Same. Key() throws Exception { Category inserted = new Category("A"); Serializable id = repository. insert(inserted); Given a database with one category repository. flush. Changes(); Category retrieved 1 = repository. retrieve(Category. class, id); Category retrieved 2 = repository. retrieve(Category. class, id); Given I retrieved 3 retrieve this Category = category several times repository. find(Category. class). iterator(). next(); When I change one instance retrieved 1. set. Category. Name("Z"); assert. Equals(retrieved 1. get. Category. Name(), Then the others should be updated, too retrieved 2. get. Category. Name()); assert. Equals(retrieved 1. get. Category. Name(), retrieved 3. get. Category. Name()); } © Steria
JUnit eksempel #3: Repository @Test public void uncommitted. Inserts. Should. Be. Invisible. For. Other. Threads() { repository. begin. Transaction(); Category category = new Category("A"); repository. insert(category); repository. flush. Changes(); assert. Null(retrieve. In. New. Thread(Category. class, category. get. Id())); repository. commit(); assert. Equals(category, retrieve. In. New. Thread(Category. class, category. get. Id())); } © Steria
JUnit eksempel #3: Repository @Test public void uncommitted. Inserts. Should. Be. Invisible. For. Other. Threads() { repository. begin. Transaction(); Given a new while in a transaction Category. I insert category = newcategory Category("A"); repository. insert(category); repository. flush. Changes(); When I retrieve the category from another thread Then I should not be able to see it assert. Null(retrieve. In. New. Thread(Category. class, category. get. Id())); } repository. commit(); When I commit the transaction assert. Equals(category, retrieve. In. New. Thread(Category. class, When I retrieve the category from another category. get. Id())); thread Then I should be able to see it © Steria
JUnit eksempel #4: Websider @Test public void list. Products. Page. Should. Show. All() throws Exception { Product product 1 = new Product(unique. Name("product"), 12300); Product product 2 = new Product(unique. Name("product"), 300); repository. insert. All(product 1, product 2); repository. flush. Changes(); tester. begin. At("/products/"); tester. assert. Text. In. Element("products", product 1. get. Product. Name()); tester. assert. Text. In. Element("products", product 2. get. Product. Name()); } © Steria
JUnit eksempel #4: Websider @Test public void list. Products. Page. Should. Show. All() throws Exception { Product product 1 = new with Product(unique. Name("product"), 12300); Given two products unique names in the database Product product 2 = new Product(unique. Name("product"), 300); repository. insert. All(product 1, product 2); repository. flush. Changes(); When I go to the /products/ web page tester. begin. At("/products/"); tester. assert. Text. In. Element("products", product 1. get. Product. Name()); Then I should see both products product 2. get. Product. Name()); tester. assert. Text. In. Element("products", } © Steria
JUnit eksempel #5: Husk negative tester @Test public void price. Must. Be. Numeric() throws Exception { String old. Name = unique. Name("product"); int old. Price = 1234; Product product = new Product(old. Name, old. Price); Serializable id = repository. insert(product); repository. flush. Changes(); tester. begin. At("/products/" + id + "/edit. html"); tester. set. Text. Field("product. Name", unique. Name("product")); tester. set. Text. Field("price", "this is not a price!"); tester. submit(); tester. assert. Match. In. Element("error. Explaination", "[Pp]rice. *numeric"); Product stored = repository. retrieve(Product. class, id); assert. Equals(old. Price, stored. get. Price()); assert. Equals(old. Name, stored. get. Product. Name()); } © Steria
JUnit eksempel #5: Husk negative tester @Test public void price. Must. Be. Numeric() throws Exception { String old. Name = unique. Name("product"); int old. Price = 1234; Given a product in the database Product product = new Product(old. Name, old. Price); Serializable id = repository. insert(product); repository. flush. Changes(); When I go to the /products/<id>/edit web page And I go change the price to a negative value And I press submit tester. begin. At("/products/" + id + "/edit. html"); tester. set. Text. Field("product. Name", unique. Name("product")); tester. set. Text. Field("price", "this is not a price!"); tester. submit(); tester. assert. Match. In. Element("error. Explaination", "[Pp]rice. *numeric"); Then I should see an error message And the product should be unchanged in the database Product stored = repository. retrieve(Product. class, id); assert. Equals(old. Price, stored. get. Price()); assert. Equals(old. Name, stored. get. Product. Name()); } © Steria
Gitt at. . Når. . Så. . . Utvikler p 47 | 07/12/2020 Presentation title © Steria
Test først gir godt design
JUnit eksempel #1: Yatzy histogram @Test public void simple. Categories. Should. Be. Sum. Of. Matching. Dice() {. . . } @Test public void small. Strait() {. . . } @Test public void large. Strait() {. . . } @Test public void three. Of. AKind() {. . . } © Steria
JUnit eksempel #1: Yatzy histogram @Test public void three. Of. AKind() { assert. Equals(0, score. For("three_of_a_kind", 1, 1, 2, 2, 3)); assert. Equals(3, score. For("three_of_a_kind", 1, 1, 1, 2, 3)); assert. Equals(6, score. For("three_of_a_kind", 2, 2, 2, 3, 3)); assert. Equals(9, score. For("three_of_a_kind", 1, 1, 3, 3, 3)); } © Steria
JUnit eksempel #1: Yatzy histogram @Test public void full. House() { assert. Equals(0, score. For("full_house", 1, 1, 2, 2, 3)); assert. Equals(0, score. For("full_house", 1, 1, 1, 2, 3)); assert. Equals(25, score. For("full_house", 1, 1, 1, 2, 2)); assert. Equals(25, score. For("full_house", 1, 1, 2, 2, 2)); assert. Equals(25, score. For("full_house", 5, 5, 6, 6, 6)); } © Steria
JUnit eksempel #1: Yatzy histogram @Test public void histogram. Should. Return. Frequency. Of. Each. Die() { int[] roll = { 1, 1, 2, 3, 4 }; int[] histogram = new Score. Card(). histogram(roll); assert. Equals(7, histogram. length); assert. Equals(-1, histogram[0]); assert. Equals(2, histogram[1]); assert. Equals(1, histogram[2]); assert. Equals(1, histogram[3]); assert. Equals(1, histogram[4]); assert. Equals(0, histogram[5]); assert. Equals(0, histogram[6]); © Steria
JUnit eksempel #1: Yatzy histogram score. Calculators. put("four_of_a_kind", new Score. Calculator() { @Override public int calculate(int[] histogram) { for (int i=0; i<histogram. length; i++) { if (histogram[i] >= 4) return i*4; } return 0; } }); © Steria
Hvordan gjøre det bra på eksamen
Kjenn fasiten?
Forstå spørsmålet
Andre typer tester
Only test what you want to work Ron Jeffries p 59 | 07/12/2020 Presentation title © Steria
Find the defect where it’s cheapest Johannes p 60 | 07/12/2020 Presentation title © Steria
© Steria
Todo
JUnit eksempel #2: Session og transaksjoner public interface Repository { Serializable insert(Object entity); <T> T retrieve(Class<T> entity. Type, Serializable id); void update(Object entity); <T> Collection<T> find(Class<T> entity. Type); void delete(Object entity); void flush. Changes(); } © Steria
JUnit eksempel #2: Session og transaksjoner should. Retrieve. Separate. But. Equals. After. Session. Flush() should. Retrieve. Same. Instance. For. Same. Key() rollback. Should. Undo. Inserts. Deletes. And. Updates() uncommitted. Inserts. Should. Be. Invisible. For. Other. Threads() uncommited. Deletes. Should. Be. Invisible. For. Other. Threads() uncommited. Updates. Should. Be. Invisible. For. Other. Threads() © Steria
JUnit eksempel #2: Session og transaksjoner public void start. Session() { session. For. Thread. set( new Nested. Change. Set(permanent. Store)); } public void begin. Transaction() { this. transaction. For. Thread. set( new Nested. Change. Set(permanent. Store)); get. Session(). set. Parent( this. transaction. For. Thread. get()); } © Steria
Repository design
Vedlikeholdbarhet © Steria
- Slides: 66