Мне нужно написать тест, который показывает, что моя транзакция запускается.
ClientDAOImpl. java - файл с трансляционными методами.
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ClientDAOImpl implements ClientDAO {
private final Connection conn;
private PreparedStatement st;
private ResultSet rs;
public ClientDAOImpl() throws SQLException, ClassNotFoundException {
this.conn = new PostgresConnection().createConnection();
}
@Override
public User get(final long id) throws SQLException {
User client = null;
this.st = this.conn.prepareStatement("SELECT * FROM CUSTOMERS WHERE sid = ?;");
this.st.setLong(1, id);
this.rs = this.st.executeQuery();
while (this.rs.next()) {
this.rs.getLong("sid");
//some code
client = new User(FIRST_NAME, LAST_NAME, PHONE_NUMBER, EMAIL, CARD_NUMBER,
DELIVERY_ADDRESS, COMMENT);
}
DAO.closing(this.rs, this.st, this.conn);
if (client == null) {
System.out.println("Something wrong with getting client by id - " + id);
} else {
System.out.println("Client with the id = " + id + " successfully retrieved");
}
return client;
}
@Override
@Transactional(propagation = Propagation.SUPPORTS, readOnly = false)
public int insert(final User user) throws SQLException, ClassNotFoundException {
this.st = this.conn.prepareStatement(
"INSERT INTO CUSTOMERS(FIRST_NAME,LAST_NAME,PHONE_NUMBER,EMAIL,CARD_NUMBER,DELIVERY_ADDRESS,COMMENT)VALUES(?,?,?,?,?,?,?);");
//some code
final int res = this.st.executeUpdate();
System.out.println("User " + user + " successfully inserted");
return res;
}
}
MyTest. java - файл с моим тестом
@ContextConfiguration(classes = Store.class, locations = {"classpath*:beans.xml"})
@RunWith(SpringRunner.class)
@WebMvcTest(MainApp.class)
@Transactional
@Rollback(true)
@TestExecutionListeners({TransactionalTestExecutionListener.class})
public class StoreTest {
private LocalValidatorFactoryBean localValidatorFactory;
@MockBean
HttpRequest request;
HttpRequest response;
@MockBean
private User user;
@Mock
private Model model;
@Autowired
MockMvc mockMvc;
@Mock
Store store;
@Autowired
List<Products> products;
@Before
public void setup() {
this.localValidatorFactory = new LocalValidatorFactoryBean();
this.localValidatorFactory.setProviderClass(HibernateValidator.class);
this.localValidatorFactory.afterPropertiesSet();
MockitoAnnotations.initMocks(this);
final Store store = new Store(this.products);
this.mockMvc = MockMvcBuilders.standaloneSetup(store).build();
}
@Test
public void testForTransaction() throws ClassNotFoundException, SQLException {
final User user = new User();
user.setFirstName("Ivan");
user.setLastName("Ivanov");
user.setPhoneNumber("18000000");
user.setEmail("mail@gmail.com");
user.setCardNumber("4111111111111111");
user.setDeliveryAddress("address");
user.setComment("comment");
String result = "";
try {
final Connection connection = new PostgresConnection().createConnection();
if (connection != null) {
System.out.println("Success");
}
final ClientDAO clientDao = new ClientDAOImpl();
clientDao.insert(user);
result = clientDao.get(3L).getFirstName();
} finally {
}
Assert.assertEquals("it should be equal", "Ivan", result);
}
И мой тест прошел успешно, и я получаю следующее:
User my.app.entities.User@2e5ee2c9 successfully inserted
Client with the id = 3 successfully retrieved
Rolled back transaction for test context [DefaultTestContext@7103ab0 testClass = StoreTest,...// and etc
Итак, мой вопрос в том, как написать правильный тест для моего примера и показать что моя транзакция работает?