In diesem Artikel werden hauptsächlich relevante Informationen zum Beispielcode der Spring Test-Methode unter Java Config vorgestellt. Freunde, die ihn benötigen, können sich auf
Spring Test-Methode unter Java Config
Drei Methoden verwendet:
1. Bohnen manuell abrufen:
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 Frühlingstest
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. Verwenden Sie Mockito
<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()); } }
Java Kostenloses Video-Tutorial
2. 3.Umfassender Zugriff auf Java-Annotationen
Das obige ist der detaillierte Inhalt vonEinführung in verschiedene Methoden des Spring Test in Config. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!