我想測試,如果創建的實體已經正確地持久化到database.There是創建方法的服務集成測試:集成測試策略的創建方法
@SpringApplicationContext({"setting ...."})
public class PersonServiceIntegrationTest extends UnitilsJUnit4 {
@SpringBeanByName
private PersonService personService;
@Test
public void createPerson() {
String name = "Name";
String sname = "Surename";
DtoPerson item = personService.createPerson(name, sname, Arrays.asList(new DtoAddress("Pisek","CZE", true), new DtoAddress("Strakonice", "CZE", false));
Assert.notNull("Cannot be null", item);
/*
* This assertion fails because of transaction (I suppose) - item is not in
* database right now.
* Why? Returned dto 'item; is not null?
*/
//domain with all fetched related entities, eg. address
Person p = personService.getPerson(item.getIdPerson());
List<Address> addresses = p.getAddresses();
Assert.notNull("Cannot be null", p);
Assert.notNull("Cannot be null", addresses);//return collection of Address
Assert.notFalse("Cannot be emtpty", addresses.isEmpty());
ReflectionAssert.assertPropertyLeniens("City", Arrays.asList("Pisek", "Strakonice"), addresses);
}
}
是否有必要測試創建實體如果我使用休眠?有人可以寫你嘗試測試低級別的休眠,但休眠有自己的測試。上面有一個微不足道的代碼,但我可以想象一些特定的代碼在同一時間持續更多的實體(例如,一對多加幾個一對一關係)。我想測試關係是否正確持續。
有沒有一種模式來測試這種方式?我有一個問題,那個記錄不在數據庫。我不想使用返回的dto(它只提供了根集合的實體 - 人,但它沒有說人的基本數據(一對多),人的地址(一對多)等)。我想獲得持久記錄。
存在問題。 Dao層不允許服務層和服務層不允許我獲取實體管理器,因此無法調用實體管理器API。我決定爲人員重做服務層級並分工負責。 – tomascejka 2013-02-27 05:43:42
該測試涉及到dao層和域對象。沒有涉及服務層。 - 無論如何,我認爲嚴格的分層與主代碼相關,但與測試代碼無關。 – Ralph 2013-02-27 06:19:14
「_this測試與dao層和域對象有關_」 - 好的,現在我明白了你的答案....「_Anyway我認爲嚴格的分層與主代碼相關,但與測試代碼無關。」 - 它是有趣。如果我已經正確理解了第二句,那麼測試將擁有自己的hibernate會話(並且不要使用服務層來加載數據)來測試例如。這個測試用例。對? – tomascejka 2013-02-27 12:10:20