2015-05-28 48 views
1

我是jmockit的新手,並且想要在基於Java的Spring應用程序配置中模擬一個bean。我想(希望更好),它會是這樣的:如何創建一個mocked(由jmockit)spring bean?

@Configuration 
public class MyApplicationConfig { 

    @Bean // this bean should be a mock 
    SomeService getSomeService() { 
    return new MockUp<SomeService>() {@Mock String someMethod() { return ""; }}.getMockInstance(); 
    } 

    @Bean // some other bean that depends on the mocked service bean 
    MyApplication getMyApplication(SomeService someService) { 
    .... 
    } 
} 

但不幸的是這種失敗,「申請一個模擬式無效的地方」。

我不知道我是否可以在Spring Configuration類中生成jmockit mocks。我需要這個bean,因爲它被其他bean引用,並且如果我不提供作爲Spring bean的模擬器,整個Spring上下文初始化失敗。

感謝您的任何幫助。

回答

2

只需使用您的常規Spring配置。在測試類中,聲明要用@Capturing模擬的類型。它會嘲笑Spring使用的任何實現類。

編輯:在下面添加完整的示例代碼。

import javax.inject.*; 

public final class MyApplication { 
    private final String name; 
    @Inject private SomeService someService; 

    public MyApplication(String name) { this.name = name; } 

    public String doSomething() { 
     String something = someService.doSomething(); 
     return name + ' ' + something; 
    } 
} 

public final class SomeService { 
    public String getName() { return null; } 
    public String doSomething() { throw new RuntimeException(); } 
} 

import org.springframework.context.annotation.*; 

@Configuration 
public class MyRealApplicationConfig { 
    @Bean 
    SomeService getSomeService() { return new SomeService(); } 

    @Bean 
    MyApplication getMyApplication(SomeService someService) { 
     String someName = someService.getName(); 
     return new MyApplication(someName); 
    } 
} 

import javax.inject.*; 
import org.junit.*; 
import org.junit.runner.*; 
import static org.junit.Assert.*; 
import mockit.*; 
import org.springframework.test.context.*; 
import org.springframework.test.context.junit4.*; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = MyRealApplicationConfig.class) 
public final class MyApplicationSpringTest { 
    @Inject MyApplication myApplication; 
    @Mocked SomeService mockService; 

    @BeforeClass // runs before Spring configuration 
    public static void setUpMocksForSpringConfiguration() { 
     new MockUp<SomeService>() { 
      @Mock String getName() { return "one"; } 
     }; 
    } 

    @Test 
    public void doSomethingUsingMockedService() { 
     new Expectations() {{ mockService.doSomething(); result = "two"; }}; 

     String result = myApplication.doSomething(); 

     assertEquals("one two", result); 
    } 
} 

import org.junit.*; 
import static org.junit.Assert.*; 
import mockit.*; 

// A simpler version of the test; no Spring. 
public final class MyApplicationTest { 
    @Tested MyApplication myApplication; 
    @Injectable String name = "one"; 
    @Injectable SomeService mockService; 

    @Test 
    public void doSomethingUsingMockedService() { 
     new Expectations() {{ mockService.doSomething(); result = "two"; }}; 

     String result = myApplication.doSomething(); 

     assertEquals("one two", result); 
    } 
} 
+0

謝謝 - 將調查此方法。 – FrVaBe

+0

不幸的是,如果在Spring上下文初始化期間需要模擬實例,則這不起作用。問題是:如何在測試之外生成一個具有已定義行爲的jmockit模擬實例? – FrVaBe

+0

模擬API僅用於* inside *測試。但是我不清楚實際問題是什麼。在上下文初始化期間Spring *調用bean的方法嗎? –

2

Spring-ReInject旨在用嘲笑取代豆。

+0

值得關注,但在我的情況下,有一個專門的彈簧試驗Configurat離子並且可以產生模擬(不需要重新注入某些東西)。如果可能的話,我會優先考慮這一嘗試,但如果沒有,我可能會更深入地瞭解你的圖書館。感謝和+1的時刻! – FrVaBe