Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. // Twój kod
  2.  
  3. interface Caller {
  4. public void createNewProduct(Product p);
  5. }
  6.  
  7. class ApiCaller implements Caller {
  8. public void createNewProduct(Product p) {
  9. // wysyłamy produkt do API
  10. }
  11. }
  12.  
  13. class ProductGenerator {
  14. private Caller caller;
  15.  
  16. public ProductGenerator(Caller c) {
  17. this.caller = c;
  18. }
  19.  
  20. /**
  21. Tworzymy nowy produkt o danej nazwie i wartości oraz wysyłamy go do API
  22. */
  23. public void generateProduct(string name, int amount) {
  24. Product p = new Product();
  25. p.setName(name);
  26. p.setAmount(amount);
  27. this.caller.createNewProduct(p);
  28. }
  29. }
  30.  
  31. // TESTY KLASY ProductGenerator
  32. public void test() {
  33. string name = "Test product";
  34. int amount = 15;
  35. Caller mock = createMock(Caller.class)
  36. .onMethod(createNewProduct)
  37. .do(Product p -> {
  38. assertEquals(p.getName(), name);
  39. assertEquals(p.getAmount(), amount);
  40. }
  41. );
  42. ProductGenerator generator = new ProductGenerator(mock);
  43. generator.generateProduct(name, amount);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement