运行 在 Spring 引导中测试

Running tests in Spring Boot

我有这个测试class

我使用 Spring Initializr 生成了一个 Spring Boot web 应用程序,使用嵌入式 Tomcat + Thymeleaf 模板引擎,并打包为可执行 JAR 文件。

使用的技术:

Spring 引导 1.4.2.RELEASE、Spring 4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcat 嵌入 8.5.6 , 行家 3, Java 8

我的class是:

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

        @Test
        public void contextLoads() {
        }
    }

    @Configuration
    public class PersistenceConfig {

        @Bean
        public  JdbcTemplate jdbcTemplate() {
            return new JdbcTemplate(dataSource());
        }


        @Bean 
        public IOTEcoSystemManager iOTEcoSystemManager() {
            return new IOTEcoSystemManagerImpl();
        }

        @Bean 
        public DeviceEventRepository deviceEventRepository() {
            return new JdbcDeviceEventRepository();
        }

        @Bean 
        public DeviceRepository deviceRepository() {
            return new JdbcDeviceRepository();
        }

        /**
         * Creates an in-memory "books" database populated 
         * with test data for fast testing
         */
        @Bean
        public DataSource dataSource(){
            return
                (new EmbeddedDatabaseBuilder())
                .addScript("classpath:db/H2.schema.sql")
                .addScript("classpath:db/H2.data.sql")
                .build();
        }

    }



    @ContextConfiguration(classes={PersistenceConfig.class})
    @RunWith(SpringRunner.class)
    public class BookManagerTests {

        /**
         * The object being tested.
         */
        @Autowired
        BookManager bookManager;


        @Test
        public void testfindDeviceByKey() {
            Device device = bookManager.findDeviceByKey("C380F");
            assertNotNull(device);
            assertEquals("C380F", device.getDeviceKey());
            assertEquals(1, device.getId().longValue());
        }

        @Test
        public void testfindDeviceByKeyFail() {
            Device device = null;
            try {
                device = bookManager.findDeviceByKey("C380FX");
            } catch (EmptyResultDataAccessException erdae) {
                assertNotNull(erdae);
            }
            assertNull(device);

        }
    }

@Service("bookManager")
public class BookManagerImpl implements BookManager {
...
}

如果我运行所有的包测试我得到这个错误:

unique constraint or index violation; SYS_PK_10092 table: T_COMPANY

因为脚本 运行s 两次。如果我删除

classes={PersistenceConfig.class}

来自 BookManagerTests 我遇到了这个依赖问题

expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

如果我运行 都单独测试一切正常

PersistenceConfig 及其内容似乎创建了两次:第一次作为普通bean,第二次作为config(没错)。我建议创建一个包含所有注释的主测试 class,然后从中扩展你的测试(没有任何注释)。

@ContextConfiguration(classes={PersistenceConfig.class})
@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class MainTest {
}

public BookManagerTest extends MainTest {
...
}

对我来说,约束验证似乎是因为 PersistentConfig 被加载了两次,这掩盖了你的第二个问题。 BookManager 在哪里定义的?如果用@repository 注释,你确定你为它正确地扫描了类路径吗?