I started writing unit tests cases recently. For now, I created only "perfect case" where there is no error. However, the test seems to me difficult to maintain and difficult to understand.
How can I make it better for other people to understand?
All permalink attributes are set within the service itself I'm testing (PermalinkService.java).
My test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Configuration.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class PermalinkServiceTest {
@Autowired
PermalinkService permalinkService;
private static Permalink PERMALINK;
private static Validacao VALIDATION;
private static DBObject PECA;
private static BasicDBObject COMMENTS;
@BeforeClass
public static void init(){
PERMALINK = new Permalink();
VALIDATION = new Validacao().setId("55534e63ccf2f879efcbd2a3");
}
@Test
public void testPermalinkServiceA_createPermalinkWithoutValidation_permalinkWithAllFilesAndWithoutValidation() throws Exception {
PERMALINK = permalinkService.createPermalink(VALIDATION.getId(), "Permalink sem validação");
Assert.notNull(PERMALINK);
}
@Test
public void testPermalinkServiceB_createPermalinkWithValidation_permalinkWithAllFilesAndValidation() throws Exception {
PERMALINK = permalinkService.createPermalink(VALIDATION.getId(), "Permalink com validação");
PECA = (DBObject) JSON.parse(PERMALINK.getPecas().get(0).toString());
Assert.notNull(PERMALINK);
}
@Test
public void testPermalinkServiceB_getAllPermalinks_getAllPermalinks() throws Exception {
Assert.notNull(permalinkService.getPermalinks(VALIDATION.getId()));
}
@Test
public void testPermalinkServiceC_getPermalink_getPermalink() throws Exception {
Assert.notNull(permalinkService.getPermalinks(VALIDATION.getId(), PERMALINK.getId()));
}
@Test
public void testPermalinkServiceD_setStatus_setNokStatus() throws Exception {
String status = "nok";
String email = "[email protected]";
permalinkService.setStatus(VALIDATION.getId(), PERMALINK.getId(),
String.valueOf(PECA.get("id")), status, email);
}
@Test
public void testPermalinkServiceE_addComment_addComment() throws Exception {
String comment = "comentario teste";
String email = "[email protected]";
PERMALINK = permalinkService.addComment(VALIDATION.getId(), PERMALINK.getId(), String.valueOf(PECA.get("id")),
comment, email);
DBObject pecas = (DBObject) JSON.parse(PERMALINK.getPecas().toString());
DBObject peca = (DBObject) JSON.parse(pecas.get("0").toString());
DBObject comentarios = (DBObject) JSON.parse(peca.get("comentarios").toString());
COMMENTS = (BasicDBObject) JSON.parse(comentarios.get("0").toString());
Assert.notNull(PERMALINK);
}
@Test
public void testPermalinkServiceF_removeComment_removeComment() throws Exception {
permalinkService.removeComment(VALIDATION.getId(), String.valueOf(COMMENTS.get("id")), PERMALINK.getId(), "");
}
@AfterClass
public static void testPermalinkService_removeFromMongo_removeFromMongo() throws Exception {
MongoCollection mongoCollection = DBSingleton.getJongo().getCollection("validacao_permalinks");
mongoCollection.remove("{ _id : #}", PERMALINK.getId());
}
}