测试在休息的编辑功能用的Mockito - 的Java EE - JUnit的

问题描述:

我目前正试图与用的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 - /型材/编辑/ {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,数据库)?或者你想只测试该方法?由于该方法只读取配置文件和更新6属性,我会测试(如果我不想创建集成测试)。 –

+0

@NiklasP这个问题只是关于测试方法。不过,我也对集成测试感兴趣。我已经找到了方法测试的解决方案,我将在下面作为答案发布。如果您对集成测试有任何参考或解决方案,我也会对此感兴趣。 –

+0

的问题'是(配置文件)JUnit测试的'的部分是,'is'如果它们是相同** **(等于是不够的)与您的模拟的方法的简档对象的指针/地址比较创建一个**新**配置文件,这意味着平等但不是**相同。最简单的方法是用'when(profileDao.find(1L))。thenReturn(profile);'返回定义的配置文件。 –

起初,我真的不知道如何使测试有效,但与下面的一段代码,我解决我的问题。

调整好自己的休息/测试的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'。 –