maven中要导的包: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency>
@RunWith(SpringJUnit4ClassRunner.class) //告诉junit spring配置文件 @ContextConfiguration("classpath:applicationContext.xml") /*@WebAppConfiguration注解是为了应对swagger影响单元测试添加的*/ @WebAppConfiguration public class UserTest { @Autowired private UserService userService; @Test public void getUser() { System.out.println("ininin"); UserInfo userInfo = new UserInfo(); userInfo = userService.getUserByName("告鸟"); System.out.println(userInfo.getUsername()); for(RoleInfo r : userInfo.getRoleInfoList()){ System.out.println(r.getRolename()); } } }