Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions app/src/main/androidTest/java/CountryFragmentTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import androidx.fragment.app.testing.FragmentScenario;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.espresso.Espresso;
import androidx.test.espresso.assertion.ViewAssertions;
import androidx.test.espresso.matcher.ViewMatchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import androidx.fragment.app.testing.FragmentScenario;
import androidx.test.espresso.Espresso;
import androidx.test.espresso.assertion.ViewAssertions;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class CountryDetailsFragmentTest {

@Test
public void testCountryDetailsDisplayed() {
FragmentScenario.launchInContainer(CountryDetailsFragment.class);

// Assuming the text view with country name has the ID country_name_textview
Espresso.onView(ViewMatchers.withId(R.id.country_name_textview))
.check(ViewAssertions.matches(withText("USA")));
}
}
@RunWith(AndroidJUnit4.class)
public class CountryFragmentTest {

@Test
public void testCountryListDisplayed() {
FragmentScenario.launchInContainer(CountryFragment.class);

// Assuming the RecyclerView for countries has the ID countries_recycler_view
Espresso.onView(ViewMatchers.withId(R.id.countries_recycler_view))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
}
}
73 changes: 73 additions & 0 deletions app/src/main/java/com/example/walmart/WalmartApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ import com.example.walmart.domain.di.ServiceProvider
import com.example.walmart.domain.di.add
import com.example.walmart.domain.di.module
import com.example.walmart.presentation.di.presentationModule
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.List;
import androidx.arch.core.executor.testing.InstantTaskExecutorRule;
import androidx.lifecycle.Observer;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.mockito.Mockito.verify;

class WalmartApplication : Application() {

Expand All @@ -20,4 +35,62 @@ class WalmartApplication : Application() {
module { add { applicationContext } }
)
}

//This test will check the SearchCountryUseCase for different cases:
//Empty search returns all countries.
//Exact match returns the correct country.
//No match returns an empty list.

@RunWith(Parameterized.class)
public class SearchCountryUseCaseTest {

private final String searchQuery;
private final List<String> expectedCountries;

public SearchCountryUseCaseTest(String searchQuery, List<String> expectedCountries) {
this.searchQuery = searchQuery;
this.expectedCountries = expectedCountries;
}

@Parameterized.Parameters
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][]{
{"", Arrays.asList("USA", "Canada", "Mexico")}, // All countries
{"USA", Arrays.asList("USA")}, // One country match
{"xyz", Arrays.asList()}, // No match
});
}

@Test
public void testSearchCountry() {
SearchCountryUseCase useCase = new SearchCountryUseCase();
List<String> result = useCase.searchCountries(searchQuery);
assertEquals(expectedCountries, result);
}
}

public class CountriesViewModelTest {

@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();

private CountriesViewModel viewModel;

@Mock
private Observer<List<String>> observer;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
viewModel = new CountriesViewModel();
viewModel.getCountries().observeForever(observer);
}

@Test
public void testSearch() {
String query = "USA";
viewModel.searchCountries(query);
verify(observer).onChanged(Arrays.asList("USA")); // Expect that search returns USA
}
}
}