0
I would like to have several exception classes in this code snippet below, but I’m only able to put one, as I do to put several exception classes?
@Test(expected = NameProjectEmployeeCadastradoException.class)
I tried like this, it didn’t work;
@Test(expected = NameProjectEmployeeCadastradoException.class, NameProjectCadastradoException.class )
Watch the full java code.
@RunWith(SpringRunner.class)
@DataJpaTest
@TestPropertySource("classpath:application.properties")
@ContextConfiguration(classes = {AppConfig.class})
public class AdicionarProjetoSucessApplicationTest {
@Autowired
private ProjectService projectService;
@Autowired
private EmployeeRepository employeeRepository;
@Autowired
private EmployeeProjectService employeeProjectService;
@Test(expected = NameProjectEmployeeCadastradoException.class)
public void testAdicionarProject() throws Exception{
Project project1 = new Project("Google");
this.projectService.salvar(project1);
Assertions.assertThat(project1.getName()).isNotNull();
Project project2 = new Project("TOTVS");
this.projectService.salvar(project2);
Assertions.assertThat(project2.getName()).isNotNull();
Project project3 = new Project("Microsoft");
this.projectService.salvar(project3);
Assertions.assertThat(project3.getName()).isNotNull();
Project project4 = new Project("TOVOS");
this.projectService.salvar(project4);
Assertions.assertThat(project4.getName()).isNotNull();
Employee employee1 = new Employee("Douglas", BigDecimal.valueOf(16556));
this.employeeRepository.save(employee1);
Assertions.assertThat(employee1.getName()).isNotNull();
Assertions.assertThat(employee1.getSalary()).isNotNull();
Employee employee2 = new Employee("Paulo", BigDecimal.valueOf(16556));
this.employeeRepository.save(employee2);
Assertions.assertThat(employee2.getName()).isNotNull();
Assertions.assertThat(employee2.getSalary()).isNotNull();
Employee employee3 = new Employee("José", BigDecimal.valueOf(16556));
this.employeeRepository.save(employee3);
Assertions.assertThat(employee3.getName()).isNotNull();
Assertions.assertThat(employee3.getSalary()).isNotNull();
Employee employee4 = new Employee("Maria", BigDecimal.valueOf(16556));
this.employeeRepository.save(employee4);
Assertions.assertThat(employee4.getName()).isNotNull();
Assertions.assertThat(employee4.getSalary()).isNotNull();
EmployeeProject employeeProject1 = new EmployeeProject(1, 1);
this.employeeProjectService.salvar(employeeProject1);
Assertions.assertThat(employeeProject1.getEmp_id());
Assertions.assertThat(employeeProject1.getProj_id());
EmployeeProject employeeProject2 = new EmployeeProject(1, 1);
this.employeeProjectService.salvar(employeeProject2);
Assertions.assertThat(employeeProject2.getEmp_id());
Assertions.assertThat(employeeProject2.getProj_id());
EmployeeProject employeeProject3 = new EmployeeProject(1, 1);
this.employeeProjectService.salvar(employeeProject3);
Assertions.assertThat(employeeProject3.getEmp_id());
Assertions.assertThat(employeeProject3.getProj_id());
EmployeeProject employeeProject4 = new EmployeeProject(1, 1);
this.employeeProjectService.salvar(employeeProject4);
Assertions.assertThat(employeeProject4.getEmp_id());
Assertions.assertThat(employeeProject4.getProj_id());
}
}
This parameter only supports 1 exception. I see two solutions: 1) do not use the
expected
rather usetry/catch
within the test method, giving aassertTrue
in case the execution falls into the exception (which is what you want to test, right? ); 2) create a "parent" exception, make the exceptions you want to test inherit that parent, and finallyexpected
you pass this parent exception as parameter.– StatelessDev
@Statelessdev, provide a response from your comment :)
– Dherik