TestNGでファイティング、Springは休止状態です。私はServiceクラスのテストを書いていますが、それは常に失敗です。しかし、テストクラスなしで正常に動作します。だからアプリケーションは動作しているが、テストはしたくない。ここで Spring + Hibernate + TestNG +黙って何も持続せず、何もテストで読み込まれません。
は私のテストクラスです@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)));
}
}
マイBorrowerService:
@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);
}
}
マイ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)
残念ながら、それはありませんが、ありがとう! – egzaell