2015-12-30 74 views
2

我是Junit的新手。最近,我需要为Spring Web项目添加一些测试功能。 因此,我只为测试添加一个新项目。JUnit在春季web项目

首先,我添加了一个用于测试ADServiceImpl的测试用例,下面是我的测试代码。

@Test 
public void test() { 
    ADServiceImpl service = new ADServiceImpl(); 
    UserInfo info = service.getUserInfo("admin", "123456"); 
    assertEquals("Result", "00", info.getStatus().getCode()); 
} 

当我运行测试并得到一个错误是'端点'为空。但'endpoint'由xml配置中的spring @Resource(name =“adEndpoint”)设置。
我该如何处理这个问题?或者还有其他建议春季Web项目测试?非常感谢!

@Service("ADService") 
public class ADServiceImpl implements ADService { 

private final static Logger logger = Logger.getLogger(ADServiceImpl.class); 

@Resource(name = "adEndpoint") 
private String endpoint; 

public UserInfo getUserInfo(String acc, String pwd) throws JAXBException, RemoteException { 

    if (StringUtils.isBlank(endpoint)) { 
     logger.error("***** AD Endpoint is blank, please check sysenv.ad.endpoint param ******"); 
    } 

    ADSoapProxy proxy = new ADSoapProxy(); 
    proxy.setEndpoint(endpoint); 
    logger.debug("***** AD endpoint:" + endpoint + "******"); 

    String xml = proxy.userInfo(acc, pwd); 
    StringReader reader = new StringReader(xml); 

    JAXBContext jaxbContext = JAXBContext.newInstance(UserInfo.class); 
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 

    return (UserInfo) jaxbUnmarshaller.unmarshal(reader); 
} 
} 
+0

你能包括类的声明,你的JUnit测试是?我猜测你没有指定一个测试运行器(通常是'@RunWith(SpringJUnit4ClassRunner.class)''这是告诉Spring从你的配置中注入依赖关系的东西。参见[Spring文档](http:// docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#testcontext-framework)获取更多信息。 – DaveyDaveDave

+1

@DaveyDaveDave是的,这是一个观点,我没有@RunWith (SpringJUnit4ClassRunner.class)注入bean。非常感谢! – Louis

回答

0

当创建需要春天是运行单元测试,你需要以下添加到您的单元测试类。例如:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = MySpringApp.class) 
public MyTestClass{ 
    @Test 
    ... 
} 

更多信息here

+0

非常感谢。现在,它工作正常。 – Louis