Java/Spring:initmock是一个从yml文件中获取@Value的类
我在服务类上编写单元测试,如下所示:Java/Spring:initmock是一个从yml文件中获取@Value的类,java,spring,mockito,Java,Spring,Mockito,我在服务类上编写单元测试,如下所示: @Component @Profile({"default", "dev"}) public class MyService { @Value("${my.property}") private String property; private OtherService otherService; public void MyMethod() { String myVar = otherService.me
@Component
@Profile({"default", "dev"})
public class MyService {
@Value("${my.property}")
private String property;
private OtherService otherService;
public void MyMethod() {
String myVar = otherService.method();
return String.format("MyVar %s & MyProperty %s", myVar, property);
}
}
我当前用于此测试的测试类如下所示:
@ActiveProfiles("dev")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MyApplication.class)
public class MyServiceTest {
@Mock
private OtherService otherService;
@InjectMocks
private MyService myService;
@BeforeEach()
public void init() {
MockitoAnnotations.initMocks(this);
when(otherService.method()).thenReturn("a string");
}
@Test
public void shouldReturnException() {
final Exception exception = assertThrows(ApiErrorException.class,
() -> myService.myMethod(var));
assertThat(exception).hasMessage("here the message");
verify(otherService, never()).method();
}
}
在这两个类中,我有一个application.yml
和application-dev.yml
来设置my.property
。
我想在测试执行期间从应用程序开发文件中获取属性。
但是,对于@InjectMocks
,属性为null。然而,使用@Autowired
代替/with@InjectMocks
,属性变量设置为文件中的值
问题,使用Autowired with/in-place代替InjectMock会导致初始化otherService
变量,因此不会创建mock
如何在使用文件中的值设置属性变量的同时仍然使用Mockito?
我看到了关于ReflectionTestUtils.setField的内容,但使用它意味着不使用yml文件(我不喜欢)
祝你今天愉快
在@Deadpool的帮助下,测试可以使用
application.yml
文件中写入的值。
但是,使用@MockBean
和@Autowired
,测试会得到我不理解的行为
例如:
我测试一个方法是否返回异常,并验证捕获异常后是否未调用其他方法:verify(otherService,never()).otherMethod()代码>
写入此行将返回以下错误org.mockito.exceptions.verification.neverwantedbutinovoked
初始异常被正确捕获,但测试似乎没有确认必须调用其他服务 我建议您更改MyService
类,通过构造函数或setter接受OtherService
。大概是这样的:
@ActiveProfiles("dev")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MyApplication.class)
public class MyServiceTest {
@Mock
private OtherService otherService;
@InjectMocks
private MyService myService;
@BeforeEach()
public void init() {
MockitoAnnotations.initMocks(this);
when(otherService.method()).thenReturn("a string");
}
@Test
public void shouldReturnException() {
final Exception exception = assertThrows(ApiErrorException.class,
() -> myService.myMethod(var));
assertThat(exception).hasMessage("here the message");
verify(otherService, never()).method();
}
}
@组件
@配置文件({“默认”、“开发”})
公共类MyService{
@值(“${my.property}”)
私有财产;
私人其他服务;
公共MyService(其他服务其他服务){
this.otherService=otherService
}
公共方法(){
字符串myVar=otherService.method();
返回String.format(“MyVar%s和MyProperty%s”,MyVar,property);
}
}
然后你就这样做你的测试:
@ActiveProfiles("dev")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MyApplication.class)
public class MyServiceTest {
@Mock
private OtherService otherService;
@InjectMocks
private MyService myService;
@BeforeEach()
public void init() {
MockitoAnnotations.initMocks(this);
when(otherService.method()).thenReturn("a string");
}
@Test
public void shouldReturnException() {
final Exception exception = assertThrows(ApiErrorException.class,
() -> myService.myMethod(var));
assertThat(exception).hasMessage("here the message");
verify(otherService, never()).method();
}
}
@ActiveProfiles(“dev”)
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(类=MyApplication.class)
公共类MyServiceTest{
@嘲弄
私人其他服务;
@注射模拟
@自动连线
私人MyService-MyService;
@beforeach()
公共void init(){
initMocks(this);
when(otherService.method())。然后返回(“字符串”);
}
}
用于加载将用于测试环境的应用程序上下文的集成测试
当我们需要引导整个容器时,可以使用@SpringBootTest注释。注释通过创建将在测试中使用的ApplicationContext来工作
由于您使用@springbootest
生成集成环境,因此需要使用n注释模拟bean
@ActiveProfiles("dev")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MyApplication.class)
public class MyServiceTest {
@MockBean
private OtherService otherService;
@Autowire
private MyService myService;
@BeforeEach
public void init() {
when(otherService.method()).thenReturn("a string");
}
}
谢谢,它工作得非常好^^使用application.yml
中的值进行的测试工作正常,但另一个测试失败,因为使用了verify(repository).save(any(MyClass.class))代码>。我正在寻找修复方法,如果没有看到代码和正确的错误消息,我无法判断,你可以用适当的信息问另一个问题@BaerrowI举个例子