2017-11-27 17 views
0

私のテストクラスは、この春の検証でユニットテストのカスタムバリデータ

public class UserRequestValidatorTest { 

    @Mock 
    SmartValidator smartValidator; 

    @InjectMocks 
    UserRequestValidator userRequestValidator; 

    private UserRequest userRequest; 

    @Before 
    public void setUp(){ 
     MockitoAnnotations.initMocks(this); 
     userRequest = new UserRequest(); 

     userRequest.setFirstName("FirstName"); 
     userRequest.setMiddleInitial("M"); 
     userRequest.setLastName("LastName"); 
    } 

    @Test 
    public void validateUserRequest_testFirstName() throws Exception { 

     userRequest.setFirstName(null); 
     RequestWrapper requestWrapper = new RequestWrapper(); 
     requestWrapper.setRequest(userRequest); 

     Errors errors = new BeanPropertyBindingResult(requestWrapper, "requestWrapper"); 
     userRequestValidator.validate(requestWrapper, errors); 

     assertTrue(errors.hasErrors()); 
     assertNotNull(errors.getFieldError("firstName")); 
    } 
    //More tests 
のように見えます

public class UserRequest{ 

     @NotNull 
     @Size(min = 1, max = 20) 
     @Pattern(regexp = ValidationRegex.ALPHA_SPECIAL) 
     private String firstName; 

     @Size(max = 1) 
     @Pattern(regexp = ValidationRegex.ALPHA) 
     private String middleInitial; 

     @NotNull 
     @Size(min = 1, max = 20) 
     @Pattern(regexp = ValidationRegex.ALPHA_SPECIAL) 
     private String lastName; 

     //more fields and getters and setters 
} 

以下のように検証用のBeanがある

@Component 
public class RequestValidator implements Validator { 

    private SmartValidator smartValidator; 

    @Autowired 
    public RequestValidator(@Qualifier("smartValidator") SmartValidator smartValidator){ 
     this.smartValidator = smartValidator; 
    } 

    @Override 
    public boolean supports(Class<?> aClass) { 
     return RequestWrapper.class.equals(aClass); 
    } 

    @Override 
    public void validate(Object object, Errors errors) { 
     RequestWrapper requestWrapper = (RequestWrapper) object; 
     if(requestWrapper.getRequest() instanceof UserRequest) { 
     UserRequest userRequest = (UserRequest) requestWrapper.getRequest(); 
     smartValidator.validate(userRequest, errors); 
    } 
} 

以下のように私はカスタムバリデータを持っています

}

Wh私はテストを実行する私は以下の例外を取得します。

java.lang.AssertionError 
    at org.junit.Assert.fail(Assert.java:92) 
    at org.junit.Assert.assertTrue(Assert.java:43) 
    at org.junit.Assert.assertTrue(Assert.java:54) 
    at com.user.validator.UserRequestValidatorTest.validateUpdateUserRequest_testFirstName(UserRequestValidatorTest.java:79) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 

ファーストネームの検証でnullエラーが発生するように値を提供しています。 フィールドを検証することができない、正しく書いていないものを理解できません。

この例外は、assertTrue(errors.hasErrors()) で発生しています。次の行であっても、同じエラーで失敗しています。

誰かがこれを見直して助けてくれますか?

おかげ

+0

BeanPropertyBindingResult contrcutorでは何が起こっていますか? –

+0

あなたが意味するものは得られませんでした。私はバリデータに渡すオブジェクトを渡しています。それについて正しくないのは何ですか? –

+0

何が正しいか間違っているか質問していません。 Errorsクラスの内部構造に基づいて宣言していますが、これは投稿されていません。また、これらのアサーションは、テスト中のメソッドが行っていることとはあまり関係していないようです。私は単にsmartValidator.validateメソッドが呼び出されていることを確認するだけです。そこには何もテストする必要はありません –

答えて

1

[OK]をので、まず私が@MockとしてSmartValidatorを作るの冒涜的ミスを犯しました。 これは、私の検証が実際には実行されなかったことを意味します。これは本当に愚かでした。 第2に、SmartValueatorから@mockを削除しても、まだ動作しませんでした。私はSmartValidatorのインスタンスを検証クラスUserRequestValidatorに注入する必要がありました。 したがって、以下の注釈をテストクラスで使用しました。 また、テストクラスのバリデータをAutowiredとしてマークしました。 それが解決しました。

@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@ContextConfiguration(loader = AnnotationConfigContextLoader.class) 
public class UserRequestValidatorTest{ 
    @Autowired 
    UserRequestValidator userRequestValidator; 

    //Remaining things same as in question 

} 
+0

あなたがこれを認めてうれしいです。テストしようとしていることを嘲笑することは、嘲笑するときのよくある間違いです。 – Makoto

関連する問題