'how to write junit for this code service layer in springboot
i am new in junit
I want to test this method with junit5 when i try to test this custom exception like this:
CustomExceptionMessage exception = assertThrows(CustomExceptionMessage.class,() -> when(accountJpaRepository.existsByAccountName(accountModel.getAccountName())).thenReturn(true)
);
assertEquals(Constant.ACCOUNT_EXIST,exception.getMessage());
how can we test this method properly?
//here is service layer method
@Override
public AccountModel addAccount(final AccountModel accountModel) {
String firstFourChars = "";
if (accountModel.getCompanyName().length() > 4) {
firstFourChars = accountModel.getCompanyName().substring(0, 4);
} else {
firstFourChars = accountModel.getCompanyName();
}
String lastFiveDigits = "";
if (accountModel.getPhone().length() > 5) {
lastFiveDigits = accountModel.getPhone().substring(0, 5);
} else {
lastFiveDigits = accountModel.getPhone();
}
String acountName = firstFourChars.concat(lastFiveDigits);
accountModel.setAccountName(acountName);
if (accountJpaRepository.existsByAccountName(accountModel.getAccountName())) {
throw new CustomExceptionMessage(Constant.ACCOUNT_EXIST);
}
if (accountJpaRepository.existsByEmail(accountModel.getEmail())) {
throw new CustomExceptionMessage(Constant.EMAIL_EXIST);
}
return accountRepository.addAccount(accountModel);
}
Solution 1:[1]
Actually, I think you were almost there :) This would be my take on this problem:
@Test
void test() {
var accountModel = new AccountModel(...);
var accountJpaRepository = mock(AccountJpaRepository.class);
when(accountJpaRepository.existsByAccountName(accountModel.getAccountName())).thenReturn(true);
var cut = new YourUnnamedService(accountJpaRepository)
var exception = assertThrows(CustomExceptionMessage.class, () ->
cut.addAccount(accountModel);
);
assertEquals(Constant.ACCOUNT_EXIST,exception.getMessage());
}
The first thing to do is to create an AccountModel
. This will be used for the mocking of the AccountJpaRepository
and as the parameter for your addAccount
Method.
The next two lines mock the behavior of your AccountJpaRepository
. This is done to isolate your YourUnnamedService
from its dependencies.
After this preparation, you instantiate YourUnnamedService
and pass all its dependencies into its constructor.
Inside JUnits assertThrows
you just have to call the method you want to test, and make your assertions afterward.
Solution 2:[2]
this is how i run the code , that actualy work for me.
private AccountModel getAccountModelObject() {
AccountModel accountModel = new AccountModel();
accountModel .setAccountId((long) 1);
accountModel .setAccountOwner("Dinesh Singh");
accountModel .setAccountName("Dinesh");
return accountModel ;
}
@Test
void testAddAccountMethodWithAccountExistException() {
AccountModel getAccountModel = getAccountModelObject();
when(accountJpaRepository.existsByAccountName(
getAccountModel.getAccountName())).thenReturn(true);
CustomExceptionMessage exception =
assertThrows(CustomExceptionMessage.class,
() -> accountServiceImpl.addAccount(getAccountModel));
assertEquals(Constant.ACCOUNT_EXIST, exception.getMessage());
}
firstly we need to provide mock value for AccountModel class , then true that condition that throw exception ,now for check exception we use asssertThrows ,in this we passed the own custom exception class as a first parameter that we use in our service class code ,then in second parameter as a lambda expression we call service class method . then we check that returned exception message is equal to our exception message ,if it equal then code work fine
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jan Schmitz |
Solution 2 |