This question already has an answer here:
#Autowired objects are null only when testing
(1 answer)
Closed 3 years ago.
I have following implementation which uses RestTemplate to make request to external service:
#Component
public class ExternalProvider {
private ProviderProperties providerProperties;
#Autowired
#Qualifier("exprd")
private RestTemplate restTemplate;
#Bean
#Qualifier("exprd")
public RestTemplate restTemplate() {
return new RestTemplate();
}
#Autowired
public ExternalProvider(ProviderProperties providerProperties) {
this.providerProperties = providerProperties;
}
public String request(String requestParams) {
...
return restTemplate.getForObject(providerProperties.getUrl(), String.class);
}
}
And here is the test:
#ContextConfiguration(classes = {ExternalProviderTest.TestConfig.class})
#RunWith(SpringJUnit4ClassRunner.class)
public class ExternalProviderTest {
private ExternalProvider externalProvider;
private ProviderProperties providerProperties;
#TestConfiguration
static class TestConfig {
#Bean
#Qualifier("exprd")
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
#Before
public void setUp() {
providerProperties = new ProviderProperties();
externalProvider = new ExternalProvider(providerProperties);
}
#Test
public void test_makeRequest() {
assertNotNull(externalProvider.request("params"));
}
}
My test above is not running because of a NullPointerException when restTemplate is null. It seems that the TestConfig I define in my test is ignored. Anyone has an idea what did I configure wrong here? Thank you!
You have to use the Spring injection in your test, don't use new (or don't use Spring at all!)
You also have to choose between constructor injection and autowired fields, avoid mixing it.
In your example I just removed the #before/new, and added #Autowired for Spring injection.
#ContextConfiguration(classes = {ExternalProviderTest.TestConfig.class})
#RunWith(SpringJUnit4ClassRunner.class)
public class ExternalProviderTest {
#Autowired
private ExternalProvider externalProvider;
#Autowired
private ProviderProperties providerProperties;
#TestConfiguration
static class TestConfig {
#Bean
#Qualifier("exprd")
public RestTemplate restTemplate() {
return new RestTemplate();
}
#Bean
public ExternalProvider externalProvider () {
return new ExternalProvider (providerProperties());
}
#Bean
public ProviderProperties providerProperties() {
return new ProviderProperties();
}
}
#Test
public void test_makeRequest() {
assertNotNull(externalProvider.request("params"));
}
}
edit:
also it seems that your Bean weren't configured, Id did it here in the test, make me know if there are other configuration errors.
Related
I created a #SpringBootTest-annotated class to test the HTTP responses of a REST endpoint.
I compare each HTTP JSON response with the content of a Resource.
#AutoConfigureMockMvc
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SampleApplicationTests {
#Autowired
private MockMvc mockMvc;
#Value("classpath:data/result1.json")
private Resource result1;
#Value("classpath:data/result2.json")
private Resource result2;
#Test
public void test1() {
mockMvc.perform(MockMvcRequestBuilders.
get("/api/persons")).
andExpect(MockMvcResutMatchers.status().isOk()).
andExpect(MockMvcResutMatchers.content().json(asString(result1)));
}
#Test
public void test2() {
mockMvc.perform(MockMvcRequestBuilders.
get("/api/persons/1")).
andExpect(MockMvcResutMatchers.status().isOk()).
andExpect(MockMvcResutMatchers.content().json(asString(result2)));
}
...
private static String asString(Resource resource) {
try {
return IOUtils.toString(resource.getInputStream());
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
}
I would like to know if there is a more elegant way to inject resources at the level of each test.
I know I can use ResourceLoader to load resources for each test:
#AutoConfigureMockMvc
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SampleApplicationTests {
#Autowired
private ResourceLoader resourceLoader = null;
#Test
public void test1() {
String result = asString(resourceLoader.getResource("classpath:data/result1.json");
mockMvc.perform(MockMvcRequestBuilders.
get("/api/persons")).
andExpect(MockMvcResutMatchers.status().isOk()).
andExpect(MockMvcResutMatchers.content().json(result));
}
...
}
But I would like to know if there is an Annotation that allows to load a resource for each test.
Something like the following:
#AutoConfigureMockMvc
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SampleApplicationTests {
#Test
#TestResource("classpath:data/result1.json")
public void test1(Resource result) {
mockMvc.perform(MockMvcRequestBuilders.
get("/api/persons")).
andExpect(MockMvcResutMatchers.status().isOk()).
andExpect(MockMvcResutMatchers.content().json(result));
}
...
}
Does anyone have any suggestions?
SpringBootTest is annotated with #SpringExtension which resolves arguments for method parameters as well:
#Test
public void test1(#Value("classpath:data/result1.json") Resource result1) {
// ...
}
#Test
public void test2(#Value("classpath:data/result2.json") Resource result2) {
// ...
}
I have a class called SomeBean and two tests that are configured with Stubs for different scenarios. I am using Spring Boot.
The second test is supposed to pass without Exception because there is no stubbing that I did to throw Exception.
The DirtiesContext is not working as well. If I remove the commented code in Test2.java I get the test to pass. I would like to remove the unnecessary subbing by using something similar to DirtiesContext.
I may be missing something basic. Can someone point to what I am doing incorrect.
#Service
public class SomeBeanProcessor {
#Autowired
private BeanValidator beanValidator;
public ResultBean process(SomeBean sb) throws ValidationException{
beanValidator.validateBean(sb);
//Do some processing and return ResultBean;
}
}
Test1.java
RunWith(SpringRunner.class)
#SpringBootTest(classes = {MyApp.class})
#WebAppConfiguration
#ContextConfiguration(classes=Test1.Test1Config.class) {
public class Test1 {
#Configuration
static class Test1Config {
#Bean
public BeanValidator getSomeRequestValidator() {
return new BeanValidator() {
public void validateBean(SomeBean bean) throws ValidationException {
throw new ValidationException("Validation failed");
}
};
}
}
#Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
#Autowired
private SomeBeanProcessor aBeanProcessor;
#Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
#Test
#DirtiesContext
public void doTestValidationErrors() throws ValidationException{
SomeBean sb = new SomeBean();
this.aBeanProcessor.process(sb);
Assert.fail("Should throw exception");
}
}
Test2.java
RunWith(SpringRunner.class)
#SpringBootTest(classes = {MyApp.class})
#WebAppConfiguration
#ContextConfiguration(classes=Test2.Test2Config.class) {
public class Test2 {
#Configuration
static class Test2Config {
//#Bean
//public BeanValidator getSomeRequestValidator() {
// return new BeanValidator() {
// public void validateBean(SomeBean bean) throws ValidationException { //Do nothing
// }
// };
//}
}
#Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
#Autowired
private SomeBeanProcessor aBeanProcessor;
#Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
#Test
#DirtiesContext
public void doTestSuccess() throws ValidationException{
SomeBean sb = new SomeBean();
this.aBeanProcessor.process(sb);
}
}
I have a controller
#RestController
public class Create {
#Autowired
private ComponentThatDoesSomething something;
#RequestMapping("/greeting")
public String call() {
something.updateCounter();
return "Hello World " + something.getCounter();
}
}
I have a component for that controller
#Component
public class ComponentThatDoesSomething {
private int counter = 0;
public void updateCounter () {
counter++;
}
public int getCounter() {
return counter;
}
}
I also have a test for my controller.
#RunWith(SpringRunner.class)
#SpringBootTest
public class ForumsApplicationTests {
#Test
public void contextLoads() {
Create subject = new Create();
subject.call();
subject.call();
assertEquals(subject.call(), "Hello World 2");
}
}
The test fails when the controller calls something.updateCounter(). I get a NullPointerException. While I understand it's possible to add #Autowired to a constructor I would like to know if there is anyway to do this with an #Autowired field. How do I make sure the #Autowired field annotation works in my test?
Spring doesn't auto wire your component cause you instantiate your Controller with new not with Spring, so Component is not instatntiated
The SpringMockMvc test check it correct:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest
public class CreateTest {
#Autowired
private WebApplicationContext context;
private MockMvc mvc;
#Before
public void setup() {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.build();
}
#Test
public void testCall() throws Exception {
//increment first time
this.mvc.perform(get("/greeting"))
.andExpect(status().isOk());
//increment secont time and get response to check
String contentAsString = this.mvc.perform(get("/greeting"))
.andExpect(status().isOk()).andReturn()
.getResponse().getContentAsString();
assertEquals("Hello World 2", contentAsString);
}
}
The #Autowired class can be easily mocked and tested with MockitoJUnitRunner with the correct annotations.
With this you can do whatever you need to do with the mock object for the unit test.
Here is a quick example that will test the Create method call with mocked data from ComponentThatDoesSomething.
#RunWith(MockitoJUnitRunner.class)
public class CreateTest {
#InjectMocks
Create create;
#Mock
ComponentThatDoesSomething componentThatDoesSomething;
#Test
public void testCallWithCounterOf4() {
when(componentThatDoesSomething.getCounter()).thenReturn(4);
String result = create.call();
assertEquals("Hello World 4", result);
}
}
Use Mockito and inject a mock that you create. I would prefer constructor injection:
#RestController
public class Create {
private ComponentThatDoesSomething something;
#Autowired
public Create(ComponentThatDoesSomething c) {
this.something = c;
}
}
Don't use Spring in your Junit tests.
public CreateTest {
private Create create;
#Before
public void setUp() {
ComponentThatDoesSomething c = Mockito.mock(ComponentThatDoesSomething .class);
this.create = new Create(c);
}
}
I am trying to Unit test a simple Spring-MVC-Controller but even the simplest Unit Test fails because a #ModelAttribute throws a NullpointerException. My Question is: How to mock/set the ModelAttribute?
I've tried mocking the findAll() method from the repository but it fails.
Below are my used classes:
TestContext:
#Configuration
public class TestContext {
#Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("i18n/messages");
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
#Bean
public BenutzerRepository benutzerRepository() {
return Mockito.mock(BenutzerRepository.class);
}
}
StandaloneBenutzerController:
#RunWith(MockitoJUnitRunner.class)
#SpringApplicationConfiguration(TestContext.class)
public class StandaloneBenutzerControllerTest {
public MockMvc mockMvc;
#Mock
private BenutzerRepository benutzerRepositoryMock;
#Mock
private Benutzer benutzer;
#Before
public void setUp() {
this.benutzerRepositoryMock = Mockito.mock(BenutzerRepository.class);
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
Benutzer hauke = new Benutzer("test","tester", "test#tester.de");
List<Benutzer> mockList = new ArrayList<Benutzer>();
mockList.add(hauke);
mockMvc = MockMvcBuilders.standaloneSetup(new BenutzerController()).setViewResolvers(viewResolver)
.build();
}
#Test
public void testSimpleStatus() throws Exception {
Mockito.when(benutzer.getEmail()).thenReturn("test#tester.de");
//Mockito.when(benutzerRepositoryMock.findAll()).thenCallRealMethod();
//Mockito.when(benutzerRepositoryMock.findAll()).thenReturn(userList);
this.mockMvc.perform(get("/verwaltung"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/WEB-INF/jsp/verwaltung.jsp"));
}
}
BenutzerController: the Part which throws the Nullpointer
#ModelAttribute("userList")
public List<Benutzer> userList() {
return toList(repository.findAll());
}
The solution is like Si mo hinted to set the mock repository for the tested BenutzerController via the constructor:
mockMvc = MockMvcBuilders.standaloneSetup( new BenutzerController(benutzerRepositoryMock)).setViewResolvers(viewResolver).build();
I have a little problem. I think this is typical question. However, I can't find good example. My application is using Jersey. And I want to test controller by client as test. Controller has private field - StudentService. When I debug test I see, that field is null. This leads to error. And I need to inject this field. I tried this:
My Controller
#Path("/student")
#Component
public class StudentResourse {
#Autowired
private StrudentService service; // this field Spring does not set
#Path("/getStudent/{id}")
#GET
#Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Student getStudent(#PathParam("id") long id) {
return service.get(id);
}
}
My JUnit test class:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = "classpath:config.xml")
#TestExecutionListeners({ DbUnitTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class })
public class StudentResourseTest extends JerseyTest {
private static final String PACKAGE_NAME = "com.example.servlet";
private static final String FILE_DATASET = "/data.xml";
#Autowired
private StudentService service; // this field is setted by Spring, but I do not need this field for test
public StudentResourseTest() {
super(new WebAppDescriptor.Builder(PACKAGE_NAME).build());
}
#Override
protected TestContainerFactory getTestContainerFactory() {
return new HTTPContainerFactory();
}
#Override
protected AppDescriptor configure() {
return new WebAppDescriptor.Builder("restful.server.resource")
.contextParam("contextConfigLocation",
"classpath:/config.xml").contextPath("/")
.servletClass(SpringServlet.class)
.contextListenerClass(ContextLoaderListener.class)
.requestListenerClass(RequestContextListener.class).build();
}
#Test
#DatabaseSetup(FILE_DATASET)
public void test() throws UnsupportedEncodingException {
ClientResponse response = resource().path("student").path("getStudent")
.path("100500").accept(MediaType.APPLICATION_XML)
.get(ClientResponse.class);
Student student = (Student) response.getEntity(Student.class);
} }
I guees, that problem is in test class. Because, when I run my application not in test, I can directly request students and everything working fine. But when I test classes, internal field of Controller does not setted. How to fix this bug? Thanks for your answers.
This is in my config.xml
<context:component-scan base-package="com.example" />
<bean id="StudentResourse" class="com.example.servlet.StudentResourse">
<property name="service" ref="studentService" />
</bean>
<bean id="service" class="com.example.service.StudentServiceImpl" />
One issue may be that you're trying to configure your test application in constructor and in configure() method. Use one or another but not both because in this case your configure() method is not invoked and hence you may not be using SpringServlet and everything that is defined in this method.
Reference: https://github.com/jiunjiunma/spring-jersey-test and http://geek.riffpie.com/unit-testing-restful-jersey-services-glued-together-with-spring/
Idea is to get a hold of the application context inside jersey by using ApplicationContextAware interface. There after we can grab the exact bean already created by spring, in your case, StudentService. Below example shows a mocked version of the dependency, SampleService, used to test the resource layer apis.
Resource class delegating the processing to a service layer
#Component
#Path("/sample")
public class SampleResource {
#Autowired
private SampleService sampleService;
#GET
#Produces(MediaType.APPLICATION_JSON)
#Path ("/{id}")
public Sample getSample(#PathParam("id") int id) {
Sample sample = sampleService.getSample(id);
if (sample == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return sample;
}
}
Service layer encapsulating business logic
#Service
public class SampleService {
private static final Map<Integer, Sample> samples = new HashMap<>();
static {
samples.put(1, new Sample(1, "sample1"));
samples.put(2, new Sample(2, "sample2"));
}
public Sample getSample(int id) {
return samples.get(id);
}
}
Unit test for the above resource
public class SampleResourceTest extends SpringContextAwareJerseyTest {
private SampleService mockSampleService;
// create mock object for our test
#Bean
static public SampleService sampleService() {
return Mockito.mock(SampleService.class);
}
/**
* Create our own resource here so only the test resource is loaded. If
* we use #ComponentScan, the whole package will be scanned and more
* resources may be loaded (which is usually NOT what we want in a test).
*/
#Bean
static public SampleResource sampleResource() {
return new SampleResource();
}
// get the mock objects from the internal servlet context, because
// the app context may get recreated for each test so we have to set
// it before each run
#Before
public void setupMocks() {
mockSampleService = getContext().getBean(SampleService.class);
}
#Test
public void testMock() {
Assert.assertNotNull(mockSampleService);
}
#Test
public void testGetSample() {
// see how the mock object hijack the sample service, now id 3 is valid
Sample sample3 = new Sample(3, "sample3");
Mockito.when(mockSampleService.getSample(3)).thenReturn(sample3);
expect().statusCode(200).get(SERVLET_PATH + "/sample/3");
String jsonStr = get(SERVLET_PATH + "/sample/3").asString();
Assert.assertNotNull(jsonStr);
}
}
SpringContextAwareJerseyTest
#Configuration
public class SpringContextAwareJerseyTest extends JerseyTest {
protected static String SERVLET_PATH = "/api";
final private static ThreadLocal<ApplicationContext> context =
new ThreadLocal<>();
protected String getResourceLocation() {
return "example.rest";
}
protected String getContextConfigLocation() {
return getClass().getName();
}
static private String getContextHolderConfigLocation() {
return SpringContextAwareJerseyTest.class.getName();
}
protected WebAppDescriptor configure() {
String contextConfigLocation = getContextConfigLocation() + " " +
getContextHolderConfigLocation();
Map<String, String> initParams = new HashMap<>();
initParams.put("com.sun.jersey.config.property.packages",
getResourceLocation());
initParams.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
return new WebAppDescriptor.Builder(initParams)
.servletClass(SpringServlet.class)
.contextParam(
"contextClass",
"org.springframework.web.context.support.AnnotationConfigWebApplicationContext")
.contextParam("contextConfigLocation", contextConfigLocation)
.servletPath(SERVLET_PATH) // if not specified, it set to root resource
.contextListenerClass(ContextLoaderListener.class)
.requestListenerClass(RequestContextListener.class)
.build();
}
protected final ApplicationContext getContext() {
return context.get();
}
#Bean
public static ContextHolder contextHolder() {
return new ContextHolder();
}
private static class ContextHolder implements ApplicationContextAware {
#Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
context.set(applicationContext);
}
}
}
Using the above with jersey 1.8