Alisher
Alisher

Reputation: 940

Integration tests fail with gradle test but pass with IDE

I keep getting exceptions in all integration tests, while unit tests run fine. All exceptions look like this:

UsersControllerTest > getOneUser() FAILED
    java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
        Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException at ScriptUtils.java:626
            Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException at DbException.java:459

I am using Jupiter for tests. The above exception to my opinion points to data.sql script that I have to populate some data. It seems to be ok in terms of syntax to me, also it runs fine, meaning records are generated if tested with postman:

INSERT INTO AUTHORITY (ID_AUTHORITY,AUTHORITY) VALUES (1,'USER');
INSERT INTO AUTHORITY (ID_AUTHORITY,AUTHORITY) VALUES (2,'MODERATOR');
INSERT INTO AUTHORITY (ID_AUTHORITY,AUTHORITY) VALUES (3,'ADMIN');
INSERT INTO USER (USER_ID,FIRST_NAME,LAST_NAME,ID,PASSWORD,EMAIL,EMAIL_VERIFIED,ACCOUNT_NON_EXPIRED,ACCOUNT_NON_LOCKED,ENABLED,CREDENTIALS_NON_EXPIRED)
VALUES (1,'Alisher','Urunov','b006c568-a79b-4c28-a06c-df7d89579e00','$2y$12$vLPHTGSDyNWBdH.zxuJgfeiExUwh3zT3Cgj8ZZzW.38rDNK/yBJtm','[email protected]',TRUE,TRUE,TRUE,TRUE,TRUE);
INSERT INTO USER (USER_ID,FIRST_NAME,LAST_NAME,ID,PASSWORD,EMAIL,EMAIL_VERIFIED,ACCOUNT_NON_EXPIRED,ACCOUNT_NON_LOCKED,ENABLED,CREDENTIALS_NON_EXPIRED)
VALUES (2,'Ralf','Tester','b006c568-a79b-123s-a06c-df7d89579e00','$2a$10$3D8eyR7FouUb3RjKoVd9r.pBa7wZbOCe4BeIgxJmJmT9kbYGU43qC','[email protected]',FALSE,TRUE,TRUE,TRUE,TRUE);
INSERT INTO USER (USER_ID,FIRST_NAME,LAST_NAME,ID,PASSWORD,EMAIL,EMAIL_VERIFIED,ACCOUNT_NON_EXPIRED,ACCOUNT_NON_LOCKED,ENABLED,CREDENTIALS_NON_EXPIRED)
VALUES (3,'Ronald','The Great','b006c568-a79b-123s-1248-df7d89579e00','$2a$10$l9jefOjW/P0/dYH.MkHO8OJkNc7SN/G4ZOK1Lk9ND0x08Yta9hJrq','[email protected]',TRUE,TRUE,TRUE,TRUE,TRUE);
INSERT INTO USER_AUTHORITY (ID_USER,ID_AUTHORITY) VALUES (1,1);
INSERT INTO USER_AUTHORITY (ID_USER,ID_AUTHORITY) VALUES (1,2);
INSERT INTO USER_AUTHORITY (ID_USER,ID_AUTHORITY) VALUES (1,3);
INSERT INTO USER_AUTHORITY (ID_USER,ID_AUTHORITY) VALUES (3,1);

Also thought of possible configuration issues with Spring boot, so I've added jdbc starter dependencies to gradle.build, but it still seems to fail.

Here is part of the test:

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = UserServiceApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = UserServiceApplication.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
class UsersControllerTest {

  private String adminToken;
  private String userToken;

  @Autowired TestRestTemplate restTemplate;
  @Autowired TokenUtils tokenUtils;

  @BeforeEach
  void setUp() {
   userToken = tokenUtils.generateTokenForUser(user());
   adminToken = tokenUtils.generateTokenForUser(admin());
  }

  @Test
  void getOneUser() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Bearer "+adminToken);
    headers.add("Access-Control-Expose-Headers", "Authorization");
    ResponseEntity<UserModel> result =
        restTemplate.exchange(
            "/users/userInfo", HttpMethod.GET, new HttpEntity<>(headers), UserModel.class);
    assertEquals("[email protected]", result.getBody().getEmail());
  }

Upvotes: 1

Views: 1378

Answers (1)

Alisher
Alisher

Reputation: 940

It seems that I had two dependencies in the project

1)Sring-boot-starter-test 2) I've added a bunch of jupiter dependencies with mockito 3*

They've been conflicting. I've removed second, everything seems to be working now.

Upvotes: 2

Related Questions