dimanche 2 août 2020

Test in spring boot without effect on database

i'm trying to test my service layer (actually insert) without any effect on my database. here in my code:

@SpringBootTest
@RunWith(SpringRunner.class)
@Transactional
public class StateServiceTest {

@Autowired
private StateService stateService;

@Test
public void createCity() {
    State state = State.builder()
            .id(null)
            .name("testState")
            .code("21")
            .build();

    Object mCreatedCity = stateService.create(state);
    assertThat(mCreatedCity).isNotNull().isNotInstanceOf(Exception.class);
    }

}

use @Transactional probably solved my problem and test data doesn't insert to database, but still has effect on my database.

it means that if the inserted data exist in database or is duplicate (for unique column) , I got error duplicate entry key.

Is there any way to test my service and dao layer without effect on my database? or I should use test database like h2 (in memory) ?

Aucun commentaire:

Enregistrer un commentaire