Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. @RunWith(AndroidJUnit4.class)
  2. @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  3. public class TestLogin {
  4.  
  5. @Rule
  6. public InstantTaskExecutorRule mInstantTaskExecutorRule = new InstantTaskExecutorRule();
  7. @Rule
  8. public ActivityTestRule<LoginActivity> activityTestRule = new ActivityTestRule<>(LoginActivity.class, true, false);
  9. @Rule
  10. public MockWebServerTestRule mockWebServerTestRule = new MockWebServerTestRule();
  11. @Mock
  12. Application application;
  13. LoginViewModel loginViewModel;
  14. AppDatabase appDatabase;
  15. AuthenticationDao authenticationDao;
  16. Authentication authentication;
  17.  
  18. @Before
  19. public void setUp() throws Exception {
  20. MockitoAnnotations.initMocks(this);
  21. loginViewModel = new LoginViewModel(application);
  22. ApiUrls.TOKEN = mockWebServerTestRule.mockWebServer.url("/").toString();
  23.  
  24. appDatabase = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(),
  25. AppDatabase.class).build();
  26. authenticationDao = appDatabase.authenticationDao();
  27.  
  28. activityTestRule = new ActivityTestRule<>(LoginActivity.class, true, true);
  29. String fileName = "valid_login_response.json";
  30. mockWebServerTestRule.mockWebServer.enqueue(new MockResponse()
  31. .setBody(RestServiceTestHelper.getStringFromFile(getContext(), fileName))
  32. .setResponseCode(HttpURLConnection.HTTP_OK));
  33.  
  34. Intent intent = new Intent();
  35. activityTestRule.launchActivity(intent);
  36.  
  37. loginViewModel.userName.postValue("Elon");
  38. loginViewModel.password.postValue("Musk123");
  39. loginViewModel.getAuthenticateTokenData();
  40. mockWebServerTestRule.mockWebServer.takeRequest();
  41.  
  42.  
  43. }
  44.  
  45. @Test
  46. public void a_testDbEntryOnValidResponse() {
  47. authentication = authenticationDao.getAuthInformation();
  48.  
  49. String issueTime = authentication.getIssueDateTime();
  50. String expirationTime = authentication.getExpireDateTime();
  51. String refreshToken = authentication.getRefreshToken();
  52.  
  53. Assert.assertEquals("Tue, 16 Apr 2019 10:39:20 GMT", issueTime);
  54. Assert.assertEquals("Tue, 16 Apr 2019 10:54:20 GMT", expirationTime);
  55. Assert.assertEquals("e2b4dfd7205587745aa3100af9a0b", refreshToken);
  56.  
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement