miércoles, 24 de mayo de 2017

The way to implements Unit Tests in Spring Boot

For doing Tests in Spring Boot you have to know the next things:

- You need a java test and resources test directories in your project. In the resources directory you put the files for the src/test/java and the src/test/resources directory the test you want to pass when you do the maven build.
- You need am application-TEST.yml file in your resources directory for configure the test calls.
- When you implements a Test you have to use the next anotations before the class name:

@org.junit.runner.RunWith(SpringJUnit4ClassRunner.class)
@org.springframework.boot.test.SpringApplicationConfiguration(classes = ClassName.class)
@org.springframework.test.context.ActiveProfiles(value = "TEST")

- For test a Service implemented in you src/main/java directory you have to indicate this way:

@org.springframework.beans.factory.annotation.Autowired
MyService service;

- At last, you need to indicate the method that test one call like:

@org.junit.Test
public void test() {
    ... do the test and finally verify the result of the test
    assertThat(result).isEqualTo("OK");
}

Here you have implemented your first test.

For call it, you have to run the maven test option.

For skip one test, you can indicate @org.junit.Ignore and the test don't will be executed.

For skip all test, you can indicate to maven that don't want to execute test this way:

Go to run configuration and check the option skip tests.

Any question of this tip, don't doubt ask me!

Try it and have fun!

No hay comentarios:

Publicar un comentario