private Greeting getEntityStubData() { Greeting entity = new Greeting(); entity.setId(1L); entity.setText("hello"); return entity; }
@Test public void testUpdateNotFound() { Exception exception = null; Greeting entity = new Greeting(); entity.setId(Long.MAX_VALUE); entity.setText("test"); try { service.update(entity); } catch (NoResultException e) { exception = e; } Assert.assertNotNull("failure - expected exception", exception); Assert.assertTrue("failure - expected NoResultException", exception instanceof NoResultException); }
@Test public void testCreateWithId() { Exception exception = null; Greeting entity = new Greeting(); entity.setId(Long.MAX_VALUE); entity.setText("test"); try { service.create(entity); } catch (EntityExistsException e) { exception = e; } Assert.assertNotNull("failure - expected exception", exception); Assert.assertTrue("failure - expected EntityExistsException", exception instanceof EntityExistsException); }
@Test public void testCreate() { Greeting entity = new Greeting(); entity.setText("test"); Greeting createdEntity = service.create(entity); Assert.assertNotNull("failure - expected not null", createdEntity); Assert.assertNotNull("failure - expected id attribute not null", createdEntity.getId()); Assert.assertEquals("failure - expected text attribute match", "test", createdEntity.getText()); Collection<Greeting> list = service.findAll(); Assert.assertEquals("failure - expected size", 3, list.size()); }
@Test public void testCreateGreeting() throws Exception { String uri = "/api/greetings"; Greeting greeting = new Greeting(); greeting.setText("test"); String inputJson = super.mapToJson(greeting); MvcResult result = mvc .perform(MockMvcRequestBuilders.post(uri) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON).content(inputJson)) .andReturn(); String content = result.getResponse().getContentAsString(); int status = result.getResponse().getStatus(); Assert.assertEquals("failure - expected HTTP status 201", 201, status); Assert.assertTrue( "failure - expected HTTP response body to have a value", content.trim().length() > 0); Greeting createdGreeting = super.mapFromJson(content, Greeting.class); Assert.assertNotNull("failure - expected greeting not null", createdGreeting); Assert.assertNotNull("failure - expected greeting.id not null", createdGreeting.getId()); Assert.assertEquals("failure - expected greeting.text match", "test", createdGreeting.getText()); }