Advertisement
moreiramota

Untitled

Jan 8th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package lapr.project.model;
  7.  
  8. import java.sql.Timestamp;
  9. import lapr.project.data.BicycleDB;
  10. import static org.junit.jupiter.api.Assertions.assertEquals;
  11. import static org.junit.jupiter.api.Assertions.assertThrows;
  12. import static org.junit.jupiter.api.Assertions.assertTrue;
  13. import org.junit.jupiter.api.Test;
  14. import org.mockito.InjectMocks;
  15. import org.mockito.Mock;
  16.  
  17. /**
  18. *
  19. * @author joaoflores
  20. */
  21. public class BatteryTest {
  22.  
  23. @Mock
  24. private BicycleDB bdb;
  25.  
  26. @InjectMocks
  27. private Battery bat;
  28. //
  29. //
  30. //
  31. //
  32. // @Test
  33. // public void testDB() {
  34. //
  35. //
  36. // bat = new Battery(120.5, 30.4, 40.6, 120.5, new Timestamp(10));
  37. // //getUser
  38. // MockitoAnnotations.initMocks(this);
  39. // Battery.setDB(bdb);
  40. //
  41. // when(bdb.getBattery(0)).thenReturn(bat);
  42. // assertTrue(bat.equals(Battery.getBattery(0)));
  43. //
  44. // //save
  45. // //enrure getUser is executed
  46. // Mockito.doThrow(new ArrayIndexOutOfBoundsException()).when(bdb).getBattery(0);
  47. // ArrayIndexOutOfBoundsException exceptionGet = assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
  48. // bat.saveBattery();
  49. // });
  50. // assertEquals(exceptionGet.getMessage(), null);
  51. //
  52. // Mockito.doReturn(null).when(bdb).getBattery(0);
  53. // assertFalse(bat.saveBattery());
  54. //
  55. // //ensure addUser is called
  56. // Mockito.doThrow(new IllegalArgumentException()).when(bdb).getBattery(0);
  57. // Mockito.doThrow(new IllegalArgumentException()).when(bdb).addBattery(bat);
  58. // exceptionGet = assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
  59. // bat.saveBattery();
  60. // });
  61. // assertEquals(exceptionGet.getMessage(), null);
  62. //
  63. // Mockito.doNothing().when(bdb).addBattery(bat);
  64. // assertTrue(bat.saveBattery());
  65. //
  66. // //remove User
  67. // when(bdb.removeBicycle(0)).thenReturn(Boolean.TRUE);
  68. // assertTrue(Battery.removeBattery(0));
  69. //
  70. // }
  71.  
  72. @Test
  73. public void testException() {
  74. User.setDB(null);
  75. NullPointerException exceptionGet = assertThrows(NullPointerException.class, () -> {
  76. Battery.getBattery(1);
  77. });
  78. assertTrue(exceptionGet.getMessage().equals("The dataBase connection was not yet set, either set or get a new one"));
  79. exceptionGet = assertThrows(NullPointerException.class, () -> {
  80. Battery.removeBattery(1);
  81. });
  82. assertTrue(exceptionGet.getMessage().equals("The dataBase connection was not yet set, either set or get a new one"));
  83. exceptionGet = assertThrows(NullPointerException.class, () -> {
  84. new Battery().saveBattery();
  85. });
  86. assertTrue(exceptionGet.getMessage().equals("The dataBase connection was not yet set, either set or get a new one"));
  87.  
  88. }
  89.  
  90. /**
  91. * Test of setBatteryId method, of class Battery.
  92. */
  93. @Test
  94. public void testSetBatteryId() {
  95. System.out.println("setBatteryId");
  96. int batteryId = 3;
  97. Battery instance = new Battery(2, 120.5, 30.4, new Timestamp(10));
  98. instance.setBatteryId(batteryId);
  99. assertEquals(batteryId, instance.getBatteryId());
  100.  
  101. }
  102.  
  103. /**
  104. * Test of setBatteryCapacity method, of class Battery.
  105. */
  106. @Test
  107. public void testSetBatteryCapacity() {
  108. System.out.println("setBattery_capacity");
  109. double batteryCapacity = 500.0;
  110. Battery instance = new Battery(2, 120.5, 30.4, new Timestamp(10));
  111. Battery instance1 = new Battery(120.5, 30.4, new Timestamp(10));
  112. instance.setBatteryCapacity(batteryCapacity);
  113. assertEquals(batteryCapacity, instance.getBatteryCapacity());
  114. instance1.setBatteryCapacity(batteryCapacity);
  115. assertEquals(batteryCapacity, instance1.getBatteryCapacity());
  116.  
  117. }
  118.  
  119. /**
  120. * Test of setBatteryRemaining method, of class Battery.
  121. */
  122. @Test
  123. public void testSetBattery_remaining() {
  124. System.out.println("setBattery_remaining");
  125. double batteryRemaining = 3.0;
  126. Battery instance = new Battery(2, 120.5, 30.4, new Timestamp(10));
  127. Battery instance1 = new Battery(120.5, 30.4, new Timestamp(10));
  128. instance.setBatteryRemaining(batteryRemaining);
  129. assertEquals(batteryRemaining, instance.getBatteryRemaining());
  130. instance1.setBatteryRemaining(batteryRemaining);
  131. assertEquals(batteryRemaining, instance1.getBatteryRemaining());
  132.  
  133. }
  134.  
  135. /**
  136. * Test of setLastUpdate method, of class Battery.
  137. */
  138. @Test
  139. public void testSetLast_update() {
  140. System.out.println("setLast_update");
  141. Timestamp lastUpdate = new Timestamp(20);
  142. Battery instance = new Battery(2, 120.5, 30.4, new Timestamp(10));
  143. Battery instance1 = new Battery(120.5, 30.4, new Timestamp(10));
  144. instance.setLastUpdate(lastUpdate);
  145. assertEquals(lastUpdate, instance.getLastUpdate());
  146. instance1.setLastUpdate(lastUpdate);
  147. assertEquals(lastUpdate, instance1.getLastUpdate());
  148. }
  149.  
  150. public int hashCodeCalculator(Battery b) {
  151. int hash = 7;
  152. hash = 67 * hash + b.getBatteryId();
  153. return hash;
  154. }
  155.  
  156. @Test
  157. public void testHashCode() {
  158. System.out.println("hashCode");
  159. Battery instance = new Battery(2, 120.5, 30.4, new Timestamp(10));
  160. int expResult = hashCodeCalculator(instance);
  161. int result = instance.hashCode();
  162. assertEquals(expResult, result);
  163. }
  164.  
  165. @Test
  166. public void testEquals() {
  167. Battery instance = new Battery(2, 120.5, 30.4, new Timestamp(10));
  168. Battery instance1 = new Battery(2, 120.5, 30.4, new Timestamp(10));
  169. boolean exp = true;
  170. boolean res = instance.equals(instance1);
  171. assertEquals(exp, res);
  172. }
  173.  
  174. @Test
  175. public void testEqualsSameObject() {
  176. Battery instance = new Battery(2, 120.5, 30.4, new Timestamp(10));
  177. Object instance1 = instance;
  178. boolean exp = true;
  179. boolean res = instance.equals(instance1);
  180. assertEquals(exp, res);
  181. }
  182.  
  183. @Test
  184. public void testEqualsDifferentObject() {
  185. Battery instance = new Battery(2, 120.5, 30.4, new Timestamp(10));
  186. Object instance1 = new Battery(1, 12.5, 3.4, new Timestamp(10));
  187. boolean exp = false;
  188. boolean res = instance.equals(instance1);
  189. assertEquals(exp, res);
  190. }
  191.  
  192. @Test
  193. public void testEqualsDifferent() {
  194. Battery instance = new Battery(2, 120.5, 30.4, new Timestamp(10));
  195. Battery instance1 = new Battery(1, 120.5, 30.4, new Timestamp(10));
  196. boolean exp = false;
  197. boolean res = instance.equals(instance1);
  198. assertEquals(exp, res);
  199. }
  200.  
  201. @Test
  202. public void testEqualsDifferentNull() {
  203. Battery instance = new Battery(2, 120.5, 30.4, new Timestamp(10));
  204. Battery instance1 = null;
  205. boolean exp = false;
  206. boolean res = instance.equals(instance1);
  207. assertEquals(exp, res);
  208. }
  209.  
  210. @Test
  211. public void testEqualsDifferentDifferentClassObjects() {
  212. Battery instance = new Battery(2, 120.5, 30.4, new Timestamp(10));
  213. Object instance1 = new Bicycle("", 1, 2, "y", 15.6, null);
  214. boolean exp = false;
  215. boolean res = instance.equals(instance1);
  216. assertEquals(exp, res);
  217. }
  218.  
  219. @Test
  220. public void testException2() {
  221. Battery.setBdb(null);
  222. NullPointerException exceptionGet = assertThrows(NullPointerException.class, () -> {
  223. Battery.getBattery(0);
  224. });
  225. assertTrue(exceptionGet.getMessage().equals("The dataBase connection was not yet set, either set or get a new one"));
  226. exceptionGet = assertThrows(NullPointerException.class, () -> {
  227. Battery.removeBattery(0);
  228. });
  229. assertTrue(exceptionGet.getMessage().equals("The dataBase connection was not yet set, either set or get a new one"));
  230. exceptionGet = assertThrows(NullPointerException.class, () -> {
  231. new Battery().saveBattery();
  232. });
  233. assertTrue(exceptionGet.getMessage().equals("The dataBase connection was not yet set, either set or get a new one"));
  234. assertTrue(Battery.getBdb() == null);
  235.  
  236. Battery.getNewBdb();
  237. assertTrue(Battery.getBdb() != null);
  238.  
  239. }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement