2016-08-28 3 views
-1

私が継承したspringbootアプリケーションを単体テストしようとしています。arg.mockito.Mockito.mockは、引数が正しいのに有効ではありません

public class ValidationServiceTest { 

    private Logger LOG = null; 
    private final Long INVALID_ID = -1L; 
    private Long custId = 4; 
    private String username = "foo"; 

    cRepository repo; 
    Customer customer = null; 
    ValidationService = null; 

    @Before 
    public void setUp() { 

     validationService = new ValidationService(); 
     customer = new Customer(); 
     customer.setUsername(username); 
     customer.setCustId(custId); 

     repo = mock(cRepository.class); 
     when(repo.searchByUsername(any(String.class))).thenReturn(customer); 

     MockitoAnnotations.initMocks(this); 
    } 

    @Test 
    public void getCustIdTest() { 
     Long result = .getCustId(username); 
    } 
} 
その後

生産部分で、私はこのコード行を持っている:ここで

は、私はユニットテストのモック一部を初期化する方法である

public Long getCustId(String username) { 

    // Here it breaks 
    Customer customer = repo.searchByUsername(username); 
    return customer.getCustId; 
} 

私が知っている正しい値その(username、password、id)がログに記録され、コードに渡されます。問題は、上記の行がNullPointerExceptionを返すことです。

これは明らかに間違っていますか?私はそれがユーザー名 "foo"の顧客を返すと期待していますが、それはありません。

例外のスタックトレース

com.mypkg.db.ValidationServiceTest.getCustIdTestでcom.mypkg.db.ValidationService.getCustIdでjava.lang.NullPointerExceptionが (ValidationService.java:27) ( sun.reflect.DelegatingMethodAccessorImpl.invokeでsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) でsun.reflect.NativeMethodAccessorImpl.invoke0(ネイティブメソッド) でValidationServiceTest.java:77) (DelegatingMethodAccessorImpl.java:43 ) at java.lang.reflect.Method.invoke(Method.java:498) 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 org.junit.internal.runners.statements.RunBefores.evaluate org.junit.runnersで(RunBefores.java:26)org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)で org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)で .BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner $ 3.run(パーorg.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71) org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) org.junit.runnersで を入力してください。 .ParentRunner.access $ 000(ParentRunner.java:58) at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgsでcom.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgsでorg.junit.runner.JUnitCore.run(JUnitCore.java:137) (JUnit4IdeaTestRunner.java:119) (JUnit4IdeaTestRunner.javaで:42) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)sun.reflect.NativeMethodAccessorImpl.invokeでsun.reflect.NativeMethodAccessorImpl.invoke0(ネイティブメソッド) でcom.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) (NativeMethodAccessorImpl.java:62で) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main (AppMain。Javaの:144)春に

+2

あなたは正しい方法を嘲笑していますか?あなたは 'searchByUsername'メソッドを嘲笑していますが、代わりに' findByUsername'メソッドを呼び出しています。 –

+1

また、 'cRepository'の模擬インスタンス(Javaの命名規則に従わず、あいまいな名前であるBTW)を作成したからといって、プロダクションコードがその模擬インスタンスを使用しているわけではありません。関連するコード全体を投稿してください。 –

+0

@ LukeWoodward、言及のおかげで...私はタイプミスを修正しました。 –

答えて

1

は、あなたのautoWiredリポジトリが与えられ、

cRepo = mock(cRepository.class); 
+0

実際にはorg.springframework.data.jpa.repository.JpaRepositoryです –

1

最も可能性の高い説明で嘲笑することはできませんインターフェース

public interface cRepo extends MongoRepository<X ,String>{ 

であることができることをいくつかの部分のあなたのコードはありません、あなたはcRepositoryを模倣しますが、あなたのValidationServiceにそれを注入しないでください。

あなたが車をテストするためにステアリングホイールを嘲笑しているとしますが、あなたがテストしている車に入れないと、もちろんハンドルにアクセスしようとするとNullpointer例外がスローされますそれだけでどこかに転がって、そこではない、まったくテストを助けない...

通常、あなたは、単にこのような何かを書くことができます...

@RunWith(MockitoJUnitRunner.class) 
public class ValidationServiceTest { 

    @Mock 
    private CRepository cRepository; // the runner, see above, will automatically mock this now 

    @InjectMocks 
    private ValidationService validationService; // The runner will create this and inject all the mocks into it 

    @Test 
    public void myTest() { 
     Customer customer = new Customer(); 
     customer.setUsername("foo"); 
     customer.setCustId(4); 
     when(cRepository.searchByUsername(any(String.class))).thenReturn(customer); 

     Long result = validationService.getCustId(username); 
     assert(result.equals(4)); 
} 

この方法で、Mockitoはなりますモックオブジェクトを自動的に作成してvalidationServiceオブジェクトに注入します。

ああ、あなたのクラスUPPER-CASEを喜ばせます!

+0

意味を理解して、コードを更新しました。 –

+0

あなたのコードのinitMocksは、あなたのテストメンバーのどれかがモックであると宣言していないので、全く効果がありません。また、MockitoJUnitRunnerを使用する場合は、自動的に実行されるため、必要ありません。 –

+0

ええ、モックが使用されていないように見える、私はそれを実現するためにコンストラクタまたはセッターを使用する必要があります –

関連する問題