2017-10-09 20 views
0

現在、私は休憩と組み合わせてMockitoを使って作業しようとしています。私はMockitoと残りの部分で私の編集方法をテストする方法がわかりません。コードの最後のブロックには、私が気づくことのできない部分が含まれています。私はMockitoと仕事をしていないので、すべてのヒントは大歓迎です。mockitoで編集機能をテストする - Java EE - JUnit

これは私がテストしたい機能です:/ /プロフィール/編集:

@POST 
@Path("edit/{id}") 
@Consumes({MediaType.APPLICATION_JSON}) 
@Produces({MediaType.APPLICATION_JSON}) 
public Profile editProfile(Profile profile, @PathParam("id") Long id){ 
    Profile updatedProfile = profileService.getProfile(id); 
    updatedProfile.setBiography(profile.getBiography()); 
    updatedProfile.setLocation(profile.getLocation()); 
    updatedProfile.setWebsite(profile.getWebsite()); 
    updatedProfile.setAvatar(profile.getAvatar()); 
    updatedProfile.setImage(profile.getImage()); 
    updatedProfile.setUpdated_at(new Date()); 
    return updatedProfile; 
} 

は、テストを編集するには、ここ

Client client; 
WebTarget root; 
static final String PATH = "/MyApp/api/profile/"; 
static final String BASEURL = "http://localhost:8080" + PATH; 
List<Profile> profileList = new ArrayList<Profile>(); 
Profile profile; 

@Mock 
ProfileDao profileDao; 

@InjectMocks 
private ProfileService profileService; 

@Before 
public void setUp() { 
    this.client = ClientBuilder.newClient(); 
    this.root = this.client.target(BASEURL); 
    profile = new Profile(1L,"biography", "location" ,"website","../avatar.jpg","../image.jpg"); 
    for(int i = 0; i < 10; i++){ 
     profileList.add(profile); 
    } 
} 

テスト機能を私のTestClassをセットアップ1 -/profile/edit/{id}

// This part doesn't work. I'm not sure how to handle this part with mockito and rest 

@Test 
public void editProfileTest() { 
    String mediaType = MediaType.APPLICATION_JSON;   
    when(profileDao.find(1L)).thenReturn(new Profile(1L,"biography", "location" ,"website", "../avatar.jpg","../image.jpg")); 
    final Entity<Profile> entity = Entity.entity(profileService.getProfile(1L), mediaType); 

    Profile profileResult = this.root.path("edit/1").request().post(entity, Profile.class); 
    assertThat(profileResult, is(profile)); // this doesn't match 
} 
+0

統合テスト(メソッド、サービス、DAO、データベースの組み合わせをテストする場合)を作成しますか?または、メソッドだけをテストしたいですか?メソッドはProfileを読み込み、6つの属性を更新するだけなので、(もし私が統合テストを作成したくなければ)テストします。 –

+0

@ NiklasP質問はメソッドのテストについてです。しかし、私は統合テストにも興味があります。私は方法だけのテストのための解決策を見つけました、私は答えとして以下に投稿します。あなたが統合テストのための参照や解決策を持っているなら、私はそれにも興味があります。 –

+0

JUnitテストの 'is(profile)'部分の問題は、**同じ**(等しくない)とあなたのモックされたメソッドの場合、プロファイルオブジェクトのポインタ/アドレスを比較することです**新しい**プロフィールを作成します。これは、同じではありません**同じ**ではありません。最も簡単な方法は 'when(profileDao.find(1L))。thenReturn(profile);'で定義されたプロファイルを返すことです。 –

答えて

0

最初は、テストをどのように有効にするかわかりませんでしたが、次のコードで問題を解決しました。

少し私のレスト/ Mockitoテスト調整:

@Test 
public void editProfileTest() { 
    String mediaType = MediaType.APPLICATION_JSON;   
    when(profileDao.find(2L)).thenReturn(profileList.get(0)); 
    final Entity<Profile> entity = Entity.entity(profileService.getProfile(2L), mediaType); 
    Profile profileResult = this.root.path("edit/2").request().post(entity, Profile.class); 
    assertThat(profileResult.getId(), is(profileService.getProfile(2L).getId())); 
    assertThat(profileResult.getUser_id(), is(profileService.getProfile(2L).getUser_id())); 
    assertThat(profileResult.getAvatar(), is(profileService.getProfile(2L).getAvatar())); 
    assertThat(profileResult.getBiography(), is(profileService.getProfile(2L).getBiography())); 
    assertThat(profileResult.getLocation(), is(profileService.getProfile(2L).getLocation())); 
    assertThat(profileResult.getWebsite(), is(profileService.getProfile(2L).getWebsite())); 
    assertThat(profileResult.getImage(), is(profileService.getProfile(2L).getImage())); 
} 

これは/ のMyApp /プロフィール/編集/ {ID}のメソッドをテストしています。統合テストの例がある場合、または参考文献に投稿してください。私は一般的なテストについてもっと学びたいと思っています。

+2

'profileService'をテストしたくないなら、' profileService.getProfile(2L) 'メソッドを模擬して、模擬して、私が' profileResult'と比較する定義済みプロファイルを返すようにします。 –

関連する問題