This article mainly introduces relevant information about the Spring Test method example code under Java Config. Friends in need can refer to
Spring Test method under Java Config
Three methods are used:
1. Purely manual bean fetching:
package com.wang.test; import com.marsmother.commission.core.config.MapperConfig; import com.marsmother.commission.core.config.PropertyConfig; import com.marsmother.commission.core.config.ServiceConfig; import com.marsmother.commission.core.dto.GeneralResponseData; import com.marsmother.commission.core.service.UserService; import com.marsmother.commission.site.config.SecurityConfig; import org.junit.Before; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * Created by Wanglei on 15/10/29. */ public class CustomeTest { private static AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); @Before public void tearUp(){ context.register(PropertyConfig.class); context.register(ServiceConfig.class); context.register(SecurityConfig.class); context.register(MapperConfig.class); context.refresh(); } @Test public void testUser(){ UserService userService = context.getBean(UserService.class); Long userId = 3L; GeneralResponseData data = userService.addUserRelation(userId); System.out.println(data.getMsg()); } }
2. Use spring-test
package com.wang.test; import com.marsmother.commission.core.config.MapperConfig; import com.marsmother.commission.core.config.PropertyConfig; import com.marsmother.commission.core.config.ServiceConfig; import com.marsmother.commission.core.dto.GeneralResponseData; import com.marsmother.commission.core.service.UserService; import com.marsmother.commission.site.config.SecurityConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * Created by Wanglei on 15/10/29. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {PropertyConfig.class, ServiceConfig.class, SecurityConfig.class, MapperConfig.class}) public class SpringTest { @Autowired private UserService userService; @Test public void testUser(){ GeneralResponseData data= userService.addUserRelation(3L); System.out.println(data.getMsg()); } }
3. Use Mockito
You need to introduce the corresponding package:
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.9.5</version> <scope>test</scope> </dependency>
package com.wang.test; import com.marsmother.commission.core.dto.GeneralResponseData; import com.marsmother.commission.core.presistence.FollowNumberMapper; import com.marsmother.commission.core.presistence.UserMapper; import com.marsmother.commission.core.presistence.UserRelationMapper; import com.marsmother.commission.core.service.UserService; import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; /** * Created by Wanglei on 15/10/29. */ public class TestUserService { @InjectMocks private UserService userService; @Mock private FollowNumberMapper followNumberMapper; @Mock private UserMapper userMapper; @Mock private UserRelationMapper userRelationMapper; @Before public void init(){ MockitoAnnotations.initMocks(this); } @Test public void testUser(){ Long userId = 3L; GeneralResponseData result = userService.addUserRelation(userId); System.out.println(result.getMsg()); } }
Here@Mock If so, it will not actually perform database operations.
There is another usage called @Spy. I don’t know the specific usage yet and need to study it.
In comparison, spring-test is more standard.
【Related Recommendations】
3. Comprehensive access to Java annotations
The above is the detailed content of Introduction to various methods of Spring Test in Config. For more information, please follow other related articles on the PHP Chinese website!