簡體   English   中英

Spring Web項目中的JUnit

[英]JUnit in Spring web project

我是Junit的新手。 最近,我需要為Spring Web項目添加一些測試功能。 因此,我添加了一個僅用於測試的新項目。

首先,我添加了一個測試ADServiceImpl的測試用例,下面是我的測試代碼。

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

在我運行測試並得到一個錯誤之后,是'endpoint'為空。 但是'endpoint'是通過xml配置中的spring @Resource(name =“ adEndpoint”)設置的。
我該如何解決這個問題? 還是有其他建議用於Spring 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);
}
}

當創建需要運行Spring的單元測試時,您需要將以下內容添加到單元測試類中。 例如:

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

更多信息在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM