相关疑难解决方法(0)

Junit测试后的数据库清理

我必须使用Junit测试一些Thrift服务.当我将测试作为Thrift客户端运行时,服务会修改服务器数据库.我无法找到一个好的解决方案,可以在每次测试运行后清理数据库.清理很重要,特别是因为ID必须是唯一的,目前从XML文件中读取.现在,我必须在运行测试后手动更改ID,以便下一组测试可以运行而不会在数据库中抛出主键冲突.如果我可以在每次测试运行后清理数据库,那么问题就完全解决了,否则我将不得不考虑其他解决方案,比如生成随机ID并在需要ID的地方使用它们.

编辑:我想强调一下,我正在测试一个写入数据库的服务,我没有直接访问数据库.但是,因为服务是我们的,我可以修改服务,以便在需要时提供任何清理方法.

java database junit unit-testing

12
推荐指数
3
解决办法
3万
查看次数

Spring H2 测试数据库在每次测试前不会重置

编辑:正如 C. Weber 在评论中建议的那样,解决方案是添加@Transactional到测试类中。

我有一些使用 H2 内存数据库的测试。我需要在每次测试之前重置数据库。尽管每次执行测试时都会运行我的 SQL 脚本,但未正确重置数据库,导致删除测试后缺少所需的条目。

测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureTestDatabase(replace=Replace.ANY, connection=EmbeddedDatabaseConnection.H2)
public class RepositoryTests {

    @Autowired
    private Repository repository;

    @Autowired
    private DataSource dataSource;

    @Before
    public void populateDb() {
        Resource initSchema = new ClassPathResource("database/schema.sql");
        Resource initData = new ClassPathResource("database/data.sql");
        DatabasePopulator dbPopulator = new ResourceDatabasePopulator(initSchema, initData);
        DatabasePopulatorUtils.execute(dbPopulator, dataSource);
    }

    @Test
    public void testMethod1() {
        // ...
        repository.delete("testdata");
    }

    @Test
    public void testMethod2() {
        // ...
        Object test = repository.get("testdata");
        // is null but should be an …
Run Code Online (Sandbox Code Playgroud)

junit h2 spring-boot

5
推荐指数
1
解决办法
7999
查看次数

测试时出错:发现测试类有多个 @BootstrapWith 声明

我想第一次测试我的 CRUD REST 控制器。我看了一些视频并提出了这个想法,但我遇到了错误。我将 JPA 与 mySql 一起使用。ITodoService 是带有 CRUD 方法的简单接口。当我通过 Postman 测试它时,我的其余控制器正在工作,所以那里的代码没问题。如果您能给我一些反馈,可能出了什么问题,我在哪里可以检查有关测试 REST 应用程序的良好信息,因为我花了大约 3 个小时但没有成功:)

    @SpringBootTest
    @RunWith(SpringRunner.class)
    @WebMvcTest
    public class TodoFinalApplicationTests {

        @Autowired
        private MockMvc mockMvc;


        @MockBean
        private ITodosService iTodosService;


        @Test
        public void getAllTodosTest() throws Exception {

            Mockito.when(iTodosService.findAll()).thenReturn(
                        Collections.emptyList()
                        );

                        MvcResult mvcResult = mockMvc.perform(
                        MockMvcRequestBuilders.get("/todos")
                        .accept(MediaType.APPLICATION_JSON)
                        ).andReturn();

                        System.out.println(mvcResult.getResponse());

                        Mockito.verify(iTodosService.findAll());

        }
    }


    Error message:
    java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.damian.todo_Final.TodoFinalApplicationTests]: [@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper)]

EDIT:
This is code for whole CRUD REST Test …
Run Code Online (Sandbox Code Playgroud)

rest junit4 mockito spring-boot

4
推荐指数
1
解决办法
1万
查看次数

如何使用 Junit 5 在 Spring boot 2.1.0.M4 中使用 @DataJpaTest 测试 Spring CrudRepository

我无法使用带有 Junit5 的 spring boot 2.1.0.M4 和@DataJpaTest. 我正在使用以下 spring crud 存储库界面。

@Repository
public interface DestinationRepository extends CrudRepository<Destination, String> {

    Optional<Destination> findCityCode(String code, String cityIsolci);

}
Run Code Online (Sandbox Code Playgroud)

这是我的单元测试课

package com.test.repository;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.test.Destination;

import java.util.Optional;

import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@DataJpaTest
// @ExtendWith(SpringExtension.class)
public class DestinationRepositoryTest {

    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private DestinationRepository destinationRepository;

    @Test
    public void whenfindByCodeAndCityIsolciThenReturnDestination() throws Exception …
Run Code Online (Sandbox Code Playgroud)

spring spring-data-jpa spring-boot junit5

1
推荐指数
1
解决办法
4067
查看次数