SHARE
TWEET

Untitled

a guest May 27th, 2018 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package rs.ac.uns.ftn.ktsnvt.service.unit;
  2.  
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.boot.test.mock.mockito.MockBean;
  9. import org.springframework.test.context.junit4.SpringRunner;
  10. import rs.ac.uns.ftn.ktsnvt.exception.BadRequestException;
  11. import rs.ac.uns.ftn.ktsnvt.exception.NotFoundException;
  12. import rs.ac.uns.ftn.ktsnvt.model.entity.Account;
  13. import rs.ac.uns.ftn.ktsnvt.repository.AccountRepository;
  14. import rs.ac.uns.ftn.ktsnvt.service.AccountService;
  15.  
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.mockito.BDDMockito.given;
  19.  
  20. /**
  21.  * Created by Katarina Cukurov on 30/11/2017.
  22.  */
  23. @RunWith(SpringRunner.class)
  24. @SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
  25. public class AccountServiceUnitTest {
  26.  
  27.     @Autowired
  28.     private AccountService accountService;
  29.  
  30.     @MockBean
  31.     private AccountRepository accountRepository;
  32.  
  33.  
  34.     @Before
  35.     public void setUp() {
  36.         Account account = new Account("kaca", "kaca");
  37.  
  38.         given(
  39.                 this.accountRepository.findOne(new Long(100))
  40.         ).willReturn(
  41.                 account
  42.         );
  43.  
  44.         given(
  45.                 this.accountRepository.findOne(new Long(200))
  46.         ).willReturn(
  47.                 null
  48.         );
  49.  
  50.         given(
  51.                 this.accountRepository.findByUsername("kaca")
  52.         ).willReturn(
  53.                 account
  54.         );
  55.  
  56.         given(
  57.                 this.accountRepository.findByUsername("acak")
  58.         ).willReturn(
  59.                 null
  60.         );
  61.  
  62.     }
  63.  
  64.     @Test
  65.     public void testFindOne() {
  66.         /*
  67.          * Test proverava ispravnost rada metode findOne servisa AccountService
  68.          * (dobavljanje naloga na osnovu ID-a)
  69.          * u slucaju kada postoji nalog sa zadatim ID-em
  70.          * */
  71.  
  72.         /*
  73.         nalog sa ID-em = 100 ima podatke:
  74.             id = 100,
  75.             username = kaca,
  76.             password = kaca ($2a$10$FXALP0p8VlmzCbbCTDTV8uIS46lpCbCpeIq74qulCyxzhCsOazAZS)
  77.          */
  78.         Account account = this.accountService.findOne(new Long(100));
  79.         assertNotNull(account);
  80.         assertEquals("kaca", account.getUsername());
  81.     }
  82.  
  83.     @Test(expected = NotFoundException.class)
  84.     public void testFindOneNonexistent() {
  85.         /*
  86.          * Test proverava ispravnost rada metode findOne servisa AccountService
  87.          * (dobavljanje naloga na osnovu ID-a)
  88.          * u slucaju kada ne postoji nalog sa zadatim ID-em - ocekivano NotFoundException
  89.          * */
  90.  
  91.         // account sa ID-em 200 ne postoji
  92.         Account account = this.accountService.findOne(new Long(200));
  93.     }
  94.  
  95.     @Test
  96.     public void testFindByUsername() {
  97.         /*
  98.          * Test proverava ispravnost rada metode findByUsername servisa AccountService
  99.          * (dobavljanje naloga na osnovu korisnickog imena)
  100.          * u slucaju kada postoji nalog sa zadatim korisnickim imenom
  101.          * */
  102.  
  103.         /*
  104.         nalog sa korisnickim imenom "kaca" ima od podataka:
  105.             id = 100,
  106.             username = kaca,
  107.             password = kaca
  108.         */
  109.         Account account = this.accountService.findByUsername("kaca");
  110.         assertNotNull(account);
  111.         assertEquals("kaca", account.getUsername());
  112.     }
  113.  
  114.     @Test(expected = NotFoundException.class)
  115.     public void testFindByUsernameNonexistent() {
  116.         /*
  117.          * Test proverava ispravnost rada metode findByUsername servisa AccountService
  118.          * (dobavljanje naloga na osnovu korisnickog imena)
  119.          * u slucaju kada ne postoji nalog sa zadatim korisnickim imenom - ocekivano NotFoundException
  120.          * */
  121.  
  122.         // ne postoji account sa korisnickim imenom acak
  123.         Account account = this.accountService.findByUsername("acak");
  124.     }
  125.  
  126.     @Test(expected = BadRequestException.class)
  127.     public void testCheckUsername(){
  128.          /*
  129.          * Test proverava ispravnost rada metode checkUsername servisa AccountService
  130.          * (provera da li je vec zauzeto neko korisnicko ime)
  131.          * u slucaju kada postoji nalog sa zadatim korisnickim imenom - ocekivano BadRequestException
  132.          * */
  133.  
  134.         this.accountService.checkUsername("kaca");
  135.     }
  136. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top