Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit tests Landon Cox April 25, 2017.

Similar presentations


Presentation on theme: "Unit tests Landon Cox April 25, 2017."— Presentation transcript:

1 Unit tests Landon Cox April 25, 2017

2 Things covered this semester
Design patterns (code structure) Decorator Open/close principle APIs (external libraries, services) Firebase SQLite Volley Project management tools Android studio git What’s missing?

3 Topics this semester Design patterns (code structure)
Decorator Open/close principle APIs (external libraries, services) Firebase SQLite Volley Project management tools Android studio git What’s missing? Testing!

4 Unit testing Unit tests are small Run tests at build time
Exercise a single method or class Test behavior in isolation from rest of program Run tests at build time Should be integrated into gradle Sanity check any changes you make Fail a unit test? Fail the build.

5 Unit testing in Android
Local unit tests Run locally on your machine Cannot depend on a launched app Instrumented unit tests Run on an emulator or device Have access to Context object

6 Unit testing in Android
Local unit tests Run locally on your machine Cannot depend on a launched app Instrumented unit tests Run on an emulator or device Have access to Context object

7 Example local test import org.junit.Test; import java.util.regex.Pattern; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class ValidatorTest {       public void Validator_Correct Simple_ReturnsTrue() {         is(true));     } }

8 Import stuff from org.junit.*
Example local test Import stuff from org.junit.* import org.junit.Test; import java.util.regex.Pattern; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class ValidatorTest {       public void Validator_Correct Simple_ReturnsTrue() {         is(true));     } }

9 Annotate method w/ @Test
Example local test Annotate method import org.junit.Test; import java.util.regex.Pattern; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class ValidatorTest {       public void Validator_Correct Simple_ReturnsTrue() {         is(true));     } }

10 Use assertions in test method
Example local test Use assertions in test method import org.junit.Test; import java.util.regex.Pattern; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class ValidatorTest {       public void Validator_Correct Simple_ReturnsTrue() {         is(true));     } }

11 Use assertions in test method
Example local test Use assertions in test method import org.junit.Test; import java.util.regex.Pattern; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class ValidatorTest {       public void Validator_Correct Simple_ReturnsTrue() {         is(true));     } }

12 Example local test import org.junit.Test; import java.util.regex.Pattern; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class ValidatorTest {       public void Validator_Correct Simple_ReturnsTrue() {         is(true));     } } These tests are useful for simple, stateless methods, like code that checks valid s.

13 Tests are also pretty limited …
Example local test import org.junit.Test; import java.util.regex.Pattern; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class ValidatorTest {       public void Validator_Correct Simple_ReturnsTrue() {         is(true));     } } Tests are also pretty limited …

14 Unit testing in Android
Local unit tests Run locally on your machine Cannot depend on a launched app Instrumented unit tests Run on an emulator or device Have access to Context object

15 Example instrumented test
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.*; import static org.mockito.Mockito.*; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import android.content.SharedPreferences; @RunWith(MockitoJUnitRunner.class) public class UnitTestSample {     private static final String FAKE_STRING = "HELLO WORLD";       Context mMockContext;       public void readStringFromContext_LocalizedString() { …     } } Import stuff from org.junit.*

16 Example instrumented test
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.*; import static org.mockito.Mockito.*; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import android.content.SharedPreferences; @RunWith(MockitoJUnitRunner.class) public class UnitTestSample {     private static final String FAKE_STRING = "HELLO WORLD";       Context mMockContext;       public void readStringFromContext_LocalizedString() { …     } } Import stuff from org.mockito.*

17 Example instrumented test
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.*; import static org.mockito.Mockito.*; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import android.content.SharedPreferences; @RunWith(MockitoJUnitRunner.class) public class UnitTestSample {     private static final String FAKE_STRING = "HELLO WORLD";       Context mMockContext;       public void readStringFromContext_LocalizedString() { …     } } Import stuff from org.hamcrest.*

18 Example instrumented test
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.*; import static org.mockito.Mockito.*; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import android.content.SharedPreferences; @RunWith(MockitoJUnitRunner.class) public class UnitTestSample {     private static final String FAKE_STRING = "HELLO WORLD";       Context mMockContext;       public void readStringFromContext_LocalizedString() { …     } } Annotate class

19 Example instrumented test
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.*; import static org.mockito.Mockito.*; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import android.content.SharedPreferences; @RunWith(MockitoJUnitRunner.class) public class UnitTestSample {     private static final String FAKE_STRING = "HELLO WORLD";       Context mMockContext;       public void readStringFromContext_LocalizedString() { …     } } Annotate mock objects

20 Example instrumented test
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.*; import static org.mockito.Mockito.*; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import android.content.SharedPreferences; @RunWith(MockitoJUnitRunner.class) public class UnitTestSample {     private static final String FAKE_STRING = "HELLO WORLD";       Context mMockContext;       public void readStringFromContext_LocalizedString() { …     } } Annotate test methods

21 Example instrumented test
… import android.content.SharedPreferences; @RunWith(MockitoJUnitRunner.class) public class UnitTestSample {     private static final String FAKE_STRING = "HELLO WORLD";       Context mMockContext;       public void readStringFromContext_LocalizedString() {         // Given a mocked Context injected into the object under test...         when(mMockContext.getString(R.string.hello_word))                 .thenReturn(FAKE_STRING);         ClassUnderTest myObjectUnderTest = new ClassUnderTest(mMockContext);         // ...when the string is returned from the object under test...         String result = myObjectUnderTest.getHelloWorldString();         // ...then the result should be the expected one.         assertThat(result, is(FAKE_STRING));     } } Define behavior for the mock object

22 Example instrumented test
… import android.content.SharedPreferences; @RunWith(MockitoJUnitRunner.class) public class UnitTestSample {     private static final String FAKE_STRING = "HELLO WORLD";       Context mMockContext;       public void readStringFromContext_LocalizedString() {         // Given a mocked Context injected into the object under test...         when(mMockContext.getString(R.string.hello_word))                 .thenReturn(FAKE_STRING);         ClassUnderTest myObjectUnderTest = new ClassUnderTest(mMockContext);         // ...when the string is returned from the object under test...         String result = myObjectUnderTest.getHelloWorldString();         // ...then the result should be the expected one.         assertThat(result, is(FAKE_STRING));     } } When accessing R.string.hello_world, return “HELLO_WORLD”

23 Example instrumented test
… import android.content.SharedPreferences; @RunWith(MockitoJUnitRunner.class) public class UnitTestSample {     private static final String FAKE_STRING = "HELLO WORLD";       Context mMockContext;       public void readStringFromContext_LocalizedString() {         // Given a mocked Context injected into the object under test...         when(mMockContext.getString(R.string.hello_word))                 .thenReturn(FAKE_STRING);         ClassUnderTest myObjectUnderTest = new ClassUnderTest(mMockContext);         // ...when the string is returned from the object under test...         String result = myObjectUnderTest.getHelloWorldString();         // ...then the result should be the expected one.         assertThat(result, is(FAKE_STRING));     } } Now create an instance of the class you want to test.

24 Example instrumented test
… import android.content.SharedPreferences; @RunWith(MockitoJUnitRunner.class) public class UnitTestSample {     private static final String FAKE_STRING = "HELLO WORLD";       Context mMockContext;       public void readStringFromContext_LocalizedString() {         // Given a mocked Context injected into the object under test...         when(mMockContext.getString(R.string.hello_word))                 .thenReturn(FAKE_STRING);         ClassUnderTest myObjectUnderTest = new ClassUnderTest(mMockContext);         // ...when the string is returned from the object under test...         String result = myObjectUnderTest.getHelloWorldString();         // ...then the result should be the expected one.         assertThat(result, is(FAKE_STRING));     } } Call a method on that instance.

25 Example instrumented test
… import android.content.SharedPreferences; @RunWith(MockitoJUnitRunner.class) public class UnitTestSample {     private static final String FAKE_STRING = "HELLO WORLD";       Context mMockContext;       public void readStringFromContext_LocalizedString() {         // Given a mocked Context injected into the object under test...         when(mMockContext.getString(R.string.hello_word))                 .thenReturn(FAKE_STRING);         ClassUnderTest myObjectUnderTest = new ClassUnderTest(mMockContext);         // ...when the string is returned from the object under test...         String result = myObjectUnderTest.getHelloWorldString();         // ...then the result should be the expected one.         assertThat(result, is(FAKE_STRING));     } } Assert that it behaved as expected


Download ppt "Unit tests Landon Cox April 25, 2017."

Similar presentations


Ads by Google