dimanche 24 janvier 2021

Spring : SuitClasses is capable of run webmvctests into the suit?

I started to test my spring web-application (rest microservice) and I've three different test classes

  1. repository tests
  2. service layer tests
  3. web layer tests

The two first ones runs the whole context application, but the third one just runs the necessary aspects to test the web-layer. My intention was put all of those classes to be executed during compilation phase, but even using @SuitClasses annotation the unique class that resist to initiate is the web-layer test class.

Would you have any idea about what is happening ?

My first class

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
/**
 * Class responsible for repository testing
 * @author Cleiton
 *
 */
public class CustomerRepositoryTests {
    
    private static final String EXISTANT_EMAIL = "patricia.pilar@uol.com.br";
    private static final String MISSING_EMAIL = "maurice.dolly@ie.com";
    private static final String EXISTANT_EXTERNAL_ID = "abcT10@#";
    private static final String MISSING_EXTERNAL_ID = "SD2sdc13$";
    private static final Long EXISTANT_CUSTOMER_ID = 7L;
    private static final Long NON_EXISTANT_CUSTOMER_ID = 12000L;
    
    @Autowired
    private CustomerRepository repository;
    
    /**
     * Method responsible for check if repository if able to find a stored email
     */
    @Test
    public void findCustomerByEmailOkTest() {
        Optional<CustomerEntity> customer = Optional.ofNullable(repository.findCustomerByEmail(EXISTANT_EMAIL));
        assertTrue(customer.isPresent());
    }

My second class

@RunWith(SpringRunner.class)
@ExtendWith(MockitoExtension.class)
@SpringBootTest
@ActiveProfiles("test")
public class CustomerOperationsTest {
    
    private static final String DUPLICATED_NAME = "Marta";
    
    private static final String DUPLICATED_SURNAME = "Gum";
    
    private static final String DUPLICATED_PASSWORD = "figL@456";
    
    private static final String DUPLICATED_EMAIL = "gum_marta@pt.com";
    
    private static final String DISTINCT_EMAIL = "gum_alternative@pt.com";

    @Autowired
    private CustomerOperations service;
    
    
    @Test(expected = EmailAlreadyRegisteredException.class)
    public void verifyEmailDuplicatedOnCreateUserTest() {
        CustomerInputDTO input = createDuplicatedUser();
        service.createCustomer(input);
    }
    
    @Test
    public void createNewUserTest() {
        CustomerInputDTO input = createDuplicatedUser();
        input.setEmail(DISTINCT_EMAIL);
        CustomerOutputDTO dto = service.createCustomer(input);
        assertTrue(dto != null);
    }
    

My Third class (the one with the problem)

WebMvcTest
@ActiveProfiles(value = "webtest")
@AutoConfigureTestDatabase
public class MoneySaverControllerTests {
    
    private static final String EMAIL = "defaultUser@email.com";
    
    private static final String NAME = "daniel";
    
    private static final String SURNAME = "houston";
    
    private static final String PASSWORD =  "b76lG@23";

    private static final String MISSING_AT_EMAIL = "fulanoemail.com";
    
    private static final String MISSING_SUFFIX_EMAIL = "fulano@email";
    
    private static final String INVALID_EMAIL_ERROR_ID = "customer.input.email.invalid";
    
    private static final String NAME_MAX_LENGTH_ERROR_ID = "customer.input.name.maxlength";
    
    private static final String SURNAME_MAX_LENGTH_ERROR_ID = "customer.input.name.maxlength";
    
    private static final String NAME_NOT_BLANK_ERROR_ID = "customer.input.name.notblank";
    private static final String SURNAME_NOT_BLANK_ERROR_ID = "customer.input.surname.notblank";
    private static final String EMAIL_NOT_BLANK_ERROR_ID = "customer.input.email.notblank";
    private static final String PASSWORD_NOT_BLANK_ERROR_ID = "customer.input.password.notblank";
    
    private static final String LONGER_USER_NAME = "MY NAME IS REALLY HUGE. I AM TOTALLY SURE ABOUT IT";
    
    private static final String LONGER_USER_SURNAME = "PROBABLY MY NAME IS THE LARGEST NAME IN THE WORLD,MAYBE AN OLD AND LOYAL NAME, WHAT DO YOU THINK ABOUT IT ?";
    
    private static final String BLANK_STRING = " ";
    
    
    
    @Autowired
    private MockMvc mockMvc;
    
    @Autowired
    private ObjectMapper mapper;
    
    @MockBean
    private PostOfficer po;
    
    @MockBean
    private CustomerOperations customerOperations;
    
    @MockBean
    private CustomerIOConverter converter;
    
    @MockBean
    private DataSource dataSource;
    
    @Test
    public void createUserWithAllFieldsFilledTest() throws JsonProcessingException, Exception {

        CustomerInputDTO input = getFilledCustomerInputDTO();       

       mockMvc.perform(post("/customer")
            .contentType("application/json")
            .content(mapper.writeValueAsString(input)))
            .andExpect(status().isOk());
    }
    

My ApplicationContextTestClass

@RunWith(Suite.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
@ActiveProfiles({"test", "webtest"}) 
@Suite.SuiteClasses(value = { CustomerOperationsTest.class,MoneySaverControllerTests.class,CustomerRepositoryTests.class })
public class MoneySaverApplicationTests {
    
    @Test
    public void contextLoads() {
    }
    
}

And finally... a picture with the only two test classes executed Executed Tests

Aucun commentaire:

Enregistrer un commentaire