在jetty上运行时spring应用程序中的模拟服务
我想在jetty上使用模拟服务运行SpringWeb应用程序,因为我的服务调用了一个远程REST服务,而这些服务还不可用 我使用JUnit类来测试我的控制器,它使用MockMvc、Mockito和ReflectionTestUtils.setField运行得非常好。。,。。,。。注入我的模仿行为 但是现在我想看到我的JSP交互并验证GUI,有没有办法在jetty上运行时模拟我的服务 这是我的jetty课程:在jetty上运行时spring应用程序中的模拟服务,spring,spring-mvc,mocking,jetty,mockito,Spring,Spring Mvc,Mocking,Jetty,Mockito,我想在jetty上使用模拟服务运行SpringWeb应用程序,因为我的服务调用了一个远程REST服务,而这些服务还不可用 我使用JUnit类来测试我的控制器,它使用MockMvc、Mockito和ReflectionTestUtils.setField运行得非常好。。,。。,。。注入我的模仿行为 但是现在我想看到我的JSP交互并验证GUI,有没有办法在jetty上运行时模拟我的服务 这是我的jetty课程: public final class MobileUITestServer { pr
public final class MobileUITestServer {
private static final Logger LOG = LoggerFactory.getLogger(MobileUITestServer.class);
private static Server server;
/**
* Constructeur.
*/
private MobileUITestServer() {}
/**
* Launch server.
*
* @param args arguments
* @throws Exception error
*/
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public static void main(final String[] args) throws Exception {
start(1234);
LOG.info("Joining this thread with server thread...");
server.join();
}
/**
* Start Mobile UI server.
*
* @param port port
* @throws Exception error
*/
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public static void start(int port) throws Exception {
LOG.info("Starting server on port {}", port);
server = new Server(port);
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setDescriptor("src/main/webapp/WEB-INF/web.xml");
webapp.setResourceBase("src/main/webapp");
server.setHandler(webapp);
server.start();
LOG.info("Server started");
}
/**
* Stop this server.
*
* @throws Exception error
*/
@SuppressWarnings({"PMD.AvoidThrowingRawExceptionTypes", "PMD.SignatureDeclareThrowsException"})
public static void stop() throws Exception {
if(server == null || !server.isRunning()) {
throw new RuntimeException("Server is not running...");
}
LOG.info("Stopping server");
server.stop();
LOG.info("Server stopped");
}
}
我假设我不知道您使用的是哪个版本的Spring,哪些模块和上下文配置是使用SpringData、JPA、Hibernate编写的某种服务,因此可以使用嵌入式数据库,如H2,提供一些模拟数据和您已经编写的相同sql模式。您只需替换数据源对象 如果您想编写一个更通用的解决方案,您可以使用Spring的概要文件来定义上下文对象,即您希望运行webapp的环境,即测试、开发、生产等 例如:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="packagesToScan" value="com.contrastofbeauty.mydomain"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<context:annotation-config/>
<jpa:repositories base-package="com.contrastofbeauty.myrepository" entity-manager-factory-ref="emf"
transaction-manager-ref="transactionManager"/>
在这里,我将数据源定义为H2数据库、要加载的模式和数据
<beans profile="dev">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:dev-data.sql"/>
</jdbc:embedded-database>
</beans>
使用SpringData、SpringTest和Spring的截面轮廓进行测试
我希望有帮助
最新消息:为什么不使用Maven来管理项目和Maven Jetty插件?您可以在IDE Eclipse、Intellij等内部启动web应用程序。对我来说,这比较容易,但我不知道您的要求。您的web服务的性质是什么?只读/积垢?JSON/XML?只有JSON,还有CRUD和其他复杂的服务