2015-11-05 62 views
1

處理假設我有以下情形:嘲笑ClassUnderTest |中的公共方法調用用的ArrayList

public class ClassUnderTest() 
     { 

     private Service myService; 

     public SomeData method1(final InputData input) { 
      final SomeData result = method2(myservice.getOutput(input)); 
      return result; 
     } 

     public SomeData method2(final OutputData output){ 
      //Do something with the OutputData 
     } 
    } 

現在我想測試方法1()。由於它調用method2()我也需要保證method2()內的所有東西都正常工作。但事情是,因爲我測試了所有的方法,我會單獨測試method2()。

那麼我怎麼知道只測試method1()而不考慮method2()。我很想在method2()被調用時使用doNothing(),但因爲它是我想測試的類而不是模擬的,所以我不能這樣做。或者有可能嗎?

2.)我該如何聲明兩個ArrayList的相等,當它們都應該只有兩個具有相同值的對象時。例如:

@Test 
public void test(){ 
User user1 = new User(); 
User user2 = new User(); 

user1.setMail("mail"); 
user2.setMail("mail"); 

list<User> list1 = new ArrayList<User>(); 
list<User> list2 = new ArrayList<User>(); 

list1.add(user1); 
list2.add(user2); 

assertEquals(list1,list2); 

} 

這會失敗,因爲它們不相等對象。

+2

你在問一個2無關的問題。把它分成兩個單獨的問題會更好。 – Tunaki

+1

如果你正在徹底地測試method2(並且徹底地測試'myservice.getOutput(input)'),而method1只是一個方便的方法來調用method2,那麼我可能只有一個方法來證明它確實有效在一些常見的情況下。爲'method1'可能做的任何驗證添加更多測試(例如'input'非空) –

+0

您是否使用任何類型的模擬框架?它可以在這裏幫助,但是示例代碼讓我知道某處存在設計氣味;爲什麼讓一個班級返回兩個完全無關的項目? – fge

回答

1

你真的應該分開問的問題,但我試着回答這兩個:

1部分嘲弄與的Mockito

我真的不知道它是真正明智的部分嘲笑。但與mockito這是可能的。你可以嘲笑你ClassUnderTest並告訴給的Mockito執行真正的方法時method1被稱爲

ClassUnderTest mock = Mockito.mock(ClassUnderTest.class); 
when(mock.method1()).thenCallRealMethod(); 

查看收藏

http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html#partial_mocks

2.斷言AssertJ爲您提供了收藏很不錯的斷言 - 例如:

List<String> list1 = Arrays.asList("1", "2", "3"); 
List<String> list2 = Arrays.asList("1", "2", "3"); 
then(list1).containsExactlyElementsOf(list2); 

請在這裏查看詳情 https://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#extracted-method-result-assertion

https://github.com/joel-costigliola/assertj-examples/blob/master/assertions-examples/src/test/java/org/assertj/examples/ListSpecificAssertionsExamples.java

+0

謝謝。但問題是,你在列表中使用了字符串。如果我只有字符串,那麼我可以使用assertEquals()。但問題是我有兩個對象,具有相同的值,這是在兩個單獨的列表中。 – user5417542

+0

然後你去正確地實現equals() - 所以你的'User'類實現等於反映你的對象的身份。 –

0

你的問題的第一部分已經被馬蒂亞斯回答。使用PowerMock將是一種選擇。

@RunWith(PowerMockRunner.class) 
@PrepareForTest(ClassUnderTest.class) 
public class YourTest { 

    @Test 
    public void yourTest() { 
     PowerMockito.suppress(MemberMatcher.method(ClassUnderTest.class, "method2")); 
     ClassUnderTest m = mock(ClassUnderTest.class); 
     //method2 will not be called 
     m.method1(); 
    } 
} 

關於名單斷言,您可以在Hamcrest使用強大的samePropertyValuesAs匹配的。

import static org.hamcrest.MatcherAssert.assertThat; 
import static org.hamcrest.Matchers.containsInAnyOrder; 
import static org.hamcrest.Matchers.samePropertyValuesAs; 

@Test 
public void yourTest() throws Exception { 

    .... 

    assertThat(list1, containsInAnyOrder(userCollectionToMatcher(list2))); 
} 

public Collection<Matcher<? super User>> userCollectionToMatcher(Collection<User> users) { 
    return users.stream().map(u -> samePropertyValuesAs(u)).collect(Collectors.toList()); 
}