現在、私は休憩と組み合わせて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
}
統合テスト(メソッド、サービス、DAO、データベースの組み合わせをテストする場合)を作成しますか?または、メソッドだけをテストしたいですか?メソッドはProfileを読み込み、6つの属性を更新するだけなので、(もし私が統合テストを作成したくなければ)テストします。 –
@ NiklasP質問はメソッドのテストについてです。しかし、私は統合テストにも興味があります。私は方法だけのテストのための解決策を見つけました、私は答えとして以下に投稿します。あなたが統合テストのための参照や解決策を持っているなら、私はそれにも興味があります。 –
JUnitテストの 'is(profile)'部分の問題は、**同じ**(等しくない)とあなたのモックされたメソッドの場合、プロファイルオブジェクトのポインタ/アドレスを比較することです**新しい**プロフィールを作成します。これは、同じではありません**同じ**ではありません。最も簡単な方法は 'when(profileDao.find(1L))。thenReturn(profile);'で定義されたプロファイルを返すことです。 –