使用测试配置文件运行单元测试时未创建的表

时间:2018-03-07 21:51:54

标签: spring postgresql-9.4

由于未知原因,我的单元测试不再起作用了。突然之间,我的测试数据库的表格不再被创建了。

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:375) [hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:359) [hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applyConstraintDropping(SchemaDropperImpl.java:331) [hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:230) [hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:154) [hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:126) [hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    ...
Caused by: org.postgresql.util.PSQLException: ERROR: relation "app_user__user_group" does not exist
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2284) ~[postgresql-9.4.1208-jdbc42-atlassian-hosted.jar:9.4.1208]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2003) ~[postgresql-9.4.1208-jdbc42-atlassian-hosted.jar:9.4.1208]
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:200) ~[postgresql-9.4.1208-jdbc42-atlassian-hosted.jar:9.4.1208]
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424) ~[postgresql-9.4.1208-jdbc42-atlassian-hosted.jar:9.4.1208]
    ..

我们只是说我没有改变任何东西,至少不是我应该改变那种行为的最佳知识,但这只是我和我与Spring的关系。

所以让我们转储必要的信息:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, properties = "spring.profiles.active=test")
public class AppUserRepositoryTest {

    private final static Logger LOGGER = LogManager.getLogger(AppUserRepositoryTest.class);

    @Autowired
    private TestRestTemplate template;

    private static String USERS_ENDPOINT = "http://localhost:8080/users/";
    private static String GROUPS_ENDPOINT = "http://localhost:8080/groups/";
    private static String USER_LOGIN = "http://localhost:8080/oauth/token/";

    @Autowired
    private WebApplicationContext wac;

    @Autowired
    private FilterChainProxy springSecurityFilterChain;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
                .addFilter(springSecurityFilterChain).build();    
        LOGGER.info("Setup done.");
    }    

    @Test
    @DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
    public void whenCreateAppUser() {

        Client client = new Client();

        AppUser appUser = client.registerUser("test@example.com", "password");

        ResponseEntity<AppUser> appUserResponse = template.getForEntity(USERS_ENDPOINT + "1/", AppUser.class);

        assertEquals("Username is incorrect. AppUser not created?",
                appUser.getUsername(), appUserResponse.getBody().getUsername());
    }

}

这是application-test.properties - 或者至少是相关部分:

# Disable feature detection by this undocumented parameter. Check the org.hibernate.engine.jdbc.internal.JdbcServiceImpl.configure method for more details.
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
# Because detection is disabled you have to set correct dialect by hand.
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect

spring.datasource.url=jdbc:postgresql://localhost/mahlzeit-test
spring.datasource.username=postgres
spring.datasource.password=root
spring.datasource.driver-class-name=org.postgresql.Driver

spring.jpa.hibernate.ddl-auto=create-drop

知道问题可能是什么?如果您需要更多信息和/或代码,请与我们联系。

0 个答案:

没有答案