我想编写一个验证器,该验证器将检查用户的年龄是否足以成为另一个用户的父母。因此,例如,我有以下代码,可以获取表单信息并可以检查错误:
Testing started at 0:09 ...
12/17 00:09:32: Launching InstrumentedTest
$ adb push /Users/a.busik/EspressoTest/app/build/outputs/apk/debug/app-debug.apk /data/local/tmp/com.example.abusik.espressotest
$ adb shell pm install -t -r "/data/local/tmp/com.example.abusik.espressotest"
Success
APK installed in 3 s 235 ms
$ adb push /Users/a.busik/EspressoTest/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk /data/local/tmp/com.example.abusik.espressotest.test
$ adb shell pm install -t -r "/data/local/tmp/com.example.abusik.espressotest.test"
Success
APK installed in 2 s 684 ms
Running tests
$ adb shell am instrument -w -r -e debug false -e class 'com.example.abusik.espressotest.InstrumentedTest' com.example.abusik.espressotest.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
java.lang.NoSuchMethodError: No static method allOf(Lorg/hamcrest/Matcher;Lorg/hamcrest/Matcher;)Lorg/hamcrest/Matcher; in class Lorg/hamcrest/core/AllOf; or its super classes (declaration of 'org.hamcrest.core.AllOf' appears in /system/framework/com.android.nfc_extras.jar)
at org.hamcrest.Matchers.allOf(Matchers.java:33)
at android.support.test.espresso.Espresso.<clinit>(Espresso.java:187)
at android.support.test.espresso.Espresso.onView(Espresso.java:75)
at com.example.abusik.espressotest.InstrumentedTest.changeText_sameActivity(InstrumentedTest.kt:34)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.-wrap0(Unknown Source:0)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:126)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.-wrap0(Unknown Source:0)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2106)
Tests ran to completion.
我已经开始编写自定义验证器:
@RequestMapping(value = "/setParent", method = RequestMethod.POST)
public ModelAndView setParent(@Valid @ModelAttribute("person") Person parent, BindingResult bindingResult,
HttpServletRequest request) {
logger.info("set parent controller");
if (bindingResult.hasErrors()) {
return new ModelAndView("index", "parent", parent);
} else {
String personName = request.getParameter("personName");
String relation = request.getParameter("relation");
Person person = new Person(personName, null);
int pID = databaseModel.getPersonID(person);
int parentID = databaseModel.getPersonID(parent);
int rID = databaseModel.getRelationID(relation);
databaseModel.insertRelation(pID, parentID, rID);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("setParentSuccess");
modelAndView.addObject("parent", databaseModel.getPersonTree(parentID));
return modelAndView;
}
}
,但随后意识到我无法获得第二个用户日期与此验证器进行比较。怎么做?谢谢