JFLORES99

Untitled

Dec 22nd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. @InjectMocks
  2. //@Mock
  3. private Bicycle bicycle;
  4.  
  5. @Mock
  6. private BicycleDB db;
  7.  
  8. @Test
  9. public void testDBTest() {
  10. System.out.println("DB test");
  11. MockitoAnnotations.initMocks(this);
  12.  
  13. Bicycle.setBdb(db);
  14.  
  15.  
  16. bicycle.setType(2);
  17. bicycle.setAvailable("y");
  18. bicycle.setWeigth(10.5);
  19. bicycle.setEmail("[email protected]");
  20.  
  21.  
  22. boolean exception = false;
  23. try {
  24. exception = false;
  25. bicycle.saveBicycle();
  26. fail("There should have been an exception");
  27. } catch (IndexOutOfBoundsException ex) {
  28. exception = true;
  29. }
  30. assertTrue(exception);
  31.  
  32. System.out.println("Test savve-add");
  33. Mockito.doThrow(new IllegalArgumentException()).when(db).updateBicycle(bicycle);
  34. //ensure method add is called
  35. Mockito.doThrow(new IndexOutOfBoundsException("test")).when(db).addBicycle(bicycle);
  36.  
  37. try {
  38. exception = false;
  39. bicycle.saveBicycle();
  40. fail("There should have been an exception");
  41. } catch (IndexOutOfBoundsException ex) {
  42. exception = true;
  43. }
  44. assertTrue(exception);
  45. Mockito.doNothing().when(db).addBicycle(bicycle);
  46. bicycle.saveBicycle();
  47. assertTrue(true);
  48.  
  49.  
  50.  
  51. when(db.getBicycle(1)).thenReturn(bicycle);
  52. assertTrue(Bicycle.getBicycle(1).equals(bicycle));
  53.  
  54.  
  55. System.out.println("Test remove");
  56. Mockito.doThrow(new IllegalArgumentException()).when(db).removeBicycle(1);
  57.  
  58. //ensure method call
  59.  
  60. try {
  61. Bicycle.removeBicycle(1);
  62. fail("There should have been an exception");
  63. } catch (Exception ex) {
  64. exception = true;
  65. }
  66. assertTrue(exception);
  67. Mockito.doNothing().when(db).removeBicycle(1);
  68.  
  69. Bicycle.removeBicycle(1);
  70. assertTrue(true);
  71.  
  72. System.out.println("Test save");
  73. System.out.println("Test save-update");
  74. Mockito.doThrow(new IndexOutOfBoundsException("test"))
  75. .when(db).updateBicycle(bicycle);
  76.  
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment