如何测试传递给 ScalarDB 函数的参数
How to test the arguments passed to a function of ScalarDB
我正在使用 ScalarDB
。它提供了超过 Cassandra
的 ACID 功能。 - https://scalar-labs.github.io/scalardb/javadoc/
ScalarDB
基于 Java
,我在 Scala
代码中使用它
使用Scalardb
在Cassandra
中插入值的方法是调用Put
方法。例如
def add() = {
val putAnswer: Put = new Put(pAnswerKey)
.forNamespace(keyspaceName)
.forTable(tablename)
.withCondition(mutationCondition)
.withValue(new TextValue("answer_id", answer.answer_id.get.toString))
.withValue(new TextValue("image", convertImageToString(imageData)))
.withValue(new TextValue("answer", convertAnswersFromModelToString(answer.answer)))
.withValue(new BigIntValue("creation_year", answer.creationYear.getOrElse(0)))
.withValue(new BigIntValue("creation_month", answer.creationMonth.getOrElse(0)))
.withValue(new TextValue("notes", answer.notes.getOrElse("")))
logger.trace(s"putting answer ${putAnswer}")
transaction.put(putAnswer)
....
}
我想通过捕获传递给 put
的参数来对 add
方法进行单元测试。我不知道如何将 Value
传递给 put
。如何将类型强制转换为所需值?
repository.add(repoTestEnv.mockDistributedTransaction,repoTestEnv.answerTestEnv.answerOfAPracticeQuestion)
val argumentCaptor = ArgumentCaptor.forClassPut,Put
verify(repoTestEnv.mockDistributedTransaction,times(1)).put(argumentCaptor.capture())
val argumentsInvoked = argumentCaptor.getAllValues
argumentsInvoked.size mustBe 1
val argument = argumentsInvoked.get(0)
val values = argument.getValues //this is util.Map[String,Value[_]]
logger.trace(s"arguments were ${argument.getValues}") //this prints the following
{answer_id=TextValue{name=answer_id, value=Optional[11111111-1111-1111-1111-111111111111]}, image=TextValue{name=image, value=Optional[{"image":["image1binarydata","image2binarydata"]}]}, answer=TextValue{name=answer, value=Optional[{"answer":[{"filename":"c.js","answer":"some answer"}]}]}, creation_year=BigIntValue{name=creation_year, value=2019}, creation_month=BigIntValue{name=creation_month, value=12}, notes=TextValue{name=notes, value=Optional[some notes]}}
问题 - 我不知道如何进一步选择每个值并进行比较。
由于 Value
是一个接口,我不得不像下面这样对其进行类型转换。
val value = values.get("answer_id").asInstanceOf[TextValue]
value.getString.isPresent mustBe true
value.getString.get() mustBe "11111111-1111-1111-1111-111111111111"
为什么不创建预期的 Put 并使用 Mockito 进行验证?
我正在使用 ScalarDB
。它提供了超过 Cassandra
的 ACID 功能。 - https://scalar-labs.github.io/scalardb/javadoc/
ScalarDB
基于 Java
,我在 Scala
代码中使用它
使用Scalardb
在Cassandra
中插入值的方法是调用Put
方法。例如
def add() = {
val putAnswer: Put = new Put(pAnswerKey)
.forNamespace(keyspaceName)
.forTable(tablename)
.withCondition(mutationCondition)
.withValue(new TextValue("answer_id", answer.answer_id.get.toString))
.withValue(new TextValue("image", convertImageToString(imageData)))
.withValue(new TextValue("answer", convertAnswersFromModelToString(answer.answer)))
.withValue(new BigIntValue("creation_year", answer.creationYear.getOrElse(0)))
.withValue(new BigIntValue("creation_month", answer.creationMonth.getOrElse(0)))
.withValue(new TextValue("notes", answer.notes.getOrElse("")))
logger.trace(s"putting answer ${putAnswer}")
transaction.put(putAnswer)
....
}
我想通过捕获传递给 put
的参数来对 add
方法进行单元测试。我不知道如何将 Value
传递给 put
。如何将类型强制转换为所需值?
repository.add(repoTestEnv.mockDistributedTransaction,repoTestEnv.answerTestEnv.answerOfAPracticeQuestion) val argumentCaptor = ArgumentCaptor.forClassPut,Put
verify(repoTestEnv.mockDistributedTransaction,times(1)).put(argumentCaptor.capture())
val argumentsInvoked = argumentCaptor.getAllValues
argumentsInvoked.size mustBe 1
val argument = argumentsInvoked.get(0)
val values = argument.getValues //this is util.Map[String,Value[_]]
logger.trace(s"arguments were ${argument.getValues}") //this prints the following
{answer_id=TextValue{name=answer_id, value=Optional[11111111-1111-1111-1111-111111111111]}, image=TextValue{name=image, value=Optional[{"image":["image1binarydata","image2binarydata"]}]}, answer=TextValue{name=answer, value=Optional[{"answer":[{"filename":"c.js","answer":"some answer"}]}]}, creation_year=BigIntValue{name=creation_year, value=2019}, creation_month=BigIntValue{name=creation_month, value=12}, notes=TextValue{name=notes, value=Optional[some notes]}}
问题 - 我不知道如何进一步选择每个值并进行比较。
由于 Value
是一个接口,我不得不像下面这样对其进行类型转换。
val value = values.get("answer_id").asInstanceOf[TextValue]
value.getString.isPresent mustBe true
value.getString.get() mustBe "11111111-1111-1111-1111-111111111111"
为什么不创建预期的 Put 并使用 Mockito 进行验证?