我知道我如何使用这些术语,但我想知道是否有单独测试的伪造,模拟和存根的可接受定义?你如何为你的测试定义这些?描述您可能使用每种情况的情况.
以下是我如何使用它们:
假:一个实现接口但包含固定数据而没有逻辑的类.根据实施情况,简单地返回"好"或"坏"数据.
Mock:一个实现接口的类,允许动态设置值以返回/异常从特定方法抛出,并提供检查是否已调用/未调用特定方法的功能.
存根:类似于模拟类,但它不提供验证方法是否已被调用/未调用的能力.
模拟和存根可以由模拟框架手动生成或生成.伪造的类是手工生成的.我主要使用模拟来验证我的类和依赖类之间的交互.一旦我验证了交互并且正在通过我的代码测试备用路径,我就会使用存根.我主要使用假类来抽象出数据依赖性,或者每次使用模拟/存根都太繁琐.
memoization和动态编程有什么区别?我认为动态编程是memoization的一个子集.这样对吗?
我正在寻找在Spring MVC中绑定和转换数据的最简单和最简单的方法.如果可能,不进行任何xml配置.
到目前为止,我一直在使用PropertyEditors:
public class CategoryEditor extends PropertyEditorSupport {
// Converts a String to a Category (when submitting form)
@Override
public void setAsText(String text) {
Category c = new Category(text);
this.setValue(c);
}
// Converts a Category to a String (when displaying form)
@Override
public String getAsText() {
Category c = (Category) this.getValue();
return c.getName();
}
}
Run Code Online (Sandbox Code Playgroud)
和
...
public class MyController {
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Category.class, new CategoryEditor());
}
...
}
Run Code Online (Sandbox Code Playgroud)
它很简单:两个转换都在同一个类中定义,绑定很简单.如果我想在我的所有控制器上进行通用绑定,我仍然可以在我的xml配置中添加3行 …
如果我创建一个复合id类,它不实现Serializable,如:
@Entity
@Table(name = "board")
public class Board {
@Id
@Column(name = "keyword_news_id")
private int id;
@Id
@Column(name = "board_no")
private int boardNo;
....
Run Code Online (Sandbox Code Playgroud)
错误发生如下:
Caused by: org.hibernate.MappingException: composite-id class must implement Serializable: com.estinternet.news.domain.IssueNewsBoard
at org.hibernate.mapping.RootClass.checkCompositeIdentifier(RootClass.java:263)
at org.hibernate.mapping.RootClass.validate(RootClass.java:244)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
Run Code Online (Sandbox Code Playgroud)
Hibernate实体类不需要是Serializable.那么,为什么composite-id类必须实现Serializable?我读了这个帖子,但它没有给我足够的信息.
我刚刚搜索了gzip和Deflate,发现Deflate更好.
但是,当我检查Google,Facebook和StackExchange的响应标题时,他们都在使用GZIP.为什么他们使用gzip而不是Deflate?
当我使用JavaScript开发普通的Web应用程序时,try/catch通常不需要该语句.JavaScript中没有检查异常,文件IO或数据库连接.
是try/catch声明在JavaScript有用吗?我什么时候可以使用它?
我想调整浏览器弹出窗口的宽度和高度.我可以设置弹出窗口的大小,但我希望当一个页面被重定向到另一个网页窗口大小适合内容大小转换.
我试过这个:
$(window).width(1000); // Not working.
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
更新
每个人都告诉我不要这样做.为了找出这是不好的做法的确切原因,我在ux.stackexchange.com上询问.
从Spring 3.1开始,由于@ Enable* annotations ,我们可以更轻松地使用JavaConfig .
所以我创建了一个WebConfig来设置WebMvc配置,并尝试对其进行测试.但是,如果我使用WebConfig扩展WebMvcConfigurerAdapter或WebMvcConfigurationSupport,则由于缺少ServletContext,单元测试失败.代码和消息如下所示.
WebConfig.java
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurationSupport {}
Run Code Online (Sandbox Code Playgroud)
Test.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=WebConfig.class)
public class TestFail {
@Test
public void test() {}
}
Run Code Online (Sandbox Code Playgroud)
信息
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
...
Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.<init>(DefaultServletHandlerConfigurer.java:54)
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:253)
at com.zum.news.comments.web.WebConfig$$EnhancerByCGLIB$$8bbfcca1.CGLIB$defaultServletHandlerMapping$10(<generated>)
at com.zum.news.comments.web.WebConfig$$EnhancerByCGLIB$$8bbfcca1$$FastClassByCGLIB$$19b86ad0.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:215)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:280)
at com.zum.news.comments.web.WebConfig$$EnhancerByCGLIB$$8bbfcca1.defaultServletHandlerMapping(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597) …Run Code Online (Sandbox Code Playgroud) 我正在使用Hibernate.我写了一些原生查询,因为我需要使用sub select语句.
查询看起来像这样:
SELECT sub.rownum FROM
(SELECT k.`news_master_id` AS id, @row := @row + 1 AS rownum
FROM keyword_news_list k
JOIN (SELECT @row := 0) r
WHERE k.`keyword_news_id` = :kid
ORDER BY k.`news_master_id` ASC) AS sub
WHERE sub.id = :nid
Run Code Online (Sandbox Code Playgroud)
当我像这样运行此查询时:
sessionFactory.getCurrentSession()
.createSQLQuery(query)
.setParameter("kid", kid)
.setParameter("nid", nid)
.uniqueResult();
Run Code Online (Sandbox Code Playgroud)
出现此异常:
org.hibernate.QueryException: Space is not allowed after parameter prefix ':' ....
Run Code Online (Sandbox Code Playgroud)
这可能是因为:=操作员.我发现了一些Hibernate问题.这个问题仍然存在.这个问题没有解决办法吗?
java ×4
javascript ×3
hibernate ×2
spring ×2
terminology ×2
browser ×1
data-binding ×1
definition ×1
deflate ×1
difference ×1
gzip ×1
jquery ×1
memoization ×1
mocking ×1
mysql ×1
nativequery ×1
performance ×1
spring-3 ×1
spring-mvc ×1
stub ×1
try-catch ×1
unit-testing ×1