這是我嘗試過的:
@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles("h2")
@Rollback(false)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class ServiceTest {
private EntityManager entityManager;
public ServiceTest(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Test
public void findLocation() {
Location location = entityManager.find(Location.class, 2);
assertEquals(location.getName(), "Avenue");
}
@Test
public void updateLocation() {
Location location = entityManager.find(Location.class, 2);
location.setNo_people(10);
entityManager.persist(location);
entityManager.flush();
}
}
我得到的錯誤是' Runner org.junit.internal.runners.ErrorReportingRunner (在類 com.unibuc.AWBD_Project_v1.services.ServiceTest 上使用)不支持過濾,因此將完全運行。測驗類應該只有一個公共零引數建構式'
這是位置服務:
@Service
public class LocationService implements BaseService<Location> {
private final LocationRepository locationRepository;
@Autowired
public LocationService(com.unibuc.AWBD_Project_v1.repositories.LocationRepository locationRepository) {
this.locationRepository = locationRepository;
}
@Override
public Location insert(Location object) {
return locationRepository.save(object);
}
@Override
public Location update(Long id, Location updatedObject) {
var foundId = locationRepository.findById(id);
return foundId.map(locationRepository::save).orElse(null);
}
@Override
public List<Location> getAll() {
return locationRepository.findAll();
}
@Override
public Optional<Location> getById(Long id) {
return locationRepository.findById(id);
}
@Override
public void deleteById(Long id)
{
try {
locationRepository.deleteById(id);
} catch (LocationException e) {
throw new LocationException("Location not found");
}
}
@Override
public Page<Location> findAll(int page, int size, String sortBy, String sortType){
Sort sort = sortType.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortBy).ascending() :
Sort.by(sortBy).descending();
Pageable pageable = PageRequest.of(page - 1, size, sort);
return locationRepository.findAll(pageable);
}
}
uj5u.com熱心網友回復:
您好,您的測驗代碼中有 3 個問題。
1 您應該從測驗建構式中洗掉 EntityManager entityManager 以獲得可運行的測驗類。
2 如果你想在你的測驗類中使用 entityManager 你應該@Autowired
public class ServiceTest {
@Autowired
private EntityManager entityManager;
3 看起來您正在測驗 entityManager 而不是您的 LocationService 在單元測驗中,您應該使用 Mockito 模擬 entityManager 等依賴項
您似乎想創建一個集成測驗。
一項服務集成測驗的 3 個步驟(以 findLocation() 為例)
準備測驗資料庫中的資料 創建一個新的位置物件并使用 entityManager 或 testEntityManager 將其保存到資料庫中。
在 id 上執行 findLocation 方法不要忘記自動裝配您的服務類。
驗證檢索到的資料是否符合預期 將檢索到的 Location 物件與您保存的物件進行比較。
這是代碼
@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles("h2")
@Rollback(false)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class ServiceTest {
@Autowired
private EntityManager entityManager;
@Autowired
private LocationService locationService;
public ServiceTest() {
}
@Test
public void findLocation() {
//given
Location location = new Location(....);
entityManager.save(location);
//when
Location foundLocation=locationService.getById(location.getId());
//then
assertTrue(foundLocation.isPresent());
assertEquals(foundLocation.get().getName(), "Avenue");
}
如果您有任何問題,我可以為您提供幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/484657.html
上一篇:如何從方法檔案字串/注釋中的特殊語法自動生成單元測驗例程?
下一篇:Junit5使用mockConstruction().withSetting().useConstructor()而不是PowerMock.whenNew().withArguments()
