我正在为我的spring应用程序编写JUnit测试用例。我在eclipse中使用codepro工具来生成测试用例。当我运行这个测试用例时,它是在JVM上运行的,而不是在Tomcat服务器上运行的。所以我想知道它是如何在服务器上运行的?在JVM或tomcat上运行测试用例的最佳实践是什么?为什么?所以请推荐我。代码如下。
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.http.HttpSession;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zodiacapi.framework.business.ZodiacMobileBusinessTx;
import com.zodiacapi.framework.controller.ZodiacMobileAPIController;
import com.zodiacapi.framework.delegate.SendNotificationDelegate;
import com.zodiacapi.framework.dto.ReturnAPIMessageDTO;
import com.zodiacapi.framework.dto.UserDTO;
import com.zodiacweb.framework.cache.CacheService;
import com.zodiacweb.framework.cache.EhCacheServiceImpl;
import com.zodiacweb.framework.exception.ZodiacWebException;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml" })
public class ZodiacMobileAPIControllerTest extends TestCase {
private static final Logger logger = LoggerFactory.getLogger(ZodiacMobileAPIControllerTest.class);
@Autowired
private ZodiacMobileBusinessTx zodiabMobileBusinessTx;
public ZodiacMobileBusinessTx getZodiabMobileBusinessTx() {
return zodiabMobileBusinessTx;
}
@Test
public void testMobileLogin_1()
throws Exception {
ReturnAPIMessageDTO entities = new ReturnAPIMessageDTO();
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("login.properties");
prop.load(in);
try{
UserDTO result = zodiabMobileBusinessTx.login(prop.getProperty("username"), prop.getProperty("password"), prop.getProperty("apikey"), prop.getProperty("deviceid"), prop.getProperty("deviceModel"));
System.out.println("result of test"+result);
} catch (ZodiacWebException e) {
logger.error("Internal Server Error fetching user info", e);
entities.setStatus("false");
entities.setMessage(e.getMessage());
entities.setVersion("");
} catch (Throwable t) {
entities.setStatus("false");
entities.setMessage(t.getMessage());
entities.setVersion("");
}
}
}
对于单元测试,您通常会在JVM中执行它。您可能只会在服务器中运行的应用程序上执行集成/功能测试。
测试Spring控制器(我很熟悉)的选择是:
- 将控制器作为容器和服务器之外的常规POJO进行测试例如:
MyController controller = new MyController()
) - 使用Spring Test MVC测试控制器。这将在测试期间启动Spring。(我更喜欢这个选项)有关一些示例,请参阅单元测试弹簧控制器
- 如果你想在一个真正的tomcat实例中测试你的应用程序,你可以使用Arquillian和The ArquillianSpring Extension。就学习曲线而言,最后一个选项无疑是最复杂的。但能意识到这一点很好。(我自己还没有成功地将其用于Spring应用程序)
现在不用担心使用Arquillian。。。学习需要一些时间。
有关测试弹簧控制器的工作示例,请参阅下面的代码。我从您的代码示例中注意到,您没有所有正确的注释和初始化方法。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = App.class)
@TestPropertySource(locations = "classpath:test.properties")
@WebAppConfiguration
public class AdminUserControllerUnitTest {
MockMvc mvc;
@Autowired
WebApplicationContext webApplicationContext;
@Before
public void initialize(){
mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void testListUsers() throws Exception {
Account account = new Account();
account.setId(1l);
mvc.perform(
get("/admin/user")
.sessionAttr("account",account)
);
.andExpect(MockMvcResultMatchers.model().attribute("users",hasSize(4)));
}