mardi 21 avril 2020

Failed to load ApplicationContext in Spring Controller E2E tests

I am running end2end tests for controllers in a spring boot maven project, individually these types of tests work for me, but if I run them in conjunction with the rest of the test the following error appears:

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/guillermo/git/DP2-1920-GI-05/target/classes/db/hsqldb/data.sql]: INSERT INTO users(username,password,enabled) VALUES ('admin1','4dm1n',TRUE); nested exception is org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Violación de indice de Unicidad ó Clave primaria: "PUBLIC.PRIMARY_KEY_4D ON PUBLIC.USERS(USERNAME) VALUES 1"
Unique index or primary key violation: "PUBLIC.PRIMARY_KEY_4D ON PUBLIC.USERS(USERNAME) VALUES 1"; SQL statement:
INSERT INTO users(username,password,enabled) VALUES ('admin1','4dm1n',TRUE) [23505-200]

    ... 65 more
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/guillermo/git/DP2-1920-GI-05/target/classes/db/hsqldb/data.sql]: INSERT INTO users(username,password,enabled) VALUES ('admin1','4dm1n',TRUE); nested exception is org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Violación de indice de Unicidad ó Clave primaria: "PUBLIC.PRIMARY_KEY_4D ON PUBLIC.USERS(USERNAME) VALUES 1"
Unique index or primary key violation: "PUBLIC.PRIMARY_KEY_4D ON PUBLIC.USERS(USERNAME) VALUES 1"; SQL statement:
INSERT INTO users(username,password,enabled) VALUES ('admin1','4dm1n',TRUE) [23505-200]

    ... 79 more
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Violación de indice de Unicidad ó Clave primaria: "PUBLIC.PRIMARY_KEY_4D ON PUBLIC.USERS(USERNAME) VALUES 1"
Unique index or primary key violation: "PUBLIC.PRIMARY_KEY_4D ON PUBLIC.USERS(USERNAME) VALUES 1"; SQL statement:
INSERT INTO users(username,password,enabled) VALUES ('admin1','4dm1n',TRUE) [23505-200]

    ... 95 more

This is an example of the end2end tests that I am running:

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
public class CategoryControllerE2ETests {


    @Autowired
    private MockMvc mockMvc;    

    // List Fields Postive Case
    @WithMockUser(username="admin1", authorities= {"admin"})
    @Test
    void testListAll() throws Exception {
        mockMvc.perform(get("/categories/all")).andExpect(status().isOk())
                .andExpect(model().attributeExists("categories")).andExpect(view().name("categories/list"));
    }
}

It appears that is trying to create admin1 with an id that is already in use (of the users type admin there is only admin1). I have tried another user that does not exist in the data.sql, but the error persists, the application uses a hsqldb database.

Data.sql:

INSERT INTO users(username,password,enabled) VALUES ('admin1','4dm1n',TRUE);
INSERT INTO authorities VALUES ('admin1','admin');

Aucun commentaire:

Enregistrer un commentaire