samedi 22 décembre 2018

My controller is working in runtime, but the mockkmvc test is failing for a dependency that is not even in the class

When I run the test, I get a dependency error of UserService is not able to be found to be injected. This is weird because no where in ConstantsController.java did I use UserService. Also, UserService is label properly with @Service annotation.

I have tried using the @MockBean annotation in the controller test class. It gave me unidentifiable errors. I even tried autowiring the bean in the configuration because the log said define bean of type of UserService in configuration. Still no luck.

UserService

package com.GMorgan.RateMyFriendv5.Service;

import com.GMorgan.RateMyFriendv5.Entitiy.Role;
import com.GMorgan.RateMyFriendv5.Entitiy.User;
import com.GMorgan.RateMyFriendv5.Repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserService {
    private UserRepository repository;

    public boolean login(String username, String password) {
        List<User> userList = repository.findByUsername(username);
        boolean isSuccessful = userList.get(0).isPassword(password);
        log.info("Username: {} isSucessful: {}", username, isSuccessful);
        return isSuccessful;
    }

     public boolean signUp(String email, String username, String password) {
        if (userEmailExists(email) || userUsernameExists(username)) return false;
        User user = new User();
        user.setEmail(email);
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(Role.USER);
        repository.save(user);
        log.info("User email: {} username: {}", email, username);
        return true;
     }

    public boolean userEmailExists(String email) {
         return !repository.findByEmail(email).isEmpty();
    }

    public boolean userUsernameExists(String username) {
        return !repository.findByUsername(username).isEmpty();
    }
}

ConstantsController.java

package com.GMorgan.RateMyFriendv5.Controller;

import com.GMorgan.RateMyFriendv5.Utils.Mappings;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConstantsController {

    @Value("${controller.constant.ping.message}")
    public String pingMessage;

    @RequestMapping(Mappings.PING)
    public String ping() {
        return pingMessage;
    } 
}

ConstantsControllerTest

@RunWith(SpringRunner.class)
@WebMvcTest
@AutoConfigureMockMvc
public class ConstantsControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @Value("${controller.constant.ping.message}")
    public String pingMessage;

    @Test
    public void pingTest() throws Exception {
this.mockMvc.perform(get(Mappings.PING)).andDo(print()).andExpect(status().isOk())
                .andExpect(content().string(containsString(pingMessage)));
    }
}

I want the test to pass. In runtime, when I go to the link, I see the ping message. I want the test to do so as well.

Aucun commentaire:

Enregistrer un commentaire