#RunWith(MockitoJUnitRunner.class) mocks are null - java

I am using the "RunWith(MockitoJUnitRunner.class)", the #Mock notationt to mock the service and the #InjectMocks notation to inject the mock service to the controller. I get a NullPointerException in the ChargingStationsControllerTest:40, in the "when". I debugged and realized that the mocks are null.
Here is my code:
package com.example.assignmentchargingstations.controllers;
import com.example.assignment.chargingStations.controllers.ChargingStationsController;
import com.example.assignment.chargingStations.models.AddressInfo;
import com.example.assignment.chargingStations.models.ChargingStation;
import com.example.assignment.chargingStations.models.StatusType;
import com.example.assignment.chargingStations.services.OpenChargeMapService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.mockito.Mockito.when;
#RunWith(MockitoJUnitRunner.class)
public class ChargingStationsControllerTest {
#Mock
private OpenChargeMapService openChargeMapService;
#InjectMocks
private ChargingStationsController chargingStationsController;
#Test
public void testGetNearestChargingStations() throws Exception {
Double latitude = 90.0;
Double longitude = 90.0;
List<ChargingStation> chargingStationsListExpected = new ArrayList<>();
StatusType statusTypeExpected = StatusType.builder().IsOperational(true).build();
AddressInfo addressInfoExpected = AddressInfo.builder().Latitude(latitude).Longitude(longitude).Title("Test Charging Station").build();
chargingStationsListExpected.add(ChargingStation.builder().StatusType(statusTypeExpected).AddressInfo(addressInfoExpected).build());
when(openChargeMapService.getNearestChargingStations(anyDouble(), anyDouble())).thenReturn(chargingStationsListExpected);
ResponseEntity<List<ChargingStation>> responseExpected = chargingStationsController.getNearestChargingStations(latitude, longitude);
Assertions.assertEquals(responseExpected.getStatusCode(), HttpStatus.OK);
Assertions.assertEquals(responseExpected.getBody(), chargingStationsListExpected);
}
}
The controller that I'm testing:
package com.example.assignment.chargingStations.controllers;
import com.example.assignment.chargingStations.models.ChargingStation;
import com.example.assignment.chargingStations.services.OpenChargeMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
#Controller
public class ChargingStationsController {
private final OpenChargeMapService openChargeMapService;
public ChargingStationsController(OpenChargeMapService openChargeMapService) {
this.openChargeMapService = openChargeMapService;
}
#RequestMapping(path = "/nearest-charging-stations", method = RequestMethod.GET)
public ResponseEntity<List<ChargingStation>> getNearestChargingStations(#RequestParam Double latitude, #RequestParam Double longitude) {
List<ChargingStation> nearestChargingStations = openChargeMapService.getNearestChargingStations(latitude, longitude);
return new ResponseEntity<>(nearestChargingStations, HttpStatus.OK);
}
}
The service:
package com.example.assignment.chargingStations.services;
import com.example.assignment.chargingStations.clients.OpenChargeMapClient;
import com.example.assignment.chargingStations.models.ChargingStation;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
#Service
public class OpenChargeMapService {
private static final Double MAX_LATITUDE = 90.0000000;
private static final Double MIN_LATITUDE = -90.0000000;
private static final Double MAX_LONGITUDE = 180.0000000;
private static final Double MIN_LONGITUDE = -180.0000000;
private final OpenChargeMapClient openChargeMapClient;
public OpenChargeMapService(OpenChargeMapClient openChargeMapClient) {
this.openChargeMapClient = openChargeMapClient;
}
public List<ChargingStation> getNearestChargingStations(Double latitude, Double longitude) {
validateLatitudeLongitude(latitude, longitude);
List<ChargingStation> chargingStationsList = openChargeMapClient.getNearestChargingStations(latitude, longitude);
return chargingStationsList;
}
private void validateLatitudeLongitude(Double latitude, Double longitude) {
if (isLatitudeOutOfRange(latitude) || isLongitudeOutOfRange(longitude)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Values for latitude and longitude are out of range.");
}
}
private boolean isLatitudeOutOfRange(Double latitude) {
return (latitude.compareTo(MIN_LATITUDE) < 0) || (latitude.compareTo(MAX_LATITUDE) > 0);
}
private boolean isLongitudeOutOfRange(Double longitude) {
return (longitude.compareTo(MIN_LONGITUDE) < 0) || (longitude.compareTo(MAX_LONGITUDE) > 0);
}
}
The build.gradle:
plugins {
id 'org.springframework.boot' version '2.5.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.projectlombok:lombok:1.18.18'
implementation 'junit:junit:4.13.1'
annotationProcessor 'org.projectlombok:lombok:1.18.18'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}

Since you are using Spring Boot. You have two options. You can either test the class in isolation or you can load in the spring context.
Both approaches have their benefits and limitations.
Supply mock directly into the class in test.
public class Test {
ClassToMock service = mock(ClassToMock.class)
ClassInTest testService = new ClassInTest(service);
#Test
public void doTest() {
....
}
}
This approach, you are supplying a mock directly into your service in test. This will result in speedier runtimes, but you will not be able to do more comprehensive integration tests.
Load mock service directly into spring context
#SpringBootTest
#RunWith(SpringJUnit4ClassRunner.class)
public class Test {
#MockBean
ClassToMock service;
#Autowired
ClassInTest testService;
#Test
public void doTest() {
....
}
}
This will generate the spring context and mock the service component you have created. Doing so would test the wiring of your beans and your service in test. You will also have more flexibility in case you want to mock an encapsulating service (i.e. repository layer/data layer). You should also be able to use #Mock & #InjectMock with the class annotation you are currently using.

Related

Why does #InjectMocks in Java Spring return null in my test case for a service?

I am trying to write test cases for a service I have implemented for a controller in Spring. I have the following Service and Test classes.
StudentCourseRequestService:
import java.util.List;
import org.springframework.stereotype.Service;
import model.CourseRequest;
import repository.ICourseRequestRepository;
import lombok.RequiredArgsConstructor;
#Service
#RequiredArgsConstructor
public class StudentCourseRequestService implements IStudentCourseRequestService {
private final ICourseRequestRepository courseRequestRepository;
#Override
public boolean requestCourse(CourseRequest courseRequest) {
return courseRequestRepository.saveRequest(courseRequest);
}
#Override
public List<CourseRequest> getAllCourseRequests() {
return courseRequestRepository.getAllCourseRequests();
}
#Override
public List<CourseRequest> getAllCourseRequestsOfStudent(Long studentId) {
return courseRequestRepository.getCourseRequestsByStudentId(studentId);
}
}
Test class:
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.springframework.boot.test.mock.mockito.MockBean;
import static org.assertj.core.api.Assertions.assertThat;
import model.CourseRequest;
import repository.ICourseRequestRepository;
public class StudentCourseRequestServiceTests {
#MockBean
private ICourseRequestRepository courseRequestRepository;
#InjectMocks
private StudentCourseRequestService studentCourseRequestService;
#Test
public void requestValidCourse() throws Exception {
final CourseRequest courseRequest = new CourseRequest(
"data0",
"data1",
"data2",
"data3",
"data4",
"data5"
);
Mockito.when(courseRequestRepository.saveRequest(courseRequest)).thenReturn(true);
boolean result = studentCourseRequestService.requestCourse(courseRequest);
assertThat(result).isTrue();
}
}
When I run the requestValidCourse() test case, I get the following error:
java.lang.NullPointerException: Cannot invoke "repository.ICourseRequestRepository.saveRequest(model.CourseRequest)" because "this.courseRequestRepository" is null
at service.StudentCourseRequestServiceTests.requestValidCourse(StudentCourseRequestServiceTests.java:29)
at java.base/java.util.ArrayList.forEach(Unknown Source)
at java.base/java.util.ArrayList.forEach(Unknown Source)
How can I resolve this issue?
As #Lesiak recommended, I added the #ExtendWith(MockitoExtension.class) annotation to the top of the class, and changed courseRequestRepository to have the #Mock annotation, instead of the previous #MockBean annotation. The issue got resolved after these changes.
Previously I had the configuration recommended by #Lesiak during my trials to resolve the issue, but I also had the annotation #ExtendWith(SpringExtension.class) at the top of the class. With this configuration, I got the same error. Removing the #ExtendWith(SpringExtension.class) annotation resolved the issue here.
New test class:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.assertj.core.api.Assertions.assertThat;
import model.CourseRequest;
import repository.ICourseRequestRepository;
#ExtendWith(MockitoExtension.class)
public class StudentCourseRequestServiceTests {
#Mock
private ICourseRequestRepository courseRequestRepository;
#InjectMocks
private StudentCourseRequestService studentCourseRequestService;
#Test
public void requestValidCourse() throws Exception {
final CourseRequest courseRequest = new CourseRequest(
"data0",
"data1",
"data2",
"data3",
"data4",
"data5"
);
Mockito.when(courseRequestRepository.saveRequest(courseRequest)).thenReturn(true);
boolean result = studentCourseRequestService.requestCourse(courseRequest);
assertThat(result).isTrue();
}
}

Autowired MockMvc is null with JUnit 5 and Spring boot

I have a class test which send me an error when I run the test.
I followed several threads and I have the right import "import org.junit.jupiter.api.Test"
So I don't understand why it sends me this error :
Cannot invoke "org.springframework.test.web.servlet.MockMvc.perform(org.springframework.test.web.servlet.RequestBuilder)" because "this.mockMvc" is null
My code :
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
#WebMvcTest(TestController.class)
public class ControllerTest {
#Autowired
private MockMvc mockMvc;
#MockBean
private CreateMessageProvider createMessageProvider;
#Test
public void test() throws Exception {
this.mockMvc.perform(get("/test"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string("OK"));
}
}
Gradle config :
mockitoCoreVersion = '4.6.1'
mockitoJunitJupiterVersion = '4.6.1'
springBootTestVersion = '2.7.2'
springTestVersion = '5.3.22'
testImplementation "org.springframework.boot:spring-boot-test:$springBootTestVersion"
testImplementation "org.springframework:spring-test:$springTestVersion"
testImplementation "org.mockito:mockito-junit-jupiter:$mockitoJunitJupiterVersion"
testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"
EDIT : I found the solution. My gradle file didn't have this dependency :
testImplementation "org.springframework.boot:spring-boot-starter-test:2.7.2"
Try
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
#AutoConfigureMockMvc
#SpringBootTest
public class ControllerTest {
#Autowired
private MockMvc mockMvc;
#MockBean
private CreateMessageProvider createMessageProvider;
#Test
public void test() throws Exception {
this.mockMvc.perform(get("/test"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string("OK"));
}
}
Use these annotations :
#RunWith(SpringRunner.class)
#AutoConfigureMockMvc
#ExtendWith({RestDocumentationExtension.class, SpringExtension.class})

How do i create a fetch service to retrieve sprint reports from JIRA?Can someone help me with the 2nd step in the pic?

https://ibb.co/Rjhwzfm
So i have to create a fetch service to retrieve sprint reports(basically data) from JIRA using the JIRA rest api so far have done this using REST template in springboot
SpringBootApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
#SpringBootApplication
public class SprintBootApplication {
private static final Logger log = LoggerFactory.getLogger(SprintBootApplication.class);
public static void main(String args[]) {
SpringApplication.run(SprintBootApplication.class);
}
#Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
#Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
JiraDataFields data = restTemplate.getForObject(
"https://jira.mtvi.com/rest/api/2/search?fields=issuetype,customfield_10280,parent,key,summary,status,timeestimate,aggregatetimeoriginalestimate,assignee,subtasks&jql=project", JiraDataFields.class);
log.info(data.toString());
};
}
}```
JiraDataFields.class
```package com.sprint.SprintBoot;
public class JiraDataFields {
private int id;
private String workedBy;
}```

Aspect in Spring boot Custom Annotation

I am creating a custom annotation NullCheck for method parameter to check value is null or not hello(#NullCheck String text), but I am not able to invoke Aspect around the Annotation.
Main Class
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
#SpringBootApplication
#EnableAutoConfiguration
#EnableAspectJAutoProxy
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Controller class, just invoking an aspect for POC, not returning anything
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/")
class HelloController {
#GetMapping
public void hello() {
hello("hi");
}
private void hello(#NullCheck String text) {
System.out.println(text);
}
}
Annotation
package com.example.demo;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
#Documented
#Retention(RUNTIME)
#Target(ElementType.PARAMETER)
public #interface NullCheck { }
Aspect
package com.example.demo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
#Aspect
#Component
public class NullCheckAspect {
this is working
#Before("#annotation(org.springframework.web.bind.annotation.GetMapping)")
but this is not
#Before("#annotation(com.example.demo.NullCheck)")
public void beforeAdvice(JoinPoint joinPoint) {
System.out.println("Before method:" + joinPoint.getSignature());
}
}
build.gradle
buildscript {
ext {
springBootVersion = '2.1.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
idea {
module {
// if you hate browsing Javadoc
downloadJavadoc = true
// and love reading sources :)
downloadSources = true
}
}
bootJar {
launchScript()
}
repositories {
mavenCentral()
jcenter()
}
bootJar {
launchScript()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
}
what am I missing?
As per my understanding and after doing some search on google, you can get method parameter and its value with particular annotation with following code:
package com.example.demo;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
#Component
#Aspect
public class NullCheckAspect {
#Around("execution(* com.example.demo.HelloController.nullChecker(String))")
public Object around(ProceedingJoinPoint pJoinPoint) throws Throwable {
Object[] args = pJoinPoint.getArgs();
Method method = MethodSignature.class.cast(pJoinPoint.getSignature()).getMethod();
Annotation[][] parametersAnnotations = method.getParameterAnnotations();
Map<String, Object> annotatedParameters = new HashMap<>();
int i = 0;
for(Annotation[] parameters : parametersAnnotations) {
Object arg = args[i];
String name = method.getParameters()[i++].getDeclaringExecutable().getName();
for(Annotation parameter: parameters) {
if ((parameter instanceof NullCheck)) {
System.out.println("Found the null checker annotation with name: " + name);
System.out.println("Found the null checker annotation with arg: " + arg);
annotatedParameters.put(name, arg);
}
}
}
System.out.println(annotatedParameters);
return pJoinPoint.proceed(args);
}
}
And with the interface annotation:
package com.example.demo;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;
#Component
#Retention(RUNTIME)
#Target(ElementType.PARAMETER)
public #interface NullCheck {
}
More details about my code you can check it at my github repository, I have created spring boot demo version and pushed it for you at https://github.com/krishnaiitd/learningJava/blob/master/springBoot/gs-spring-boot/src/main/java/com/example/demo/HelloController.java
This also include other type of aspects like tracking the time of a particular methods.
Hope this will help you to get the basic understanding of #Aspect in Spring boot.
I don't know why it is as it is but I had the same issue. This is can be solved easily by using Pointcuts, for example:
#Before("nulllCheckAnnotation()")
public void beforeAdvice(JoinPoint joinPoint) {
System.out.println("Before method:" + joinPoint.getSignature());
}
}
#Pointcut("#annotation(com.example.demo.NullCheck)")
private void nulllCheckAnnotation() { }
Read more about pointcuts here if you interested: https://www.baeldung.com/spring-aop-pointcut-tutorial

Why doesn't childApplicationContext.registerSingleton(class) effect what gets autowired?

This test is failing but I don't know why or how to fix it. If I hit a break point and call mockingContext.getBean(Repository.class), it does return my mock object, but for some reason the ProductionCode returned in createProductionCodeWithMock still has the real Repository autowired into it. I can only suspect that I need to do something special/extra to have an effect on autowired beans, but I don't know what it is. Why isn't this test passing, and how can I make it pass?
For what it's worth, I'm well aware of all the answers to this question. I still want to know why this test isn't working. I am using Spring 3.x.
Here's the test:
package mavensnapshot;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class ProductionCodeTest {
#Test
public void testReplaceRepositoryWithMock() {
Repository mockRepository = mock(Repository.class);
ProductionCode productionCode = createProductionCodeWithMock(mockRepository);
productionCode.doSomething();
verify(mockRepository).save();
}
private ProductionCode createProductionCodeWithMock(Repository mockRepository) {
GenericApplicationContext mockingContext = new GenericApplicationContext();
mockingContext.getBeanFactory().registerSingleton(Repository.class.getName(), mockRepository);
mockingContext.setParent(new ClassPathXmlApplicationContext("/beans.xml"));
return mockingContext.getBean(ProductionCode.class);
}
}
Here's my production code:
package mavensnapshot;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/beans.xml");
ProductionCode productionCode = context.getBean(ProductionCode.class);
productionCode.doSomething();
}
}
package mavensnapshot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service
public class ProductionCode {
#Autowired
private Repository repository;
public void doSomething() {
repository.save();
}
}
package mavensnapshot;
import org.springframework.stereotype.Service;
#Service
public class Repository {
public void save() {
System.out.println("production code");
}
}

Categories