Тестирование класса Controller, когда я пытаюсь удалить все записи из моей базы данных через CrudRepository Spring, но, похоже, ничего не происходит.Кажется, по умолчанию он не сбрасывается.
Я никогда не пробовал этот репозиторий в реальном вызове контроллера в браузере только с тестами Junit, но я думаю, что он будет работать нормально!:)
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@ActiveProfiles("test")
@Transactional
public class CostumerControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private CostumerService costumerService;
private Costumer costumer;
@Before
public void before() {
this.costumer = this.exampleBuilder();
costumerService.saveAll(this.costumer);
}
@After
public void after() {
costumerService.deleteAll();
}
@Test
public void Should_ReturnStandardError_When_NotFoundById() throws Exception {
//implementation
}
private Costumer exampleBuilder() {
Costumer costumer = new Costumer("Test", "Test", "Test", CostumerType.LEGAL_PERSON);
State state = new State("Example State");
City city = new City("Example Sity", state);
Address address = new Address("Example Address",
"Example Address", "Example Address",
"Example Address", city, costumer);
costumer.getAddresses().add(address);
return costumer;
}
}
@Service
@Transactional
public class CostumerService {
@Autowired
private CostumerRepository repository;
public void deleteAll() {
repository.deleteAll();
}
//another methods
}
Расширение репозитория CrudRepository
@Repository
public interface CostumerRepository extends CrudRepository<Costumer, Integer> {
}
После включения показа sql hibernate.show_sql=true
на основе комментария @TheCoder, результатis:
deleteAll()
on @After
:
@After
public void after() {
costumerService.deleteAll();
}
Выходной sql:
2019-02-27 06:07:06 - HHH000397: Using ASTQueryTranslatorFactory
Hibernate:
select
costumer0_.id as id1_3_,
costumer0_.cpf_cnpj as cpf_cnpj2_3_,
costumer0_.email as email3_3_,
costumer0_.name as name4_3_,
costumer0_.type as type5_3_
from
costumers costumer0_
deteleAll()
on @AfterTransaction
Выходной sqlвключает запрос на удаление.
@AfterTransaction
public void afterTransatcion(){
// List<Costumer> costumers = costumerService.findAll();
costumerService.deleteAll();
}
Hibernate:
select
costumer0_.id as id1_3_,
costumer0_.cpf_cnpj as cpf_cnpj2_3_,
costumer0_.email as email3_3_,
costumer0_.name as name4_3_,
costumer0_.type as type5_3_
from
costumers costumer0_
Hibernate:
delete
from
phones
where
costumer_id=?
Hibernate:
delete
from
costumers
where
id=?