I use spring boot 2.3.2 with mapstruct.
In a service class I have a mapper who have an autowired annotation.
#Service
public BillingService{
private BillingRepository billingRepository;
#Autowired
private BillingMapper billingMapper;
#Autowired
public BillingService (BillingRepository billingRepository){
this.billingRepository=billingRepository;
}
public void getBilling(Long billingId){
....
billingMapper.convertTo(...);
}
}
#RunWith(MockitoJUnitRunner.class)
public class BillingServiceTest{
#Mock
BillingRepository billingRepository;
private BillingService bilingService;
#Spy
private BillingMapper billingMapper = Mappers.getMapper(BillingMapper.class);
#BeforeEach
public void setup(){
MockitoAnnotations.initMocks(this);
billingService = new BillingService();
}
#Test
public void testGetBilling(){
List<Billing> billings = new ArrayList<>();
...
List<BillingPayload> payloads = new ArrayList<>();
when(billingMapper.convertTo(billings)).thenReturn(payloads);
bilingService.getBilling(1l);
}
}
#Mapper(componentModel="spring")
public interface BillingMapper{
...
}
When I debug and I'm stuck in getBilling method in BillingService Class, billingMapper is alway null;
You are using very strange configuration. First of all you have method returning BillingService that doesn't specify any return value so this would not even compile. I suggest following way:
#Service
public BillingService{
private final BillingRepository billingRepository;
private final BillingMapper billingMapper;
// constructor with bean injection
public BillingService(final BillingRepository billingRepository,
final BillingMapper billingMapper) {
this.billingRepository = billingRepository;
this.billingMapper = billingMapper;
}
public void getBilling(Long billingId){
....
billingMapper.convertTo(...);
}
}
Then you can configure your test like following:
#RunWith(SpringJUnit4ClassRunner.class)
public class BillingServiceTest {
#Spy private BillingMapper billingMapper = Mappers.getMapper(BillingMapper.class);
#MockBean private BillingRepository billingRepository;
#Autowired private BillingService billingService;
#TestConfiguration
static class BillingServiceTestContextConfiguration {
#Bean public BillingService billingService() {
return new BillingService(billingRepository, billingMapper);
}
}
#Test
public void testGetBilling(){
List<Billing> billings = new ArrayList<>();
...
List<BillingPayload> payloads = new ArrayList<>();
when(billingRepository.findById(1L)).thenReturn(); // someResult
when(billingMapper.convertTo(billings)).thenReturn(payloads);
bilingService.getBilling(1l);
}
}
#Mapper(componentModel="spring")
public interface BillingMapper{
...
}
Similiar configuration should work. Main problem is that you are mixing #Autowired with setter/constructor injection (don't know since your weird method inside BillingService. Also dont know why you use #Spy annotation when you are tryning to Mock interface. #Spy is used to mock actual instance, while #Mock is mocking Class type. I would stick with #MockBean private BillingMapper billingMapper instead if BillingMapper is specified as Bean in your application.
Related
I try to implement MockitoExtension in Spring Boot with a simple service class. When I implement it via field-set, it works. But when I implement it constructor, i doesn't work.
ExampleService:
#Service
public class ExampleService
{
private final AmqpTemplate requestTemplate;
private final AmqpTemplate resultTemplate;
public ExampleService(#Qualifier("requestTemplate") AmqpTemplate requestTemplate,
#Qualifier("resultTemplate") AmqpTemplate resultTemplate)
{
this.requestTemplate = requestTemplate;
this.resultTemplate = resultTemplate;
}
}
ExampleServiceTest:
#ExtendWith(MockitoExtension.class)
public class ExampleServiceTest
{
#InjectMocks
private ExampleService exampleService;
#Mock(name = "requestTemplate")
private AmqpTemplate requestTemplate;
#Mock(name = "resultTemplate")
private AmqpTemplate resultTemplate;
#Test
public void test1()
{
...
}
}
requestTemplate and resultTemplate looks like same object on test cases and it behaves wrong...
When I implement it via Autowired, it works...
#Service
public class ExampleService
{
#Autowired
private AmqpTemplate requestTemplate;
#Autowired
private AmqpTemplate resultTemplate;
...
}
I wonder the constructor solution and reason why it doesn't work.
In my JUNIT5-test I want to mock a bean by #MockBean. In my #BeforeEach - method the calls are injected.
But other beans #Autowire-ing the #MockBean are instantiated with the #MockBean before the method injection. This is weird and gives me NPEs. How can I force method injection before use of the #MockBean?
#SpringBootTest
#ExtendWith(SpringExtension.class)
#ContextConfiguration("classpath:context/authenticationStaff.xml")
#EnableAutoConfiguration
public class PasswordPolicyServiceTest {
private final List<Reference> bcryptDigestRefs = new ArrayList<>();
private final DigestHistoryRule bcryptDigestRule = new DigestHistoryRule(new BCryptHashBean());
#MockBean
private SystemConfiguration systemConfiguration;
#BeforeEach
public void initMock() {
MockitoAnnotations.initMocks(this);
Arrays.asList(SystemConfigKey.values()).forEach(key -> {
Mockito.when(systemConfiguration.getConfig(key)).thenReturn(getConfig(key, key.getDefaultValue()));
});
Mockito.when(systemConfiguration.getConfig(SystemConfigKey.MIN_PASSWORD_LENGTH)).thenReturn(getConfig(SystemConfigKey.MIN_PASSWORD_LENGTH, "5"));
A failing class is:
#Service
public class SessionCacheManager {
private final Ehcache ehCache;
private final Cache<String, SessionVerificationType> sessionCache;
private final SystemConfiguration systemConfiguration;
#Autowired
public SessionCacheManager(final Ehcache ehCache, final SystemConfiguration systemConfiguration) {
this.ehCache=ehCache;
this.systemConfiguration=systemConfiguration;
SystemConfigType systemConfig = systemConfiguration.getConfig(SystemConfigKey.SESSION_MAX_NUMBER);
Integer numberOfParalledSessions = systemConfig.getIntegerValue();
CacheManager cacheManager=ehCache.registerNewCacheManager(CACHE_MANAGER);
sessionCache = cacheManager.createCache(CACHE_NAME,
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, SessionVerificationType.class, ResourcePoolsBuilder.heap(numberOfParalledSessions)));
}
As I can see (with debug), the "SessionCacheManager" uses the mocked "SystemConfiguration" but systemConfiguration.getConfig(SystemConfigKey.SESSION_MAX_NUMBER); returns a null.
I helped myself, though I do not like my solution. It is more a trick than a solution. But I cannot think of something else right now.
I change the #ContextConfiguration to:
#ContextConfiguration(locations = "/context/authenticationStaff.xml", classes = { SpringApplicationContext.class })
The XML is setup, that it cannot autodetect the class "SystemConfiguration.class".
Instead of that the "SpringApplicationContext.class" provides the "SystemConfiguration.class" as a mocked bean.
#Configuration
public class SpringApplicationContext {
#Mock
private SystemConfiguration mockedSystemConfiguration;
#Bean
public SystemConfiguration systemConfiguration() {
MockitoAnnotations.initMocks(this);
Arrays.asList(SystemConfigKey.values()).forEach(key -> {
Mockito.when(mockedSystemConfiguration.getConfig(key)).thenReturn(getConfig(key, key.getDefaultValue()));
});
Mockito.when(mockedSystemConfiguration.getConfig(SystemConfigKey.MIN_PASSWORD_LENGTH)).thenReturn(getConfig(SystemConfigKey.MIN_PASSWORD_LENGTH, "5"));
Mockito.when(mockedSystemConfiguration.getConfig(SystemConfigKey.PASSWORD_BCRYPTENCODER_COSTFACTOR)).thenReturn(getConfig(SystemConfigKey.PASSWORD_BCRYPTENCODER_COSTFACTOR, "5"));
return mockedSystemConfiguration;
}
private SystemConfigType getConfig(SystemConfigKey key, String value) {
SystemConfigType config = new SystemConfigType();
config.setKey(key);
config.setValue(value);
return config;
}
The test-code now looks like this:
#SpringBootTest
#ExtendWith(SpringExtension.class)
#ContextConfiguration(locations = "/context/authenticationStaff.xml", classes = { SpringApplicationContext.class })
#EnableAutoConfiguration
public class PasswordPolicyServiceTest {
#Autowired
private PasswordPolicyService passwordPolicyService;
#Autowired
private PasswordHandlerService passwordHandlerService;
#Autowired
private SystemConfiguration systemConfiguration;
private final List<Reference> bcryptDigestRefs = new ArrayList<>();
private final DigestHistoryRule bcryptDigestRule = new DigestHistoryRule(new BCryptHashBean());
#BeforeEach
public void initMock() {
MockitoAnnotations.initMocks(this);
String password=passwordHandlerService.getPassword("my$Password");
bcryptDigestRefs.add(
new HistoricalReference(
"bcrypt-history",
password));
}
This works, but is not a nice solution. Other recommendations are very welcome.
I'm trying to #MockBean a #Repository annotated class:
#Repository
public interface ApplicationDao extends MongoRepository<Application, String> {}
I'm injecting it into a #Service annotated class:
#Service
public class AuthorizationService {
private ApplicationDao appsDao;
private List<Application> allowedApplications;
#Autowired
public AuthorizationService(ApplicationDao appsDao) {
this.appsDao = appsDao; //<<MOCKED INJECTED BEAN>>
this.fillApplications();
}
private void fillApplications() {
this.appsDao.findAll() //<<MOCKED method>>
.forEach(entry -> {
this.allowedApplications.put(entry.getName(), entry);
});
}
public bool isAuthorized(Application application) {
return this.allowedApplications
.stream()
.anyMatch(app -> app.getId().equals(application.getId()));
}
}
My test mocking configuration looks like:
#RunWith(SpringRunner.class)
#SpringBootTest()
public class GroupReferencesTest {
private #Autowired AuthorizationService;
private #MockBean ApplicationDao applicationDao;
#Before
public void setUp() {
Application testApplication = new Application();
testApplication.setName("test-application");
List<Application> allowedApplications = new ArrayList<Application>();
allowedApplications.add(testApplication);
Mockito
.when(this.applicationDao.findAll())
.thenReturn(allowedApplications);
}
#Test
public void test() {
Application app = new Application();
app.getId("test-application");
assertTrue(this.authorizationService.isAuthorized(app)); //<<FAILS>>
}
}
Nevertheless, my mocked object is not injected. I mean, when my AuthorizationService calls its injected ApplicationDao is returns an empty list instead of my mocked list.
I've tried to use #MockBean(name="applicationDao") as well. The behavior is the same.
I've also tried to configure my mocked bean using this code:
#TestConfiguration
public class RestTemplateTestConfiguration {
#Bean("applicationDao")
#Primary
public static ApplicationDao mockApplicationDao() {
ApplicationDao mock = Mockito.mock(ApplicationDao.class);
Application testApplication = new Application();
testApplication.setName("test-application");
List<Application> allowedApplications = new ArrayList<Application>();
allowedApplications.add(testApplication);
Mockito
.when(mock.findAll())
.thenReturn(allowedApplications);
return mock;
}
}
However, it doesn't works right.
Application class is:
public class Application {
private String id;
//setters & getters
}
Any ideas?
First things first - the type of test. Answer: Unit test.
You are starting Spring context that manages a lifecycle of AuthorizationService and then you are trying to inject mock. What really happens is that Spring IoC container is injecting a real ApplicationDao (the one managed by Spring IoC container) into the AuthorizationService.
Solution:
Manage lifecyle of AuthorizationService by your test runner (like MockitoJUnitRunner and inject ApplicationDao mock into it):
#RunWith(MockitoJUnitRunner.class)
public class GroupReferencesTest {
private #InjectMocks AuthorizationService authorizationService;
private #Mock ApplicationDao applicationDao;
#Before
public void setUp() {
Application testApplication = new Application();
testApplication.setName("test-application");
List<Application> allowedApplications = new ArrayList<Application>();
allowedApplications.add(testApplication);
Mockito
.when(this.applicationDao.findAll())
.thenReturn(allowedApplications);
}
#Test
public void test() {
Application app = new Application();
app.getId("test-application");
assertTrue(this.authorizationService.isAuthorized(app));
}
}
Working example
#RunWith(SpringRunner.class)
#SpringBootTest(classes = {AuthorizationService.class})
public class GroupReferencesTest {
#Autowired
private AuthorizationService;
#MockBean
private ApplicationDao applicationDao;
#Test
public void test() {
//given
Mockito.when(applicationDao.findAll()).thenReturn(emptyList());
//when & then
assertTrue(authorizationService.isAuthorized(app));
}
}
I came across a problem, while mocking my class i get NullPointerException on mocked object and I cant figure out why mockito is not mocking only one class annotated by #Mock. Do someone have any idea what's the problem there ?
Tested class :
public class TimeoutJpaTransactionManager extends JpaTransactionManager {
private final String TRANSACTION_METHOD_NAME= String.join(".", ZapisPakietuReceptService.class.getName(), "transactionMethod");
#Autowired
private TransactionTimeoutConfig transactionTimeoutConfig;
public TimeoutJpaTransactionManager(EntityManagerFactory emf) {
super(emf);
}
protected int determineTimeout(TransactionDefinition definition) {
if (TRANSACTION_METHOD_NAME.equals(definition.getName())) {
return transactionTimeoutConfig.getSgr();
}
return super.getDefaultTimeout();
}
}
Autowired class :
#Getter
#Setter
#Component
#ConfigurationProperties(prefix = "transaction.timeout")
public class TransactionTimeoutConfig {
private int sgr;
}
Test class:
public class TimeoutJpaTransactionManagerTest {
#InjectMocks
private TimeoutJpaTransactionManager timeoutJpaTransactionManager;
#Mock
private EntityManagerFactory entityManagerFactory;
#Mock
private TransactionDefinition transactionDefinition;
#Mock
private TransactionTimeoutConfig transactionTimeoutConfig;
private final String TRANSACTION_METHOD_NAME_MOCK= String.join(".", ZapisPakietuReceptService.class.getName(), "transactionMethod");
private final int TIMEOUT_VALUE= 30;
#BeforeMethod
public void setUp() {
MockitoAnnotations.initMocks(this);
}
#Test
public void testDetermineTimeout() {
//given
Mockito.when(transactionDefinition.getName()).thenReturn(TRANSACTION_METHOD_NAME_MOCK);
Mockito.when(transactionTimeoutConfig.getSgr()).thenReturn(TIMEOUT_VALUE);
//when
int result = timeoutJpaTransactionManager.determineTimeout(transactionDefinition);
//then
Assertions.assertThat(result).isEqualTo(TIMEOUT_VALUE);
}
}
Whats weird and i cant undestand is that by adding another line like that all seems to work and TransactionDefinition is really mocked :
#BeforeMethod
public void setUp() {
MockitoAnnotations.initMocks(this);
MockitoAnnotations.initMocks(this);
}
Thanks to the M. Deinum comment i was able to figure it out, it seems the problem was with mixing field and constructor injections. Solution is to either move dependencies to the constructor and then wire it there or modify the current test case to
#InjectMocks
private TimeoutJpaTransactionManager timeoutJpaTransactionManager;
#Mock
private TransactionDefinition transactionDefinition;
#Mock
private TransactionTimeoutConfig transactionTimeoutConfig;
private EntityManagerFactory entityManagerFactory;
private final String TRANSACTION_METHOD_NAME_MOCK= String.join(".", ZapisPakietuReceptService.class.getName(), "transactionMethod");
private final int TIMEOUT_VALUE= 30;
#BeforeMethod
public void setUp() {
entityManagerFactory = Mockito.mock(EntityManagerFactory.class);
timeoutJpaTransactionManager = new TimeoutJpaTransactionManager(entityManagerFactory);
MockitoAnnotations.initMocks(this);
}
I´m having issues with my test cases after having introduced #Autowired in one of the classes under test.
My testcase now looks like this:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations={"/applicationContext.xml", "/spring-security.xml"})
public class StudentRepositoryTest extends AbstractDatabaseTestCase {
private StudentRepository studentRepository;
private CompanyRepository companyRepository;
private Student testStudent;
private Company testCompany;
#Before
public void setUp() {
studentRepository = new StudentRepository();
studentRepository.setJdbcTemplate(getJdbcTemplate());
testStudent = Utils.testStudentNoApplication();
}
#Test
....
}
StudentRepository now looks like this:
#Service
public class StudentRepository extends AbstractRepository<Student> {
...
private PasswordEncoder passwordEncoder;
private MailService mailService;
public StudentRepository() {
// TODO Auto-generated constructor stub
}
#Autowired
public StudentRepository(MailService mailService, PasswordEncoder passwordEncoder) {
this.mailService = mailService;
this.passwordEncoder = passwordEncoder;
}
Obviously this test case won´t work anymore.
But what changes do I need to make to the testcase for the #Autowired annotation to be picked up by the test case?
EDIT:
I´ve now updated my setUp() to this (I need the password encoder to avoid null password):
#Before
public void setUp() {
//studentRepository = new StudentRepository();
studentRepository = new StudentRepository(mock(MailService.class), ctx.getAutowireCapableBeanFactory().createBean(ShaPasswordEncoder.class));
studentRepository.setJdbcTemplate(getJdbcTemplate());
testStudent = Utils.testStudentNoApplication();
}
My testcase is now running OK, but my testsuite failes with a NullPointerException.
I´m guessing the ApplicationContext is not being Autowired when running the testsuite for some reason?
If you don't want to declare your StudentRepository in one of XML files referenced by #ContextConfiguration and autowire it into the test, you can try to use AutowireCapableBeanFactory as follows:
...
public class StudentRepositoryTest extends AbstractDatabaseTestCase {
...
#Autowired ApplicationContext ctx;
#Before
public void setUp() {
studentRepository = ctx.getAutowireCapableBeanFactory()
.createBean(StudentRepository.class);
...
}
...
}