這是我的代碼的一個非常簡化的版本,它說明了具體的問題。模擬加載導航屬性
有沒有什麼辦法可以控制從測試中調用accountProductRepository.refresh()時會發生什麼?
不知何故,我需要在buyProduct()方法中創建的AccountProductPojo上設置ProductPojo,所以在訪問getProduct()。getName()屬性時不會獲得空指針。
refresh使用javax.persistence.EntityManager.refresh()根據在buyProduct()方法中設置的id加載導航屬性。
public class ProductServiceTest {
@InjectMocks
IProductService productService = new ProductService();
@Mock
IWriteANoteService writeANoteService;
@Mock
IAccountProductRepository accountProductRepository;
@Test
public void buyProductTest() {
productService.buyProduct(1l, 1l);
}
}
@Service
public class ProductService implements IProductService {
@Autowired
IWriteANoteService writeANoteService;
@Autowired
IAccountProductRepository accountProductRepository:
public void buyProduct(Long productId, Long accountId) {
AccountProductPojo accountProduct = new AccountProductPojo();
accountProduct.setProductId(productId);
accountProduct.setAccountId(accountId);
accountProductRepository.persist(accountProduct);
// load navigation properties
accountProductRepository.refresh(accountProduct);
writeANoteService.writeAccountNote(accountId, "Bought product " + accountProduct.getProduct().getName());
}
}
@Entity
@Table(name = "account_product")
public class AccountProductPojo {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "account_id")
private Long accountId;
@Column(name = "product_id")
private Integer productId;
@ManyToOne
@JoinColumn(name = "product_id", insertable = false, updatable = false)
private ProductPojo product;
@OneToOne(fetch = FetchType.LAZY, targetEntity = AccountPojo.class)
@JoinColumn(name = "account_id", insertable = false, updatable = false)
private AccountPojo account;
// getters and setters
}
Mockito.when(yourMethod).thenReturn(something)。 https://mockito.googlecode.com/hg-history/1.7/javadoc/org/mockito/Mockito.html – Damiano
除此之外,它看起來像buyProduct()中的一個bug,您可以在其中調用'setProductId(accountId)'。 productId參數未使用。 –
哦,是的,這是一個複製粘貼錯誤,現在編輯。 – heuts