0

We have a Java/SpringBoot RESTful microservice and an associated integration-test module that is running Cucumber steps. The problem is that when the test is run, although the Cucumber call to the API to insert data does in fact insert data and return a fully populated class with a new id and validation for the response also passes (in Cucumber), when we call the checkData() method/step, then the result is 0 records. Like we have 2 datasources configured, but checking through the logs suggests that this is not the case!

The following contains the crux of the code that is present in the integration-test. My belief is that some of the annotations may be wrong; I've tried for 2 days fiddling with the annotations and application config yml file, but almost always the same error. I tried DirtiesContext on the Cucumber class, setting IsolationLevel to ReadUncommitted, but never can I see that "inserted" record in the Cucumber step!

@CucumberContextConfiguration
@ContextConfiguration(classes = {MainApp.class})
@RunWith(Cucumber.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, 
    classes = {MainApp.class})
@Import(MainApp.class)
@EnableAutoConfiguration
@AutoConfigureWireMock(port = 0)
public class ApplicationTestSteps {
  
  @Autowired
  private JdbcTemplate jdbcTemplate;

  private List<Book> retrieveAllBooks() {
    return jdbcTemplate.query("select * from t_books", new BookMapper());
  }

  @When("I call the insert data endpoint in the app") {
     //api call to the app that inserts data into the db
  }

  @And("I check the database has the data")
  public void checkData() {
    assertThat(retrieveAllBooks().size().isGreaterThan(0));
  }
}

The following is the application.yml file;

spring:
  datasource:
    url: jdbc:h2:memLTESTDB;MODE=DB2
    username: sa
    password:
    driver-class-name: org.h2.Driver
  jpa:
    defer-datasource-initialisation: true
    generate-ddl: false

hibernate:
  hbm2ddl:
    auto: none
    hibernate:
      hbm2ddl.auto: update

Finally, we are using a /resources/schema.sql and /resources/data.sql to initialise the DB because the DB is an old DB2 database and the Entity classes in the API codebase are only selecting a subset of many of the fields.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.