Home > database >  How to instantiate my Interface object inside Junit test class?
How to instantiate my Interface object inside Junit test class?

Time:08-14

I am getting NullPointerException at this line: Role additionalRole = this.roleService.getRoleByAuthority(userRegistrationModel.getAdditionalRole()); Because roleService object is not initialised..This is an interface object How can I instantiate inside Junit class? I have tried initializing using constructor Then I see error called "Test class should have exactly one public zero argument constructor so I cant initialize it with my constructor as well..Below is my full code. Can someone suggest here?

package com.doctorAppointmentBookingSystem.serviceImpl;
    
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


import com.doctorAppointmentBookingSystem.entity.Doctor;
    import com.doctorAppointmentBookingSystem.entity.Role;
    import com.doctorAppointmentBookingSystem.entity.User;
    import com.doctorAppointmentBookingSystem.entity.WeekSchedule;
    import com.doctorAppointmentBookingSystem.model.bindingModel.DoctorRegistrationModel;
    import com.doctorAppointmentBookingSystem.model.bindingModel.UserRegistrationModel;
    import com.doctorAppointmentBookingSystem.repository.DoctorRepository;
    import com.doctorAppointmentBookingSystem.repository.UserRepository;
    import com.doctorAppointmentBookingSystem.service.RoleService;

import com.doctorAppointmentBookingSystem.service.UserService;
    import com.doctorAppointmentBookingSystem.service.WeekScheduleService;
@SpringBootTest
    public class DoctorServiceImplTest {

         private ModelMapper modelMapper;
        
           private UserService userService;
        
            private DoctorRepository doctorRepository;

            private WeekScheduleService weekScheduleService;
            private BCryptPasswordEncoder bCryptPasswordEncoder;
           @Autowired
            private RoleService roleService;
            @Autowired
            private UserRepository userRepository;

/*          public DoctorServiceImplTest(RoleService roleService) {
            
                this.roleService = roleService;
            }
        public DoctorServiceImplTest() {
                super();
            }*/
        @Test
        public void saveDoctor() throws ParseException
        {

            DateFormat d2= new SimpleDateFormat("1997-06-25");
            DateFormat d3= new SimpleDateFormat("2010-06-25");
            modelMapper=new ModelMapper();
            bCryptPasswordEncoder= new BCryptPasswordEncoder();
          
            Date d4 = d2.parse("1997-06-25");
            Date d5 = d3.parse("2010-06-25");
            DoctorRegistrationModel d= new DoctorRegistrationModel("[email protected]"
                    ,"ajfjdhskjfh","1234567890","1234567890","shfgjdsgh","shfgjdsgh",
                    "1234567890",d4,"MALE", d5);
            System.out.println(d);
        
            UserRegistrationModel userRegistrationModel = this.modelMapper.map(d, UserRegistrationModel.class);
            
            
            String DEFAULT_DOCTOR_ROLE = "ROLE_DOCTOR";
            System.out.println("userRegistrationModel" userRegistrationModel);
            userRegistrationModel.setAdditionalRole(DEFAULT_DOCTOR_ROLE);
            
            User user = this.modelMapper.map(userRegistrationModel, User.class);
            String encryptedPassword = this.bCryptPasswordEncoder.encode(userRegistrationModel.getPassword());
            user.setPassword(encryptedPassword);
            System.out.println(user);
            user.setAccountNonExpired(true);
            user.setAccountNonLocked(true);
            user.setCredentialsNonExpired(true);
            user.setEnabled(true);
       //     user.addRole(this.roleService.getDefaultRole());
            System.out.println("roleService.."  roleService);
            
    
            if (userRegistrationModel.getAdditionalRole() != null) {
                Role additionalRole = this.roleService.getRoleByAuthority(userRegistrationModel.getAdditionalRole());
                user.addRole(additionalRole);
                System.out.println("additionalRole.."  additionalRole);
            }
          User u=  this.userRepository.saveAndFlush(user);
             WeekSchedule weekSchedule = this.weekScheduleService.createDefault();
    
                Doctor doctor = this.modelMapper.map(u, Doctor.class);
                doctor.setUser(u);
                doctor.setWeekSchedule(weekSchedule);
                doctorRepository.saveAndFlush(doctor);
        }








        
    }

CodePudding user response:

Out of comments, I believe the spring boot / spring versions you're using are too old. If I remember correctly, at that time, spring boot tests used to support junit 4 only, while you're trying to use JUnit 5. So you have the following options:

  1. Downgrade to JUnit 4 and use @RunWith(SpringRunner.class)
  2. Upgrade spring / spring boot
  3. Try to blend them together with this article. You might also want to read this article.

Needless to say that solution (2) is the best one :)

  • Related