简体   繁体   中英

Junit - mockito Wanted but not invoked:

Am writting test class for my service implementation class. That service impl class having communication with my repository interface for DB action.

Code is working properly, but while i was writting the test cases for this implementation and getting "wanter but not invoked" error from junit.

Please find my classes below.

Following is my Repository interface

public interface UserRepository extends JpaSpecificationExecutor<UserDeactivationThreshold>,CrudRepository<UserDeactivationThreshold,Long> {

    @Query("SELECT bt from BusinessTypes bt where bt.code = :businessTypeCode")
    BusinessTypes findByBusinessCodeId(@Param("businessTypeCode") String businessTypeCode); 
    }

Following is my Service interface

public interface UserDeactivationThresholdService {

    public List<UserDeactivationThreshold> getThresholdValue(String businessTypeName);
}

Following is my Service Implementation class

@Service
@Transactional(readOnly = true)
public class UserDeactivationThresholdServiceImpl implements UserDeactivationThresholdService {

    private UserRepository userRepository;


    @Autowired
    public UserDeactivationThresholdServiceImpl(UserRepository userRepository,SecurityClient securityClient, EmailDispatcherService emailDispatcherService) {
        this.userRepository = userRepository;

    }


    @Override
    public List<UserDeactivationThreshold> getThresholdValue(String businessTypeName){
        return userRepository.findByBusinessType(businessTypeName);
    }
}

Please find my Test class

@RunWith(MockitoJUnitRunner.class)
public class UserDeactivationThresholdServiceImplTest {

    @Mock
    private UserRepository userRepository;

    @Mock private UserDeactivationThresholdServiceImpl userDeactivationThresholdServiceImpl;

    @Test
    public void shouldGetThresholdValueTest() {
        UserDeactivationThreshold userDeactivationThreshold = make(a(UserDeactivationThresholdMaker.BCP));
        when(userRepository.findByBusinessType("BCP")).thenReturn(asList(userDeactivationThreshold));

        verify(userRepository, times(1)).findByBusinessType("BCP");
    }
}

Following errors that am getting :

Wanted but not invoked:
userDeactivationThresholdRepository.findByBusinessType(
    "BCP"
);
-> at com.core.service.impl.UserDeactivationThresholdServiceImplTest.shouldGetThresholdValueTest(UserDeactivationThresholdServiceImplTest.java:58)
Actually, there were zero interactions with this mock.

* Update *

I tried in this way also, but that also not worked.

verify(userDeactivationThresholdServiceImpl, times(1)).getThresholdValue("BCP");

Following error that i was getting.

-> at com.core.service.impl.UserDeactivationThresholdServiceImplTest.shouldGetThresholdValueTest(UserDeactivationThresholdServiceImplTest.java:58)
Actually, there were zero interactions with this mock.

* Update 2 *

I have added the below code in to my test class. But now am getting the different issue "The method is(Integer) is ambiguous for the type UserDeactivationThresholdServiceImplTest"

    List<UserDeactivationThreshold> userDeactivationThresholdList = userDeactivationThresholdServiceImpl.getThresholdValue("BCP");
    assertThat(userDeactivationThresholdList.size(), is(1));

Am getting the below messiage for is(1) .

The method is(Integer) is ambiguous for the type UserDeactivationThresholdServiceImplTest

As others pointed out already you made the usual beginner mistakes:

  • Do not mock the class under test .
  • Make sure that your mocks are injected into your class under test , either by using annotations ( @Mock , @InjectMocks ) or by providing the references to the mock manually to the class under test .
  • Use the instance of your class under test in the test to actually test something.

Make sure to read the documentation and a tutorial (eg this , this or this ) again. There are also plenty of similiar question to be found here with answers that provide some code examples as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM