[英]How to load the properties from application.properties in Mockito test
I am trying to write a unit test case to test the method, but I encounter a problem.我正在尝试编写单元测试用例来测试该方法,但遇到了问题。
here is the sample code:这是示例代码:
MyService1我的服务1
@Service
public class MyService1 {
@Autowired
private ServiceProperties serviceProperties;
public void getMyLanguage(){
String language = serviceProperties.getLocale().getLanguage();
printSomething(language);
}
private void printSomething(String input){
System.out.print("your current language is " + input);
}
}
ServiceProperties服务属性
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Locale;
@ConfigurationProperties(prefix = "conversation")
public class ServiceProperties {
private ServiceProperties(){};
private Locale locale;
public Locale getLocale(){
return locale;
}
}
application.properties应用程序属性
conversation.locale=en_US
Here is my test case:这是我的测试用例:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class MyService1Test {
@Mock
private ServiceProperties serviceProperties;
@InjectMocks
private MyService1 myService1;
@Test
public void getMyLanguage(){
when(serviceProperties.getLocale().getLanguage()).thenReturn("EN");
myService1.getMyLanguage();
verify(myService1).getMyLanguage();
}
}
the test will trigger nullpointerexception, because the properties for locale are not loaded in test, if I don't want to start the server(use @SpringBootTest annotation) to load the context, is there any way to solve this problem, can anyone help?测试会触发nullpointerexception,因为测试中没有加载locale的属性,如果我不想启动服务器(使用@SpringBootTest注解)加载上下文,有没有办法解决这个问题,谁能帮忙?
The problem is at this line:问题出在这一行:
when(serviceProperties.getLocale().getLanguage()).thenReturn("EN");
Because serviceProperties
is mocked, serviceProperties.getLocale()
is equal to null
.因为
serviceProperties
被模拟,所以serviceProperties.getLocale()
等于null
。 So you get NullPointerException
when serviceProperties.getLocale().getLanguage()
is called.所以当
serviceProperties.getLocale().getLanguage()
被调用时你会得到NullPointerException
。
One workaround would be as follows:一种解决方法如下:
@RunWith(MockitoJUnitRunner.class)
public class MyService1Test {
@Mock
private ServiceProperties serviceProperties;
@InjectMocks
private MyService1 myService1;
@Test
public void getMyLanguage(){
when(serviceProperties.getLocale()).thenReturn(new Locale("EN"));
myService1.getMyLanguage();
verify(myService1).getMyLanguage();
}
}
Field injection is not convenient for testing.现场注入不便于测试。 You can use constructor injection
您可以使用构造函数注入
@Service
public class MyService {
private final ServiceProperties serviceProperties;
@Autowired
public MyService(ServiceProperties serviceProperties) {
this.serviceProperties = serviceProperties;
}
//...
}
And then you will be able to inject mocks before each test然后你将能够在每次测试之前注入模拟
@RunWith(MockitoJUnitRunner.class)
public class MyService1Test {
@Mock
private ServiceProperties serviceProperties;
private MyService1 myService1;
@Before
public void createService(){
myService1 = new MyService1(serviceProperties);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.