Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RunWith(AndroidJUnit4.class)
- @FixMethodOrder(MethodSorters.NAME_ASCENDING)
- public class TestLogin {
- @Rule
- public InstantTaskExecutorRule mInstantTaskExecutorRule = new InstantTaskExecutorRule();
- @Rule
- public ActivityTestRule<LoginActivity> activityTestRule = new ActivityTestRule<>(LoginActivity.class, true, false);
- @Rule
- public MockWebServerTestRule mockWebServerTestRule = new MockWebServerTestRule();
- @Mock
- Application application;
- LoginViewModel loginViewModel;
- AppDatabase appDatabase;
- AuthenticationDao authenticationDao;
- Authentication authentication;
- @Before
- public void setUp() throws Exception {
- MockitoAnnotations.initMocks(this);
- loginViewModel = new LoginViewModel(application);
- ApiUrls.TOKEN = mockWebServerTestRule.mockWebServer.url("/").toString();
- appDatabase = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(),
- AppDatabase.class).build();
- authenticationDao = appDatabase.authenticationDao();
- activityTestRule = new ActivityTestRule<>(LoginActivity.class, true, true);
- String fileName = "valid_login_response.json";
- mockWebServerTestRule.mockWebServer.enqueue(new MockResponse()
- .setBody(RestServiceTestHelper.getStringFromFile(getContext(), fileName))
- .setResponseCode(HttpURLConnection.HTTP_OK));
- Intent intent = new Intent();
- activityTestRule.launchActivity(intent);
- loginViewModel.userName.postValue("Elon");
- loginViewModel.password.postValue("Musk123");
- loginViewModel.getAuthenticateTokenData();
- mockWebServerTestRule.mockWebServer.takeRequest();
- }
- @Test
- public void a_testDbEntryOnValidResponse() {
- authentication = authenticationDao.getAuthInformation();
- String issueTime = authentication.getIssueDateTime();
- String expirationTime = authentication.getExpireDateTime();
- String refreshToken = authentication.getRefreshToken();
- Assert.assertEquals("Tue, 16 Apr 2019 10:39:20 GMT", issueTime);
- Assert.assertEquals("Tue, 16 Apr 2019 10:54:20 GMT", expirationTime);
- Assert.assertEquals("e2b4dfd7205587745aa3100af9a0b", refreshToken);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement