0
我有以下測試用例。我遇到了@Rollback(false)的問題。它不工作。但junit工作正常。我正在使用mongodb。@Rollback(false)不起作用
我試圖調試代碼,我可以認爲數據庫中的數據。但是在單元測試用例完成後,數據不會保留在數據庫中。
public class CustomerRepositoryIntegrationTest extends AbstractIntegrationTest {
@Autowired
CustomerRepository repository;
@Test
@Rollback(false)
public void savesCustomerCorrectly() {
EmailAddress email = new EmailAddress("[email protected]");
Customer dave = new Customer("Alicia", "Keys");
dave.setEmailAddress(email);
dave.add(new Address("27 Broadway", "New York", "United States"));
Customer result = repository.save(dave);
assertThat(result.getId(), is(notNullValue()));
}
另一類是:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ApplicationConfig.class })
public abstract class AbstractIntegrationTest {
@Autowired
Mongo mongo;
@Before
public void setUp() {
DB database = mongo.getDB("e-store");
// Customers
DBCollection customers = database.getCollection("customer");
customers.remove(new BasicDBObject());
BasicDBObject address = new BasicDBObject();
address.put("city", "New York");
address.put("street", "Broadway");
address.put("country", "United States");
BasicDBList addresses = new BasicDBList();
addresses.add(address);
DBObject dave = new BasicDBObject("firstname", "Dave");
dave.put("lastname", "Matthews");
dave.put("email", "[email protected]");
dave.put("addresses", addresses);
customers.insert(dave);
// Products
DBCollection products = database.getCollection("product");
products.drop();
DBObject iPad = new BasicDBObject("name", "iPad");
iPad.put("description", "Apple tablet device");
iPad.put("price", 499.0);
iPad.put("attributes", new BasicDBObject("connector", "plug"));
DBObject macBook = new BasicDBObject("name", "MacBook Pro");
macBook.put("description", "Apple notebook");
macBook.put("price", 1299.0);
BasicDBObject dock = new BasicDBObject("name", "Dock");
dock.put("description", "Dock for iPhone/iPad");
dock.put("price", 49.0);
dock.put("attributes", new BasicDBObject("connector", "plug"));
products.insert(iPad, macBook, dock);
// Orders
DBCollection orders = database.getCollection("order");
orders.drop();
// Line items
DBObject iPadLineItem = new BasicDBObject("product", iPad);
iPadLineItem.put("amount", 2);
DBObject macBookLineItem = new BasicDBObject("product", macBook);
macBookLineItem.put("amount", 1);
BasicDBList lineItems = new BasicDBList();
lineItems.add(iPadLineItem);
lineItems.add(macBookLineItem);
DBObject order = new BasicDBObject("customer", new DBRef(database, "customer", dave.get("_id")));
order.put("lineItems", lineItems);
order.put("shippingAddress", address);
orders.insert(order);
}
}
任何指針會有幫助。
阿米特
不MongoDB中甚至支持回滾? – Thilo
還可以添加您的CustomerRepository實現? – fgakk