Injecting @Autowired private field during testing

后端 未结 6 1049
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 17:08

I have a component setup that is essentially a launcher for an application. It is configured like so:

@Component
public class MyLauncher {
    @Autowired
            


        
6条回答
  •  既然无缘
    2020-11-29 17:22

    I'm a new user for Spring. I found a different solution for this. Using reflection and making public necessary fields and assign mock objects.

    This is my auth controller and it has some Autowired private properties.

    @RestController
    public class AuthController {
    
        @Autowired
        private UsersDAOInterface usersDao;
    
        @Autowired
        private TokensDAOInterface tokensDao;
    
        @RequestMapping(path = "/auth/getToken", method = RequestMethod.POST)
        public @ResponseBody Object getToken(@RequestParam String username,
                @RequestParam String password) {
            User user = usersDao.getLoginUser(username, password);
    
            if (user == null)
                return new ErrorResult("Kullanıcıadı veya şifre hatalı");
    
            Token token = new Token();
            token.setTokenId("aergaerg");
            token.setUserId(1);
            token.setInsertDatetime(new Date());
            return token;
        }
    }
    

    And this is my Junit test for AuthController. I'm making public needed private properties and assign mock objects to them and rock :)

    public class AuthControllerTest {
    
        @Test
        public void getToken() {
            try {
                UsersDAO mockUsersDao = mock(UsersDAO.class);
                TokensDAO mockTokensDao = mock(TokensDAO.class);
    
                User dummyUser = new User();
                dummyUser.setId(10);
                dummyUser.setUsername("nixarsoft");
                dummyUser.setTopId(0);
    
                when(mockUsersDao.getLoginUser(Matchers.anyString(), Matchers.anyString())) //
                        .thenReturn(dummyUser);
    
                AuthController ctrl = new AuthController();
    
                Field usersDaoField = ctrl.getClass().getDeclaredField("usersDao");
                usersDaoField.setAccessible(true);
                usersDaoField.set(ctrl, mockUsersDao);
    
                Field tokensDaoField = ctrl.getClass().getDeclaredField("tokensDao");
                tokensDaoField.setAccessible(true);
                tokensDaoField.set(ctrl, mockTokensDao);
    
                Token t = (Token) ctrl.getToken("test", "aergaeg");
    
                Assert.assertNotNull(t);
    
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    
    }
    

    I don't know advantages and disadvantages for this way but this is working. This technic has a little bit more code but these codes can be seperated by different methods etc. There are more good answers for this question but I want to point to different solution. Sorry for my bad english. Have a good java to everybody :)

提交回复
热议问题