将 Junit 中 Argument Captor 的选择性字段与 Mockito 进行比较
Comparing selective fields from an Argument Captor in Junit with Mockito
我正在使用 Spock 和 Mockito 框架编写我的单元测试,并且偶然发现了 Mockito 中的一个限制,我无法很好地解决它。
以下代码解析 .csv 文件和 returns TradedInstrument 对象的集合:
@ManagedOperation
public Collection<TradedInstrument> loadTradedInstrumentFromBatchFile() {
Collection<TradedInstrument> tradedInstrumentsFrombatchFile = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new InputStreamReader(
this.getClass().getResourceAsStream("/" + tradedInstrumentBatchFilename)))) {
String line;
while ((line = br.readLine()) != null) {
String[] instrumentAttributes = line.split(",");
TradedInstrument tradedInstrument = new TradedInstrument();
tradedInstrument.setInstId(instrumentAttributes[0]);
tradedInstrument.setPriceSource(PriceSource.valueOf(instrumentAttributes[1]));
tradedInstrument.setPrice(new BigDecimal(instrumentAttributes[2]));
tradedInstrument.setDateCreated(businessDateDao.getBusinessDate());
insertTradedInstrument(tradedInstrument);
tradedInstrumentsFrombatchFile.add(tradedInstrument);
LOGGER.info("Loaded traded instrument: " + tradedInstrument + " from batch file: " + tradedInstrumentBatchFilename);
}
} catch (NullPointerException e) {
String errorMessage = "An ERROR occurred locating the traded instrument upload batch file: " + tradedInstrumentBatchFilename;
LOGGER.error(errorMessage);
throw new PriceServiceException(errorMessage, e);
} catch (IOException e) {
String errorMessage = "An ERROR occurred reading the traded instrument upload batch file: " + tradedInstrumentBatchFilename;
LOGGER.error(errorMessage);
throw new PriceServiceException(errorMessage, e);
} catch (Exception e) {
String errorMessage = "An ERROR occurred creating traded instrument using data from upload batch file: " + tradedInstrumentBatchFilename;
LOGGER.error(errorMessage, e);
throw new PriceServiceException(errorMessage, e);
}
return tradedInstrumentsFrombatchFile;
}
我有一个名为 "tradedInstrumentBatchFile.csv" 的批处理文件,其中包含以下内容:
1234,ICAP,0.4956 2345,BBG,0.8456 8456,NASDAQ,0.3567 5967,REUTERS,0.8675
我在 Groovy 中编写了以下 Spock 测试:
def "should load traded instrument from batch file"() {
given:
TradedInstrumentSubscriber tradedInstrumentSubscriber = Mock()
TradedInstrumentTable tradedInstrumentDao = Mock()
VendorTopicDao vendorTopicDao = Mock()
TradedInstrumentListener listener = Mock()
BusinessDateDao businessDateDao = Mock()
PriceService priceService = new PriceService(tradedInstrumentSubscriber, listener, vendorTopicDao, tradedInstrumentDao, businessDateDao)
priceService.setTradedInstrumentBatchFilename("tradedInstrumentBatchFile.csv")
businessDateDao.getBusinessDate() >> new Date()
when:
priceService.loadTradedInstrumentFromBatchFile()
then:
1 * tradedInstrumentDao.insert(_ as TradedInstrument) >> { TradedInstrument arg ->
assert arg.instId == "1234"
assert arg.priceSource == PriceSource.ICAP
assert arg.price == BigDecimal.valueOf(0.4956)
}
1 * tradedInstrumentDao.insert(_ as TradedInstrument) >> { TradedInstrument arg ->
assert arg.instId == "2345"
assert arg.priceSource == PriceSource.BBG
assert arg.price == BigDecimal.valueOf(0.8456)
}
1 * tradedInstrumentDao.insert(_ as TradedInstrument) >> { TradedInstrument arg ->
assert arg.instId == "8456"
assert arg.priceSource == PriceSource.NASDAQ
assert arg.price == BigDecimal.valueOf(0.3567)
}
1 * tradedInstrumentDao.insert(_ as TradedInstrument) >> { TradedInstrument arg ->
assert arg.instId == "5967"
assert arg.priceSource == PriceSource.REUTERS
assert arg.price == BigDecimal.valueOf(0.8675)
}
}
我已经尝试使用 Mockito 编写了等效的 Junit 测试:
@Test
public void shouldLoadTradedInstrumentFromBatchFile() {
// Given
VendorTopicDao vendorTopicDao = mock(VendorTopicDao.class);
TradedInstrumentSubscriber tradedInstrumentSubscriber = mock(TradedInstrumentSubscriber.class);
TradedInstrumentTable tradedInstrumentDao = mock(TradedInstrumentTable.class);
TradedInstrumentListener listener = mock(TradedInstrumentListener.class);
BusinessDateDao businessDateDao = mock(BusinessDateDao.class);
PriceService priceService = new PriceService(tradedInstrumentSubscriber, listener, vendorTopicDao, tradedInstrumentDao);
priceService.setTradedInstrumentBatchFilename("tradedInstrumentBatchFile.csv");
ArgumentCaptor<TradedInstrument> captor = ArgumentCaptor.forClass(TradedInstrument.class);
when(businessDateDao.getBusinessDate()).thenReturn(new Date());
// When
priceService.loadTradedInstrumentFromBatchFile();
// Then
verify(tradedInstrumentDao, times(4)).insert(captor.capture());
List<TradedInstrument> tradedInstruments = captor.getAllValues();
TradedInstrument tradedInstrument1 = new TradedInstrument();
tradedInstrument1.setInstId("1234");
tradedInstrument1.setPriceSource(PriceSource.ICAP);
tradedInstrument1.setPrice(BigDecimal.valueOf(0.4956));
TradedInstrument tradedInstrument2 = new TradedInstrument();
tradedInstrument2.setInstId("2345");
tradedInstrument2.setPriceSource(PriceSource.BBG);
tradedInstrument2.setPrice(BigDecimal.valueOf(0.8456));
TradedInstrument tradedInstrument3= new TradedInstrument();
tradedInstrument2.setInstId("8456");
tradedInstrument2.setPriceSource(PriceSource.NASDAQ);
tradedInstrument2.setPrice(BigDecimal.valueOf(0.3567));
TradedInstrument tradedInstrument4= new TradedInstrument();
tradedInstrument2.setInstId("5967");
tradedInstrument2.setPriceSource(PriceSource.REUTERS);
tradedInstrument2.setPrice(BigDecimal.valueOf(0.8675));
assertThat(tradedInstruments, hasItem(tradedInstrument1));
assertThat(tradedInstruments, hasItem(tradedInstrument2));
assertThat(tradedInstruments, hasItem(tradedInstrument3));
assertThat(tradedInstruments, hasItem(tradedInstrument4));
}
Junit 测试失败,因为测试在比较中包含字段 "dateCreated",而 Spock 测试选择性地忽略了该字段。
字段 "dateCreated" 应该是创建 TradedInstrument 的实际时间,因此需要有意将其排除在比较之外。
我能看到的唯一解决方案是在 businessDateDao 上添加以下交互:
when(businessDateDao.getBusinessDate()).thenReturn(null);
是否有等效的方法在 Junit/Mockito 中使用匹配器来选择性地忽略字段的比较?
您可以结合使用 hasItem
和 hasProperty
来将您的断言集中在 TradedInstrument
的属性上,其中 已填充。例如:
assertThat(tradedInstruments, hasItem(hasProperty("instId", is(1234))));
assertThat(tradedInstruments, hasItem(hasProperty("priceSource", is(PriceSource.ICAP))));
assertThat(tradedInstruments, hasItem(hasProperty("price", is(BigDecimal.valueOf(0.4956)))));
或者您可以声明一个自定义匹配器...
private Matcher<TradedInstrument> isEquivalent(final String instId, final PriceSource priceSource, final BigDecimal price) {
return new BaseMatcher<TradedInstrument>() {
@Override
public boolean matches(final Object item) {
final TradedInstrument tradedInstrument = (TradedInstrument) item;
// your custom equality implementation e.g.
return instId.equals(tradeInstrument.getInstId()) && priceSource == tradeInstrument.getPriceSource() && price.equals(tradeInstrument.getPrice());
}
@Override
public void describeTo(final Description description) {
description.appendText(String.format("the given object should contain id=%s, priceSource=%s, price=%s ", id, priceSource, price));
}
};
}
... 并像这样使用它:
assertThat(tradedInstruments, hasItem(isEquivalent(1, PriceSource.ICAP, BigDecimal.valueOf(0.4956))));
我正在使用 Spock 和 Mockito 框架编写我的单元测试,并且偶然发现了 Mockito 中的一个限制,我无法很好地解决它。
以下代码解析 .csv 文件和 returns TradedInstrument 对象的集合:
@ManagedOperation
public Collection<TradedInstrument> loadTradedInstrumentFromBatchFile() {
Collection<TradedInstrument> tradedInstrumentsFrombatchFile = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new InputStreamReader(
this.getClass().getResourceAsStream("/" + tradedInstrumentBatchFilename)))) {
String line;
while ((line = br.readLine()) != null) {
String[] instrumentAttributes = line.split(",");
TradedInstrument tradedInstrument = new TradedInstrument();
tradedInstrument.setInstId(instrumentAttributes[0]);
tradedInstrument.setPriceSource(PriceSource.valueOf(instrumentAttributes[1]));
tradedInstrument.setPrice(new BigDecimal(instrumentAttributes[2]));
tradedInstrument.setDateCreated(businessDateDao.getBusinessDate());
insertTradedInstrument(tradedInstrument);
tradedInstrumentsFrombatchFile.add(tradedInstrument);
LOGGER.info("Loaded traded instrument: " + tradedInstrument + " from batch file: " + tradedInstrumentBatchFilename);
}
} catch (NullPointerException e) {
String errorMessage = "An ERROR occurred locating the traded instrument upload batch file: " + tradedInstrumentBatchFilename;
LOGGER.error(errorMessage);
throw new PriceServiceException(errorMessage, e);
} catch (IOException e) {
String errorMessage = "An ERROR occurred reading the traded instrument upload batch file: " + tradedInstrumentBatchFilename;
LOGGER.error(errorMessage);
throw new PriceServiceException(errorMessage, e);
} catch (Exception e) {
String errorMessage = "An ERROR occurred creating traded instrument using data from upload batch file: " + tradedInstrumentBatchFilename;
LOGGER.error(errorMessage, e);
throw new PriceServiceException(errorMessage, e);
}
return tradedInstrumentsFrombatchFile;
}
我有一个名为 "tradedInstrumentBatchFile.csv" 的批处理文件,其中包含以下内容:
1234,ICAP,0.4956 2345,BBG,0.8456 8456,NASDAQ,0.3567 5967,REUTERS,0.8675
我在 Groovy 中编写了以下 Spock 测试:
def "should load traded instrument from batch file"() {
given:
TradedInstrumentSubscriber tradedInstrumentSubscriber = Mock()
TradedInstrumentTable tradedInstrumentDao = Mock()
VendorTopicDao vendorTopicDao = Mock()
TradedInstrumentListener listener = Mock()
BusinessDateDao businessDateDao = Mock()
PriceService priceService = new PriceService(tradedInstrumentSubscriber, listener, vendorTopicDao, tradedInstrumentDao, businessDateDao)
priceService.setTradedInstrumentBatchFilename("tradedInstrumentBatchFile.csv")
businessDateDao.getBusinessDate() >> new Date()
when:
priceService.loadTradedInstrumentFromBatchFile()
then:
1 * tradedInstrumentDao.insert(_ as TradedInstrument) >> { TradedInstrument arg ->
assert arg.instId == "1234"
assert arg.priceSource == PriceSource.ICAP
assert arg.price == BigDecimal.valueOf(0.4956)
}
1 * tradedInstrumentDao.insert(_ as TradedInstrument) >> { TradedInstrument arg ->
assert arg.instId == "2345"
assert arg.priceSource == PriceSource.BBG
assert arg.price == BigDecimal.valueOf(0.8456)
}
1 * tradedInstrumentDao.insert(_ as TradedInstrument) >> { TradedInstrument arg ->
assert arg.instId == "8456"
assert arg.priceSource == PriceSource.NASDAQ
assert arg.price == BigDecimal.valueOf(0.3567)
}
1 * tradedInstrumentDao.insert(_ as TradedInstrument) >> { TradedInstrument arg ->
assert arg.instId == "5967"
assert arg.priceSource == PriceSource.REUTERS
assert arg.price == BigDecimal.valueOf(0.8675)
}
}
我已经尝试使用 Mockito 编写了等效的 Junit 测试:
@Test
public void shouldLoadTradedInstrumentFromBatchFile() {
// Given
VendorTopicDao vendorTopicDao = mock(VendorTopicDao.class);
TradedInstrumentSubscriber tradedInstrumentSubscriber = mock(TradedInstrumentSubscriber.class);
TradedInstrumentTable tradedInstrumentDao = mock(TradedInstrumentTable.class);
TradedInstrumentListener listener = mock(TradedInstrumentListener.class);
BusinessDateDao businessDateDao = mock(BusinessDateDao.class);
PriceService priceService = new PriceService(tradedInstrumentSubscriber, listener, vendorTopicDao, tradedInstrumentDao);
priceService.setTradedInstrumentBatchFilename("tradedInstrumentBatchFile.csv");
ArgumentCaptor<TradedInstrument> captor = ArgumentCaptor.forClass(TradedInstrument.class);
when(businessDateDao.getBusinessDate()).thenReturn(new Date());
// When
priceService.loadTradedInstrumentFromBatchFile();
// Then
verify(tradedInstrumentDao, times(4)).insert(captor.capture());
List<TradedInstrument> tradedInstruments = captor.getAllValues();
TradedInstrument tradedInstrument1 = new TradedInstrument();
tradedInstrument1.setInstId("1234");
tradedInstrument1.setPriceSource(PriceSource.ICAP);
tradedInstrument1.setPrice(BigDecimal.valueOf(0.4956));
TradedInstrument tradedInstrument2 = new TradedInstrument();
tradedInstrument2.setInstId("2345");
tradedInstrument2.setPriceSource(PriceSource.BBG);
tradedInstrument2.setPrice(BigDecimal.valueOf(0.8456));
TradedInstrument tradedInstrument3= new TradedInstrument();
tradedInstrument2.setInstId("8456");
tradedInstrument2.setPriceSource(PriceSource.NASDAQ);
tradedInstrument2.setPrice(BigDecimal.valueOf(0.3567));
TradedInstrument tradedInstrument4= new TradedInstrument();
tradedInstrument2.setInstId("5967");
tradedInstrument2.setPriceSource(PriceSource.REUTERS);
tradedInstrument2.setPrice(BigDecimal.valueOf(0.8675));
assertThat(tradedInstruments, hasItem(tradedInstrument1));
assertThat(tradedInstruments, hasItem(tradedInstrument2));
assertThat(tradedInstruments, hasItem(tradedInstrument3));
assertThat(tradedInstruments, hasItem(tradedInstrument4));
}
Junit 测试失败,因为测试在比较中包含字段 "dateCreated",而 Spock 测试选择性地忽略了该字段。
字段 "dateCreated" 应该是创建 TradedInstrument 的实际时间,因此需要有意将其排除在比较之外。
我能看到的唯一解决方案是在 businessDateDao 上添加以下交互:
when(businessDateDao.getBusinessDate()).thenReturn(null);
是否有等效的方法在 Junit/Mockito 中使用匹配器来选择性地忽略字段的比较?
您可以结合使用 hasItem
和 hasProperty
来将您的断言集中在 TradedInstrument
的属性上,其中 已填充。例如:
assertThat(tradedInstruments, hasItem(hasProperty("instId", is(1234))));
assertThat(tradedInstruments, hasItem(hasProperty("priceSource", is(PriceSource.ICAP))));
assertThat(tradedInstruments, hasItem(hasProperty("price", is(BigDecimal.valueOf(0.4956)))));
或者您可以声明一个自定义匹配器...
private Matcher<TradedInstrument> isEquivalent(final String instId, final PriceSource priceSource, final BigDecimal price) {
return new BaseMatcher<TradedInstrument>() {
@Override
public boolean matches(final Object item) {
final TradedInstrument tradedInstrument = (TradedInstrument) item;
// your custom equality implementation e.g.
return instId.equals(tradeInstrument.getInstId()) && priceSource == tradeInstrument.getPriceSource() && price.equals(tradeInstrument.getPrice());
}
@Override
public void describeTo(final Description description) {
description.appendText(String.format("the given object should contain id=%s, priceSource=%s, price=%s ", id, priceSource, price));
}
};
}
... 并像这样使用它:
assertThat(tradedInstruments, hasItem(isEquivalent(1, PriceSource.ICAP, BigDecimal.valueOf(0.4956))));