package org.aplas.myshop; import static org.junit.Assert.assertNotNull; import androidx.appcompat.widget.AppCompatButton; import androidx.lifecycle.Lifecycle; import androidx.test.core.app.ActivityScenario; import org.junit.After; import org.junit.Before; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import com.google.android.material.textfield.TextInputEditText; import com.google.android.material.textfield.TextInputLayout; @RunWith(RobolectricTestRunner.class) @Config(manifest= Config.NONE) @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class TestAddProductFail extends ViewTest{ ActivityScenario scenario; private TextInputLayout namaLayout, priceLayout; private TextInputEditText nama, price; private AppCompatButton addButton; @Before public void setUp() { scenario = ActivityScenario.launch(AddProductActivity.class); scenario.moveToState(Lifecycle.State.CREATED); scenario.onActivity(activity -> { namaLayout = activity.findViewById(R.id.textInputLayoutProductName); priceLayout = activity.findViewById(R.id.textInputLayoutProductPrice); nama = activity.findViewById(R.id.textInputEditTextProductName); price = activity.findViewById(R.id.textInputEditTextProductPrice); addButton = activity.findViewById(R.id.appCompatButtonAddProduct); }); } @After public void tearDown() { scenario.close(); } @Test public void test_01_emptyName() { scenario.onActivity(activity -> { nama.setText(""); price.setText("100000"); addButton.performClick(); String expectedErrorMessage = "Enter Product Name"; assertNotNull("The text layout should throw error when product name is empty, please check your input validation inside AddProductActivity and make sure it has the same error message according to guide file", namaLayout.getError()); String actualErrorMessage = namaLayout.getError().toString(); testItem(expectedErrorMessage, actualErrorMessage, "Error message does not match, please check your input validation inside AddProductActivity and make sure it has the same error message according to guide file", 1); }); } @Test public void test_02_emptyPrice() { scenario.onActivity(activity -> { nama.setText("Product"); price.setText(""); addButton.performClick(); String expectedErrorMessage = "Enter Product Price"; assertNotNull("The text layout should throw error when product price is empty, please check your input validation inside AddProductActivity and make sure it has the same error message according to guide file", priceLayout.getError()); String actualErrorMessage = priceLayout.getError().toString(); testItem(expectedErrorMessage, actualErrorMessage, "Error message does not match, please check your input validation inside AddProductActivity and make sure it has the same error message according to guide file", 1); }); } }