When junit test operation of the database, each will add more than one record?

When using juint today, we tested several methods were all queries, a single query, save one update one, delete one.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
    @Autowired
    private IAccountService accountService;
    @Test
    public void tetsFindAll() {
        List<Account> accounts = accountService.findAllAccount();
        for (Account account : accounts) {
            System.out.println(account);
        }
    }

    @Test
    public void tetsFindOneById() {
        List<Account> accounts = accountService.findAllAccount();
        Account account = accountService.findOneById(1);
        System.out.println(account);
    }

    @Test
    public void tetsSave() {
        List<Account> accounts = accountService.findAllAccount();
        Account account = new Account();
        account.setName("双双");
        account.setMoney(13000F);
        accountService.saveAccount(account);
    }

    @Test
    public void tetsUpdate() {
        List<Account> accounts = accountService.findAllAccount();
        Account account = accountService.findOneById(4);
        account.setMoney(23456F);
        accountService.updateAccount(account);
    }

    @Test
    public void testDelete() {
        List<Account> accounts = accountService.findAllAccount();
        accountService.deleteAccount(7);
    }
}

 

But found a problem: No matter which method each test, the database will add a new record, this record is a preservation method in content.

 

 

The test class inside preservation methods to remove, and then run when other methods, found no new increase recorded.

For the following reasons:

We know that the application has an entry is the main method, but junit unit test, there is no main method can be performed, it is because junit own integrates a main method that will determine what the current test class methods @ Test annotation, junit have let annotated method Test execution, so preservation methods will be carried out.

But the question is, every time I'm on the right method name, a method that runs only selected. . .

So he had to save the previous method of adding a @Ignore notes, barely solve the problem, if there is a better way to come back later update.

Guess you like

Origin www.cnblogs.com/iceywu/p/12355144.html