我在我的测试框架中有一个方法,它创建一个类的实例,具体取决于传入的参数:
public void test(Object... constructorArgs) throws Exception {
Constructor<T> con;
if (constructorArgs.length > 0) {
Class<?>[] parameterTypes = new Class<?>[constructorArgs.length];
for (int i = 0; i < constructorArgs.length; i++) {
parameterTypes[i] = constructorArgs[i].getClass();
}
con = clazz.getConstructor(parameterTypes);
} else {
con = clazz.getConstructor();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,如果构造函数具有基本类型,则不起作用,如下所示:
public Range(String name, int lowerBound, int upperBound) { ... }
.test("a", 1, 3);
Run Code Online (Sandbox Code Playgroud)
结果是:
java.lang.NoSuchMethodException: Range.<init>(java.lang.String, java.lang.Integer, java.lang.Integer)
Run Code Online (Sandbox Code Playgroud)
原始int是自动装入对象版本的,但是如何让它们返回以调用构造函数?
我的Spring应用程序有问题,我的@Service类在应用程序启动时被创建了两次.我知道这是我的配置问题,因为我之前已经经历过,但究竟我做错了什么?
我在下面列出配置的方式有什么根本原因吗?(我已经省略了我认为无关紧要的一切)
web.xml中:
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/myapp-config.xml
/WEB-INF/myapp-security.xml
/WEB-INF/myapp-mvc.xml
</param-value>
</context-param>
<listener>
<listener-class>com.myapp.servlet.MyAppContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
MYAPP-servlet.xml中
<context:component-scan base-package="com.myapp" annotation-config="true" />
<mvc:annotation-driven />
Run Code Online (Sandbox Code Playgroud)
MYAPP-config.xml中
<context:component-scan base-package="com.myapp" annotation-config="true" />
<context:annotation-config />
Run Code Online (Sandbox Code Playgroud) 我正在测试一个使用@Autowired注入服务的类:
public class RuleIdValidator implements ConstraintValidator<ValidRuleId, String> {
@Autowired
private RuleStore ruleStore;
// Some other methods
}
Run Code Online (Sandbox Code Playgroud)
但是如何在测试期间模拟ruleStore?我无法弄清楚如何将我的模拟RuleStore注入Spring并进入自动布线系统.
谢谢
在JUnit测试中,我使用此代码加载特定于测试的配置文件:
InputStream configFile = getClass().getResourceAsStream("config.xml");
Run Code Online (Sandbox Code Playgroud)
当我通过eclipse运行测试时,它需要xml文件与测试文件位于同一目录中.
当我使用maven构建项目时,它需要xml src/test/resources,因此它被复制到target/test-classes.
如何让它们只用一个文件?
我应该写作
assertTrue("User logged in", user.isLoggedIn());
还是
assertTrue("User is not logged in", user.isLoggedIn());
前者在源文件中提供了更好的读取:
"我断言以下情况属实:用户已登录."
错误消息可以双向读取:
java.lang.AssertionError: User logged in
"断言用户已登录时
出错" "错误是用户已登录."
JUnit文档没有提供它应该是什么的明确指南,除了它是
"{@link AssertionError}的标识消息",
并且在这两种情况下,文本标识正在运行的测试.
常见用法是什么?
我有一个固定大小的方形div,并希望在内部放置一个任意大小的图像,以便使用CSS在水平和垂直方向上居中.水平很容易:
.container { text-align: center }
Run Code Online (Sandbox Code Playgroud)
对于纵向,常见的解决方案是:
.container {
height: 50px;
line-height: 50px;
}
img {
vertical-align: middle;
}
Run Code Online (Sandbox Code Playgroud)
但这并不完美,取决于字体大小,图像将在2-4px左右太远.
据我所知,这是因为用于垂直对齐的"中间"实际上不是中间,而是靠近中间的字体上的特定位置.一个(略微hacky)解决方法是:
container {
font-size: 0;
}
Run Code Online (Sandbox Code Playgroud)
这适用于Chrome和IE7,但不适用于IE8.我们希望所有字体行都在同一点,中间,但它似乎是根据浏览器,可能是使用的字体来命中注定.
我能想到的唯一解决方案是破解线高,使其略短,使图像出现在正确的位置,但它看起来非常脆弱.有更好的解决方案吗?
在这里查看所有三种解决方案的演示:http: //jsfiddle.net/usvrj/3/
没有IE8的人可能会发现这个截图很有用:

我有一个Spring应用程序,我想知道提供静态内容的最佳方式.我尝试过以下方法:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
这是有效的,但DefaultServlet的行为意味着表单的任何请求都/static/PATH从中提供文件webapp/PATH.这暴露了一个巨大的漏洞,允许使用以下URL显示敏感信息:http://localhost/app/static/META-INF/context.xml
这是什么常见的解决方案?我应该移动敏感文件吗?写我自己的DefaultServlet?或者有更好的方式来提供静态内容吗?
python-oauth2的Github页面提供了有关创建签名请求的说明req = oauth.Request(...),该页面返回可以签名的字典.但是我如何实际发送这些请求呢?
我有一个方法的对象
public boolean hasPermission(String role) {
return permissions.contains(role);
}
Run Code Online (Sandbox Code Playgroud)
我想做相同的:
<c:if test="${row.hasPermission(role)}">
<td></td>
</c:if>
Run Code Online (Sandbox Code Playgroud)
但我无法从JSP文件中访问hasPermission方法.我该怎么做?
根据http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources的建议,我希望用它<mvc:resources>来提供我的弹簧静态内容.
我尝试了以下XML,但.xsd文件不包含声明<mvc:resources>,我找不到替代.xsd.我可以忽略eclipse错误,但由于SAXParseException,服务器无法启动.
我哪里出错了?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:resources mapping="/css/**" location="/css/"/>
</beans>
Run Code Online (Sandbox Code Playgroud) 要使用Twitter的Bootstrap库创建主按钮,您需要使用两个CSS类,如下所示:class="btn btn-primary".
为什么他们以这种方式设计API,当他们可以使btn-primary包含btn包含的所有CSS时?这纯粹是为了节省代码重复,因此文件大小还是存在更复杂的原因?