Home > Mobile >  Junit and mockito with spring boot list is not available
Junit and mockito with spring boot list is not available

Time:10-01

hi every one i have some problem

i have a test class called TestUserService

    @RunWith(MockitoJUnitRunner.class)
    public class TestUserService {
    
        @InjectMocks
        UserService userService;
    
        @Mock
        InMemory inMemory;
    
    
    
    
        @Before
        public void init(){
            MockitoAnnotations.initMocks(this);
        }
    
        @Test
        public void registry(){
            
            System.out.println("hi");
            UserDto userDto=new UserDto(1L,"abed","alrhman",26);
            UserDto userDto1=new UserDto(1L,"abdallah","almasre",27);
            UserDto userDto2=new UserDto(1L,"bisher","alahmad",35);
            UserDto userDto3=new UserDto(1L,"ibrahem","alrabe",32);
    
           
            userService.registry(userDto);
            userService.registry(userDto1);
            userService.registry(userDto2);
            userService.registry(userDto3);
            System.out.println("\"registry\" = "   "registry");
            verify(inMemory,times(0)).save(userDto.ToDTO(userDto));
            verify(inMemory,times(1)).save(userDto1.ToDTO(userDto1));
            verify(inMemory,times(0)).save(userDto2.ToDTO(userDto2));
            verify(inMemory,times(0)).save(userDto3.ToDTO(userDto3));
    
        }
    
    }

when run the test i want save new user by UserService class and UserService will go to InMemory class

@Service
public class UserService implements IUserService {

    private final InMemory inMemory;

    public UserService(InMemory inMemory){
        this.inMemory=inMemory;
    }

    @Override
    public UserDto registry(UserDto userDto) {
        User user=userDto.ToDTO(userDto);
        User user1=inMemory.save(user);
        return user.toEntity(user1);
    }
}

the problem in InMemory Class it is a local repository has Map Called users and some other methods after called registry(User user) method
the Map has it when debugger 'this' is not available and the user param not receive into the save(User user) method and the debugger dont walk of any point check

private Map<Long,User> users=new HashMap<>();
    @Override
    public  User save(User user) {
        user.setId(1L);
        users.put(1L,user);
        return user;
    }

and this is Image when debugger code

enter image description here

if you attention save(User user) dont has object or any value and users map not avalible

CodePudding user response:

In your test, you are defining InMemory as a mock. A mock is an object that emulates the behavior of a real object according to how you configure such mock to behave. Having said that, your InMemory object in your test is not a real InMemory object, but a simple mock which means that its real code will not be called. Take a look at @Spy and consider replacing @Mock with it. Find more information in the following articles:

CodePudding user response:

in first step we must replace @Mock to Spy

and then we must change in when() method to when(spyInstance.method) and then we need called method has spyInstance.method to testing

this is my code after fixed you can see it

@Spy
InMemory inMemoryGet;  // replace it from mock to spy


@InjectMocks
UserService userService;

@Before
public void init(){
    MockitoAnnotations.initMocks(this);
}

@Test
public void registry(){

    UserDto emp = new UserDto(1L,"Lokesh","Gupta",25); // add it

    when(inMemoryGet.save(emp.ToDTO(emp))).thenReturn(emp.ToDTO(emp));
    verify(inMemoryGet, times(0)).save(emp.ToDTO(emp)); // how many time invoke
    userService.registry(emp); // called method from here
    verify(inMemoryGet, times(1)).save(emp.ToDTO(emp));
    System.out.println("\"Test\" = "   "Pass");
}
  • Related