@Test
  public void testUpdateRetryWithError() {
    Repository<Entity> mockRepo = Mockito.spy(repository);
    Map<String, Object> studentBody = buildTestStudentEntity();
    Map<String, Object> studentMetaData = new HashMap<String, Object>();
    Entity entity = new MongoEntity("student", null, studentBody, studentMetaData);
    int noOfRetries = 3;

    Mockito.doThrow(new InvalidDataAccessApiUsageException("Test Exception"))
        .when(mockRepo)
        .update("student", entity, false);
    Mockito.doCallRealMethod().when(mockRepo).updateWithRetries("student", entity, noOfRetries);

    try {
      mockRepo.updateWithRetries("student", entity, noOfRetries);
    } catch (InvalidDataAccessApiUsageException ex) {
      assertEquals(ex.getMessage(), "Test Exception");
    }

    Mockito.verify(mockRepo, Mockito.times(noOfRetries)).update("student", entity, false);
  }
Esempio n. 2
0
  /** Simple test to verify BaseDao works. */
  public void testCRUD() {
    User user = new User();
    // set required fields
    user.setUsername("foo");
    user.setPassword("bar");
    user.setFirstName("first");
    user.setLastName("last");
    user.getAddress().setCity("Denver");
    user.getAddress().setPostalCode("80465");
    user.setEmail("*****@*****.**");

    // create
    dao.saveObject(user);
    assertNotNull(user.getId());

    // retrieve
    user = (User) dao.getObject(User.class, user.getId());
    assertNotNull(user);
    assertEquals(user.getLastName(), "last");

    // update
    user.getAddress().setCountry("USA");
    dao.saveObject(user);
    assertEquals(user.getAddress().getCountry(), "USA");

    // delete
    dao.removeObject(User.class, user.getId());
    try {
      dao.getObject(User.class, user.getId());
      fail("User 'foo' found in database");
    } catch (ObjectRetrievalFailureException e) {
      assertNotNull(e.getMessage());
    } catch (InvalidDataAccessApiUsageException e) { // Spring 2.0 throws this one
      assertNotNull(e.getMessage());
    }
  }