JUnit

You will use JUnit to run your unit tests. Let's check it.

Here is a class to be tested:

public class JUnitExample {

@Size (min = 6, max = 10,message = "Name should be between 6 and 10
characters")
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

Here is a testing class:

public class JUnitTest {

private static Validator VALIDATOR;

@BeforeClass
public static void setUpClass() {
VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();
}

@Test
public void smallName(){
JUnitExample junit = new JUnitExample();

junit.setName("Name");

Set<ConstraintViolation<JUnitExample>> cv =
VALIDATOR.validate(junit);
assertFalse(cv.isEmpty());
}

@Test
public void validName(){
JUnitExample junit = new JUnitExample();

junit.setName("Valid Name");

Set<ConstraintViolation<JUnitExample>> cv =
VALIDATOR.validate(junit);
assertTrue(cv.isEmpty());
}

@Test
public void invalidName(){
JUnitExample junit = new JUnitExample();

junit.setName("Invalid Name");

Set<ConstraintViolation<JUnitExample>> cv =
VALIDATOR.validate(junit);
assertFalse(cv.isEmpty());
}
}

Whenever you run the building process for this project, the preceding test will be executed and will ensure that those conditions are still valid.

Now you are ready for continuous integration. Just make sure to merge your new and working code into the main branch as soon as possible. Now let's move on to continuous delivery.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.111.197