mockito

Mockito in maven using JPMS cannot access a member of class with modifiers “private”

无人久伴 提交于 2020-12-15 06:24:01
问题 I am migrating a codebase to Java 11 and JPMS / Jigsaw and am having some trouble with mocking. This is the test I am trying to run. import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit

How to mock ElasticsearchOperations (spring-data-elasticsearch v.4.0.3) when unit testing

馋奶兔 提交于 2020-12-15 05:59:40
问题 I am working on a project that uses Spring 2.3.3, spring-data-elastic-4.0.3, ElasticSearch 7.9.0 and mockito-core 3.3.3 I'm trying to mock ElasticsearchOperations when writing unit tests, this is the Service I would like to test (where it's used ElasticsearchOperations): @Service public class SearchByLabelServiceImpl implements SearchByLabelService { private ElasticsearchOperations elasticsearchTemplate; public SearchByLabelServiceImpl(ElasticsearchOperations elasticsearchTemplate) { this

How to mock ElasticsearchOperations (spring-data-elasticsearch v.4.0.3) when unit testing

旧时模样 提交于 2020-12-15 05:58:22
问题 I am working on a project that uses Spring 2.3.3, spring-data-elastic-4.0.3, ElasticSearch 7.9.0 and mockito-core 3.3.3 I'm trying to mock ElasticsearchOperations when writing unit tests, this is the Service I would like to test (where it's used ElasticsearchOperations): @Service public class SearchByLabelServiceImpl implements SearchByLabelService { private ElasticsearchOperations elasticsearchTemplate; public SearchByLabelServiceImpl(ElasticsearchOperations elasticsearchTemplate) { this

Unable to mock static methods with parameters using Mockito

可紊 提交于 2020-12-15 05:51:55
问题 I am trying to use some of the new features of Mockito, specifically mocking of static methods. I am able to get it to work when the method I am mocking has no parameters, but for some reason it will not work if the method has any parameters. As the example below is written, the test assertEquals( "bar", Foo.foo() ) works but the test assertEquals(2, map.size() ) fails as no behavior has been defined for the mocked class. fooMock.when(Foo::genMap).thenCallRealMethod() gives the follwing

Write a unit test for downloading a file

风流意气都作罢 提交于 2020-12-05 05:21:06
问题 At the moment I wrote a small downloadService which let's user download a file (at the moment only excel). The code works properly, however I do not know how to write the unit test for it. That's my code: package com.pzm.service; import com.pzm.model.UserBillingsMock; import com.pzm.model.report.ExcelReport; import com.pzm.model.report.Report; import com.pzm.model.report.ReportFactory; import org.springframework.stereotype.Repository; import javax.servlet.ServletOutputStream; import javax

“Cannot resolve method” with mockito

痞子三分冷 提交于 2020-12-04 15:57:17
问题 I use org.springframework.security.core.Authentication which has a method: Collection<? extends GrantedAuthority> getAuthorities(); I want to mock it as below: when(authentication.getAuthorities()).thenReturn(grantedAuthorities); with authorities collection: Collection<SimpleGrantedAuthority> grantedAuthorities = Lists.newArrayList( new SimpleGrantedAuthority(AuthoritiesConstants.USER)); And I am using org.springframework.security.core.authority.SimpleGrantedAuthority which extends

“Cannot resolve method” with mockito

主宰稳场 提交于 2020-12-04 15:57:06
问题 I use org.springframework.security.core.Authentication which has a method: Collection<? extends GrantedAuthority> getAuthorities(); I want to mock it as below: when(authentication.getAuthorities()).thenReturn(grantedAuthorities); with authorities collection: Collection<SimpleGrantedAuthority> grantedAuthorities = Lists.newArrayList( new SimpleGrantedAuthority(AuthoritiesConstants.USER)); And I am using org.springframework.security.core.authority.SimpleGrantedAuthority which extends

How to mock JPA repository's save method in unit tests

醉酒当歌 提交于 2020-11-30 09:09:28
问题 For example, I have this method in UserService: @Override @Transactional public UserDto create(UserDto userDto) { User dbUser = userRepository.findOne(userDto.getId()); if (dbUser != null) { throw new AuthException(AuthException.ErrorCode.DUPLICATE_USER_EXCEPTION); } User oneByLogin = userRepository.findOneByLogin(userDto.getLogin()); if (oneByLogin != null) { throw new AuthExceptionAuthException.ErrorCode.DUPLICATE_LOGIN_EXCEPTION); } User newUser = new User(); newUser.setGuid(UUID