#Trying to write unit tests but I'm having trouble setting up the H2 database. Pls help!
1 messages · Page 1 of 1 (latest)
And this is my test class:
@DataJdbcTest
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
// @Table("cart")
public class CartRepositoryTests {
@Autowired
CartRepository cartRepository;
@Autowired
UserRepository userRepository;
@Autowired
ProductRepository productRepository;
private User userOne;
private User userTwo;
private Product productOne;
private Product productTwo;
private CartItem cartItemOne;
private CartItem cartItemTwo;
@BeforeAll
public static void initTest() throws SQLException {
Server.createWebServer("-web", "-webAllowOthers", "-webPort", "8082")
.start();
}
@BeforeEach
void init() {
userOne = new User("John", "Doe", "john123", "[email protected]");
userRepository.save(userOne);
userTwo = new User("Jane", "Doe", "jane123", "[email protected]");
userRepository.save(userTwo);
productOne = new Product("Product 1", "This is Product 1.", 25.0, ProductCategory.MENS_CLOTHING.name());
productRepository.save(productOne);
productTwo = new Product("Product 2", "This is Product 2.", 25.0, ProductCategory.MENS_CLOTHING.name());
productRepository.save(productTwo);
cartItemOne = new CartItem();
cartItemOne.setUserId(userOne.getId());
cartItemOne.setProductId(productOne.getId());
cartItemOne.setQuantity(4);
cartItemTwo = new CartItem();
cartItemTwo.setUserId(userOne.getId());
cartItemTwo.setProductId(productTwo.getId());
cartItemTwo.setQuantity(2);
cartRepository.save(cartItemOne);
cartRepository.save(cartItemTwo);
}
@Test
void shouldFindByUserIdOrderByCartItemIdDesc() {
...
}
}
Detected code, here are some useful tools:
"Table "PRODUCT" not found (candidates are: "product"); SQL statement:
INSERT INTO "PRODUCT" ("CATEGORY", "DESCRIPTION", "PRICE", "TITLE") VALUES (?, ?, ?, ?)" It seems like the H2 database tries to name the database in all uppercase whereas Spring Data JDBC looks for lower case names. And according to this thread, https://stackoverflow.com/questions/63838715/spring-data-jdbc-error-badsqlgrammarexception-preparedstatementcallback-bad-sq, using double-quotes in table names should fix it.
Additionally, I also added DATABASE_TO_UPPER=false to the H2 database URL but I guess that's not helping either.
nvm crisis averted
Closed the thread.