情況和問題:在Spring Boot中,如何將一個或多個模擬類/ bean注入應用程序以進行集成測試?在StackOverflow上有幾個答案,但是他們專注於Spring Boot 1.4之前的情況,或者只是不適合我。使用模擬服務/組件進行Spring Boot集成測試
背景是,在下面的代碼中,設置的實現依賴於第三方服務器和其他外部系統。 Settings的功能已經在單元測試中進行過測試,因此對於完整的集成測試,我想模擬對這些服務器或系統的依賴關係,並提供虛擬值。
MockBean將忽略所有現有的bean定義並提供一個虛擬對象,但該對象在其他注入此類的類中不提供方法行爲。在測試之前使用@Before方法設置行爲不會影響注入的對象,或者不會注入其他應用程序服務(如AuthenticationService)。
我的問題:如何將我的bean注入應用程序上下文? 我的測試:
package ch.swaechter.testapp;
import ch.swaechter.testapp.utils.settings.Settings;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.junit4.SpringRunner;
@TestConfiguration
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyApplication.class})
public class MyApplicationTests {
@MockBean
private Settings settings;
@Before
public void before() {
Mockito.when(settings.getApplicationSecret()).thenReturn("Application Secret");
}
@Test
public void contextLoads() {
String applicationsecret = settings.getApplicationSecret();
System.out.println("Application secret: " + applicationsecret);
}
}
而且吼叫應該用嘲笑的類,但沒有收到此嘲笑類服務:
package ch.swaechter.testapp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AuthenticationServiceImpl implements AuthenticationService {
private final Settings settings;
@Autowired
public AuthenticationServiceImpl(Settings settings) {
this.settings = settings;
}
@Override
public boolean loginUser(String token) {
// Use the application secret to check the token signature
// But here settings.getApplicationSecret() will return null (Instead of Application Secret as specified in the mock)!
return false;
}
}
如果您需要調用bean然後用'@ testconfiguration'註釋類並創建模擬服務,那麼將'@ Before'的方法移動到'@ Before'的方法中,因爲Spring測試不會調用'@ Bean',謝謝您的支持 – rajadilipkolli
回答!這將適用於contextLoads測試中的設置對象(值爲「應用程序密鑰」)。但是對於自動裝配設置的應用程序中的所有其他組件,將使用缺少方法定義的默認模擬對象。任何想法也注入嘲弄的對象? – swaechter