10 hours
- Medium
Free online content available in this course.
course.header.alt.is_video
course.header.alt.is_certifying
Got it!Last updated on 1/4/21
Using test automation practices to write effective unit tests
Evaluated skills
- Write effective unit tests by using test automation best practices
Description
A quiz covering the testing pyramid, basic JUNit unit test structure, annotations, Hamcrest matchers, and interpreting failing tests.
Question 1
Which of the following statements are true of TDD?
Careful, there are several correct answers.You to start by writing a failing test before you write your code.
You start by implementing just enough code to begin writing a test.
Typically, you should write multiple tests for each method.
Typically, you should write one unit test per method.
Question 2
Imagine you're working in a small team and a colleague shows you some of their code and review comments. First, you look at the class under test, called Person:
public class Person {private String firstName;private String lastName;public Person(String firstName, String lastName) {this.firstName = firstName;this.lastName = lastName;}public String getFullName() {return String.format("%s %s", firstName, lastName);}}Your colleague shows you their JUnit 4 unit test for this Person class :
import Person;import org.junit.*;@RunWith(BlockJUnit4ClassRunner.class)public class PersonTest {// check that person.getFullName() workspublic static void getFullName_shouldReturnTheFirstAndLastName_whenAPersonHasBoth() {// ArrangeString expected = "Katherine Janelay";Person personUnderTest = new Person("Katherine", "Janelay");// ActString fullName = person.getFullName();// AssertBoolean testIsSuccessful = fullName.equals(expected);assert( testIsSuccessful );System.out.println("Passed: " + testIsSuccessful );}public static void main(String[] args) {getFullName_shouldReturnTheFirstAndLastName_whenAPersonHasBoth();}}Which of the following comments about this code are correct?
Careful, there are several correct answers.The runner class should be replaced with
JUnitRunner.class
.The
@Test
annotation is missing.The assertion method is not suited for testing.
The tests shouldn't be called from the
main(String[] args)
.
Question 3
What is red-green-refactoring?
A technique to mark code via highlighting. Poor quality code is marked in red, and once fixed, should be marked in green for the code reviewer to check. Fixing your code is called refactoring.
A technique where you start with a failing test in red, then write code to make it pass to green. You can then improve and modify the code while checking if your changes pass the test via refactoring.
A testing library that enables you to highlight failing tests in red and passing tests in green. You begin refactoring your code in the failing tests until it turns green.
A design pattern for a factory class which can start your tests (green) and stop you them (red) so you can take the time to refactor them.