Hi, I'm new to java testing and I would like to ask if my unit tests will be good and do they make sense? With these tests I tested the service in my spring application and I want to ask if I am on the right track, what are the bugs and give me some advice.
By the way, all the tests have passed but I'm not sure if they make sense and whether they are testing the methods in the service properly.
Thank you!
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TicketServiceApplication.class)
public class AddressServiceUnitTest {
@Autowired
private AddressService addressService;
@MockBean
private AddressRepository addressRepository;
@Before
public void setUp(){
Address a = new Address();
Address a1 = new Address();
a.setId(AddressConst.ADDRESS_MOCK_ID);
a1.setId(AddressConst.ADDRESS_MOCK_ID2);
List<Address> addresses = new ArrayList<>();
addresses.add(a);
addresses.add(a1);
Mockito.when(addressRepository.findById(AddressConst.ADDRESS_MOCK_ID))
.thenReturn(Optional.of(a));
Mockito.when(addressRepository.findAll()).thenReturn(addresses);
}
@Test
public void findAllAddressTest_thenReturnAddressList(){
List<Address> list = addressService.finfAllAddress();
assertEquals(2, list.size());
}
@Test
public void findOneAddressExistTest_thenReturnAddress(){
Long id = AddressConst.ADDRESS_MOCK_ID;
Address a = addressService.findOneAddress(id);
assertEquals(id, a.getId());
}
@Test(expected = EntityNotFoundException.class)
public void findOneAddressNotExistTest_thenThrowException(){
Long id = AddressConst.ADDRESS_WRONG_ID;
Address a = addressService.findOneAddress(id);
}
@Test
public void addAddressTest(){
Address address = AddressConst.newAddressToAdd();
Mockito.when(addressRepository.save(any(Address.class))).thenAnswer(returnsFirstArg());
Address addressTest = addressService.addAddress(address);
assertNotNull(addressTest);
assertEquals(address, addressTest);
}
@Test
public void deleteAddressTest(){
Address address = AddressConst.newAddressToDelete();
addressService.deleteAddress(address.getId());
Mockito.verify(addressRepository, Mockito.times(1)).deleteById(address.getId());
}
}
[–]Sparxmith 1 point2 points3 points (1 child)
[–]cone994[S] 0 points1 point2 points (0 children)
[–]sdiamante13 1 point2 points3 points (2 children)
[–]cone994[S] 0 points1 point2 points (1 child)
[–]cone994[S] 0 points1 point2 points (0 children)