Spring Boot: добавление @Transcational к тесту делает тест неудачным, в противном случае он проходит - PullRequest
1 голос
/ 07 октября 2019

Итак, я пытаюсь протестировать запрос на обновление в CrudRepository

@Repository
public interface AutomationInformationRepository extends 
CrudRepository<AutomationInformation, Long> {
List<AutomationInformation> findByControlID (String controlID);
List<AutomationInformation> findByCsmpAutomationState(CSMPAutomation csmpAutomation);

AutomationInformation findFirstByControlID (String controlID);

 //    AutomationInformation findOne(Long aId);

AutomationInformation findByAID(Long aId);
@Transactional
@Modifying
@Query("update AutomationInformation a set a.csmpAutomationState = ?1 where a.aID = ?2")
int  setCsmpAutomationState(CSMPAutomation active,long aId);
 }

Я запускаю тест, в котором я получаю запись из базы данных, обновляю ее, а затем снова получаю и проверяю, что значениебыло обновлено правильно.

Когда я добавляю @Transactional к тесту, чтобы откатить его после выполнения теста, тест завершается неудачно, и обновление не происходит в извлекаемом объекте. Однако, когда я вынимаю @Transactional, он проходит, и я вижу, что обновление произошло в БД

@RunWith(SpringRunner.class)
 @SpringBootTest
public class AutomationInformationRepoTest {

@Autowired
AutomationInformationRepository repo;

@Test
public void testSetAutomationInformationActiveByAID() {
    //this control csmpatomation is set to NOAUTO
    //insert a dummy automationInformation

AutomationInformation auto = repo.findByAID((long) 588);
//CONFIRM IT IS SET TO NOAUTO
assertEquals(auto.getCsmpAutomation(),CSMPAutomation.NOAUTO);
// set it to active
int result =repo.setCsmpAutomationState(CSMPAutomation.ACTIVE, 
auto.getaID());
//confirm 1 row was changed
assertEquals(result,1);

 //retrieve it again, it should be active now
AutomationInformation auto2 = repo.findByAID((long) 588);

assertEquals(auto2.getCsmpAutomation(),CSMPAutomation.ACTIVE);
}
}
...