与TestNG战斗,Spring是一个Hibernate。我正在为Service类编写测试,它总是失败。但没有测试类工作正常。所以App正在运行,但测试不想。
这是我的测试类
@Transactional
public class BorrowerServiceTest {
@Mock
BorrowerDAOImpl borrowerDAO;
@InjectMocks
BorrowerService borrowerService;
@BeforeClass
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void persistTest() {
Borrower borrower = new Borrower.BorrowerBuilder().firstName("Lars").lastName("Urlich").adress("LA")
.phoneNumber("900900990").build();
borrowerService.persist(borrower);
List<Borrower> borrowerList = borrowerService.getBorrowerByName("Lars Urlich");
Assert.assertEquals(true, borrower.equals(borrowerList.get(0)));
}
}
我的借款人服务:
@Service("borrowerService")
@Transactional
public class BorrowerService {
@Autowired
private BorrowerDAO borrowerDAO;
public List<Borrower> getBorrowers() {
return borrowerDAO.getBorrowers();
}
public List<Borrower> getBorrowerByName(String name) {
return borrowerDAO.getBorrowerByName(name);
}
public boolean removeBorrower(Borrower borrower) {
return borrowerDAO.removeBorrower(borrower);
}
public boolean persist(Borrower borrower) {
return borrowerDAO.persist(borrower);
}
}
My BorrowerDAOImpl:
@Repository("borrowerDAO")
@Transactional
public class BorrowerDAOImpl extends DAO implements BorrowerDAO {
@Override
public List<Borrower> getBorrowers() {
List<Borrower> borrowerList = null;
Query query = entityManager.createQuery("SELECT B FROM Borrower B");
borrowerList = query.getResultList();
return borrowerList;
}
@Override
public List<Borrower> getBorrowerByName(String name) {
List<Borrower> borrowerList = null;
String[] values = name.split(" ");
Query query = entityManager.createQuery("SELECT B FROM Borrower B WHERE B.firstName LIKE '" + values[0]
+ "' AND B.lastName LIKE '" + values[1] + "'");
borrowerList = query.getResultList();
return borrowerList;
}
@Override
public boolean removeBorrower(Borrower borrower) {
String firstName = borrower.getFirstName();
String lastName = borrower.getLastName();
Query query = entityManager
.createQuery("DELETE Borrower where FIRST_NAME LIKE :FirstName AND LAST_NAME LIKE :LastName");
query.setParameter("FirstName", firstName);
query.setParameter("LastName", lastName);
query.executeUpdate();
return true;
}
@Override
public boolean persist(Borrower borrower) {
entityManager.persist(borrower);
return true;
}
}
和摘要DAO:
@Repository
@Transactional
public abstract class DAO {
@PersistenceContext
protected EntityManager entityManager;
}
Maven返回失败:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.LinkedList.checkElementIndex(LinkedList.java:555)
at java.util.LinkedList.get(LinkedList.java:476)
at com.me.service.test.BorrowerServiceTest.persistTest(BorrowerServiceTest.java:41)
答案 0 :(得分:0)
我也不得不与此作斗争。这里的问题是你的测试运行在它自己的事务中,因此在方法执行期间不会提交任何内容。现在这就是我所做的:
public class IntegrationTest extends SomeTestBase
{
@Autowired
private PlatformTransactionManager platformTransactionManager;
private TransactionTemplate transactionTemplate;
@Autowired
private BeanToTest beanToTest;
@Override
@Before
public void setup()
{
super.setup();
this.transactionTemplate = new TransactionTemplate(this.platformTransactionManager);
}
@Test
public void fooTest()
{
// given
// when
boolean result = this.transactionTemplate.execute(new TransactionCallback<Boolean>()
{
@Override
public Boolean doInTransaction(TransactionStatus status)
{
return IntegrationTest.this.beanToTest.foo();
}
});
// then
}
}
这允许您在单独的事务中执行方法。请注意,您可以将一些变量声明为final
。
希望有所帮助。
答案 1 :(得分:0)
检查the Spring documentation:看起来您的测试类应该扩展AbstractTestNGSpringContextTests
。
答案 2 :(得分:0)
在整个测试类或甚至方法上使用@Commit
注释来持久保存测试中所做的更改。有关详细信息https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#commit