英文:
How to mock class with @ConfigurationProperties in Spring Boot
问题
我有一个类,使用 @ConfigurationProperties 自动装配另一个类。
### 使用 @ConfigurationProperties 的类
@ConfigurationProperties(prefix = "report")
public class SomeProperties {
private String property1;
private String property2;
...
### 自动装配上述 SomeProperties 类的类
@Service
@Transactional
public class SomeService {
....
@Autowired
private SomeProperties someProperties;
.... // 还有其他内容
现在,我想测试 **SomeService** 类,在我的测试类中,当我模拟 **SomeProperties** 类时,所有属性的值都为 `null`。
### 测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SomeProperties.class)
@ActiveProfiles("test")
@EnableConfigurationProperties
public class SomeServiceTest {
@InjectMocks
private SomeService someService;
@Mock // 我尝试过 @MockBean,但没有成功
private SomeProperties someProperties;
如何在模拟拥有来自 `application-test.properties` 文件的属性的 **SomeProperties** 呢?
英文:
I have a class that Autowires another class with @ConfigurationProperties.
Class with @ConfigurationProperties
@ConfigurationProperties(prefix = "report")
public class SomeProperties {
private String property1;
private String property2;
...
Class that Autowires above class SomeProperties
@Service
@Transactional
public class SomeService {
....
@Autowired
private SomeProperties someProperties;
.... // There are other things
Now, I want to test SomeService class and in my test class when I mock SomeProperties class, I am getting null
value for all the properties.
Test class
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SomeProperties.class)
@ActiveProfiles("test")
@EnableConfigurationProperties
public class SomeServiceTest {
@InjectMocks
private SomeService someService;
@Mock // I tried @MockBean as well, it did not work
private SomeProperties someProperties;
How can I mock SomeProperties having properties from application-test.properties
file.
答案1
得分: 3
你需要在SomeServiceTest.java的@Test/@Before方法中存根化所有属性值,如下:
ReflectionTestUtils.setField(someProperties, "property1", "value1");
ReflectionTestUtils.setField(object, name, value);
您还可以通过Mockito.when()来模拟依赖类的响应。
英文:
You need to stub all the property values in @Test/@Before methods of SomeServiceTest.java like :
ReflectionTestUtils.setField(someProperties, "property1", "value1");
ReflectionTestUtils.setField(object, name, value);
You can also mock the reponse for dependent classes via Mockito.when()
答案2
得分: 3
以下是您要翻译的内容:
如果您打算从属性文件绑定值,那么您不应该对 SomeProperties 进行模拟。在这种情况下,会提供 SomeProperties 的实际实例。
模拟示例:
@RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest {
@InjectMocks
private SomeService someService;
@Mock
private SomeProperties someProperties;
@Test
public void foo() {
// 每当在 someService 中调用 someProperties 的方法/属性时,您需要提供返回行为
when(someProperties.getProperty1()).thenReturn(...);
}
}
非模拟示例(someProperties
是一个实际对象,其值从某个属性源绑定):
@RunWith(SpringRunner.class)
@EnableConfigurationProperties(SomeConfig.class)
@TestPropertySource("classpath:application-test.properties")
public class SomeServiceTest {
private SomeService someService;
@Autowired
private SomeProperties someProperties;
@Before
public void setup() {
someService = new SomeService(someProperties); // 构造函数注入
}
...
}
英文:
You are not mocking SomeProperties if you intend to bind values from a properties file, in which case an actual instance of SomeProperties would be provided.
Mock:
@RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest {
@InjectMocks
private SomeService someService;
@Mock
private SomeProperties someProperties;
@Test
public void foo() {
// you need to provide a return behavior whenever someProperties methods/props are invoked in someService
when(someProperties.getProperty1()).thenReturn(...)
}
No Mock (someProperties
is an real object that binds its values from some propertysource):
@RunWith(SpringRunner.class)
@EnableConfigurationProperties(SomeConfig.class)
@TestPropertySource("classpath:application-test.properties")
public class SomeServiceTest {
private SomeService someService;
@Autowired
private SomeProperties someProperties;
@Before
public void setup() {
someService = new someService(someProperties); // Constructor Injection
}
...
答案3
得分: 1
如果您正在使用@Mock ,您还需要对值进行存根处理。默认情况下,所有属性的值都将为null。
英文:
If you are using @Mock , you need to stub the values as well. By default the value will be null for all the properties.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论