1
\$\begingroup\$

For unit testing my ProductList service, I tried run a script to insert some data and then try to get this data and compare the list size.

But my database insert script is not running and returns null.

The related code is below.

Looking forward to your evaluation.

Thanks.

 @Test
    public void getProductsList() throws Exception {

        initDatabase();

        RequestBuilder request = MockMvcRequestBuilders
                .get ("http://localhost:8080/products")
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON);

        MvcResult result = mockMvc.perform(request).andReturn();

        String content = result.getResponse().getContentAsString();

        Product[] productList = super.mapFromJson(content, Product[].class);
        assertEquals(3, productList.length);
    }

InitDatabase method :

private static String connection_url = "jdbc:h2:file:C:/data/sample/product;DB_CLOSE_DELAY=-1";
private static String username = "";
private static String password = "";

 public static void initDatabase() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(connection_url, username, password);
            ResultSet rs = RunScript.execute(connection, new FileReader("src/test/resources/testDataProduct.sql"));
            System.out.println(rs.toString());
        } catch (Exception e) {
            throw new TransactionException("Error creating test data :" + e.getMessage());
        } finally {
            //DbUtils.closeQuietly(connection);
        }
    }

SQL:

-- Test data initialization

DROP TABLE IF EXISTS PRODUCT;

CREATE TABLE PRODUCT (ID LONG PRIMARY KEY AUTO_INCREMENT NOT NULL,
                      CATEGORY VARCHAR(30) NOT NULL,
                      NAME VARCHAR(30) NOT NULL,
                      DESCRIPTION VARCHAR(30) NOT NULL,
                      UNITPRICE DECIMAL(19,4) NOT NULL,
                      INVENTORY VARCHAR(10) NOT NULL,
                      PAYMENT_OPTIONS VARCHAR(30) NOT NULL,
                      DELIVERY_OPTIONS VARCHAR(30) NOT NULL);

CREATE UNIQUE INDEX idx_acc on PRODUCT(ID,NAME);

INSERT INTO PRODUCT (ID, CATEGORY, NAME, DESCRIPTION, UNITPRICE, INVENTORY, PAYMENT_OPTIONS, DELIVERY_OPTIONS)
VALUES (101,'ELECTRONICS','TV','LG Smart TV',1000.0000,20,'DIRECT','X');

INSERT INTO PRODUCT (ID, CATEGORY, NAME, DESCRIPTION, UNITPRICE, INVENTORY, PAYMENT_OPTIONS, DELIVERY_OPTIONS)
VALUES (102,'FASHION','Skirt 101','Black Skirt',20.0000,10,'INSTALLMENT','X');

INSERT INTO PRODUCT (ID, CATEGORY, NAME, DESCRIPTION, UNITPRICE, INVENTORY, PAYMENT_OPTIONS, DELIVERY_OPTIONS)
VALUES (103,'FOOD','Bread','Whole Wheat Bread',5,5,'DIRECT','X');

I am getting error:

org.hibernate.TransactionException: Error creating test data :null

    at com.merchant.springjwtmerchant.ProductServiceControllerTest.initDatabase(ProductServiceControllerTest.java:77)
    at com.merchant.springjwtmerchant.ProductServiceControllerTest.getProductsList(ProductServiceControllerTest.java:144)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
\$\endgroup\$
1