我想在我的Gradle构建(版本1.0)中添加集成测试.它们应该与我的正常测试分开运行,因为它们需要将webapp部署到localhost(它们测试该webapp).测试应该能够使用我的主要源集中定义的类.我该如何实现这一目标?
我正在调查使用FreeMarker编写EDI文件.这些基本上是格式化(和验证)的电子发票.我决定开始编写一个简单的例子,并且很难让它模板化'嵌套的pojo'.我的意思是一个包含POJO的POJO,其中两个都有我想要输出的数据.我写了一个单元测试(自包含),由于未知原因而失败.当我运行测试时,我得到以下异常:
Expression user.getSub is undefined on line 1, column 24 in simple.
The problematic instruction:
----------
==> ${user.getSub().user} [on line 1, column 22 in simple]
----------
Java backtrace for programmers:
----------
freemarker.core.InvalidReferenceException: Expression user.getSub is undefined on line 1, column 24 in simple.
at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)
at freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:134)
<snip>
example.TestFreeMarkerTemplating.testSimpleTemplate(TestFreeMarkerTemplating.java:23)
<snip>
Run Code Online (Sandbox Code Playgroud)
我在模板上尝试了很多变化,并没有取得成功.这是测试:
package example;
import java.io.*;
import org.junit.Test;
import freemarker.cache.StringTemplateLoader;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.*;
import static org.junit.Assert.*;
public class TestFreeMarkerTemplating {
Configuration cfg = new Configuration();
StringTemplateLoader stringLoader = new StringTemplateLoader(); …Run Code Online (Sandbox Code Playgroud) 我是Camel的新手,现在我的Tomcat服务器上运行了一条简单的路由.路由是这样构建的:
Processor generateWebResponse = new MySpecialProcessor();
from("servlet:///url?matchOnUriPrefix=true").process(generateWebResponse);
Run Code Online (Sandbox Code Playgroud)
我试过这样一个简单的单元测试:
Exchange lAuthRequest = createExchangeWithBody("[json body!]");
template.send("servlet:///url", lAuthRequest);
assertEquals("baseline body", lAuthRequest.getOut().getBody());
Run Code Online (Sandbox Code Playgroud)
但得到一个异常,表明我无法创建一个servlet端点.这是异常消息:
org.apache.camel.FailedToCreateProducerException: Failed to create Producer for endpoint: Endpoint[servlet:///url]. Reason: java.lang.UnsupportedOperationException: You cannot create producer with servlet endpoint, please consider to use http or http4 endpoint.
Run Code Online (Sandbox Code Playgroud)
这是新的发展,所以除了良好的设计之外,我没有很多限制.我愿意接受需要改变路线的建议.此外,如果我正在做一些不是惯用的事情,我很乐意用任何建议的改进来修改问题.
我花了一些时间试图在文档中识别出这个"特殊变量".我有一个案例,我希望能够将根哈希传递给一个可以对其进行操作的宏.我发现了对Environment.getCurrentEnvironment()的引用,但它在Java中不是模板.如何将根数据模型传递给宏?
我做了一些像这样的简单实验:
public static void main(String[] args) {
try {
NullPointerException n = new NullPointerException();
System.out.println(Lists.newArrayList(n.getStackTrace()));
n.printStackTrace();
System.out.println(Lists.newArrayList(n.getStackTrace()));
throw n;
} catch (NullPointerException e) {
e.printStackTrace();
System.out.println(Lists.newArrayList(e.getStackTrace()));
}
}
Run Code Online (Sandbox Code Playgroud)
得到这样的输出:
java.lang.NullPointerException
at MyTest.main(MyTest.java:231)
java.lang.NullPointerException
at MyTest.main(MyTest.java:231)
[MyTest.main(AbstractScannerTest.java:231)]
[MyTest.main(AbstractScannerTest.java:231)]
[MyTest.main(AbstractScannerTest.java:231)]
Run Code Online (Sandbox Code Playgroud)
但是我想知道抛出异常时是否做了什么.这是一个主要的学术问题,但如果异常是API的一部分,并且在提供给实现时可能会或可能不会被抛出,则在某些情况下它可能是相关的.
我正在寻找满足以下测试用例的比较器最可读的定义:
@Test
public void testComparator() {
List<String> toSort = Lists.newArrayList("a", "b", "c", "d", "e", "f");
Collections.shuffle(toSort);
Comparator<String> theSolution = ???;
Collections.sort(toSort, theSolution);
System.out.println(toSort); // Prints [c, a, b, d, e, f]
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过使用Guava的Ordering定义的比较器,如下所示:
Ordering.explicit("c").thenComparing(Ordering.natural());
Run Code Online (Sandbox Code Playgroud)
但是,显式引发了未枚举的项的异常.所以解决方案失败了.有什么建议?
我花了很长时间试图解决这个问题.我正在编写一个接收用户名和密码的服务.然后,它使用处理器生成一个身份验证令牌,该令牌在消息的Out部分中返回.我想接受JSON格式的参数,并尝试让类型转换正常工作.我把问题简化为一个独立的单元测试,如下所示:
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonDataFormat;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
import com.thoughtworks.xstream.annotations.XStreamAlias;
public class BasicJsonMarshallingTest extends CamelTestSupport {
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
final Processor simpleProcessor = new Processor() {
@Override public void process(Exchange exchange) throws Exception {
SimpleBean bean = exchange.getIn().getBody(SimpleBean.class);
if(bean == null){
return;
}
exchange.getOut().setBody("a=" + bean.getA() + " b=" + bean.getB());
}
};
return new RouteBuilder() {
@Override public void configure() throws Exception {
JsonDataFormat jsonFormat = new JsonDataFormat(JsonLibrary.XStream); …Run Code Online (Sandbox Code Playgroud) 我正在将我的构建从Ant移动到Gradle.Ant允许JUnit任务创建具有不同格式的多个报表.Gradle更具限制性 - 它生成HTML报告和XML报告.XML报告是JUnit文本报告的超集,因此可以将其从一个转换为另一个.什么XSLT将XML转换为文本?这是一个示例XML:
<?xml version="1.0" encoding="UTF-8"?>
<testsuite errors="0" failures="0" hostname="spina.stsci.edu" name="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" tests="6" time="0.14" timestamp="2012-02-27T18:08:03">
<properties />
<testcase classname="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" name="testSupressionWorks" time="0.01" />
<testcase classname="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" name="testSupressionWorksWithDependenciesDisabled" time="0.0020" />
<testcase classname="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" name="testPropagationIsDeferred" time="0.0010" />
<testcase classname="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" name="testPropagationIsDeferredWhenDependenciesAreSuppressed" time="0.0010" />
<testcase classname="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" name="testPropagationIsDeferredWhenDependenciesAreSuppressed2" time="0.0010" />
<testcase classname="edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest" name="testGarbageCollection" time="0.066" />
<system-out><![CDATA[Running Supressing Constraint
Running Supressing Constraint
Running Supressing Constraint
Change Me is: 2
Running Supressing Constraint
Change Me is: 5
]]></system-out>
<system-err><![CDATA[]]></system-err>
</testsuite>
Run Code Online (Sandbox Code Playgroud)
这是我希望它生成的文本:
Testsuite: edu.stsci.CoSI.test.DependencySupressingConstraintJUnitTest
Tests run: 6, Failures: 0, Errors: 0, Time …Run Code Online (Sandbox Code Playgroud) 我是Camel的新手,并试图学习习语和最佳实践.我正在编写需要处理几种不同错误情况的Web服务.这是我的错误处理和路由:
onException(JsonParseException.class).inOut("direct:syntaxError").handled(true);
onException(UnrecognizedPropertyException.class).inOut("direct:syntaxError").handled(true);
// Route service through direct to allow testing.
from("servlet:///service?matchOnUriPrefix=true").inOut("direct:service");
from("direct:service")
.choice()
.when(body().isEqualTo(null))
.inOut("direct:syntaxError")
.otherwise()
.unmarshal().json(lJsonLib, AuthorizationParameters.class).inOut("bean:mybean?method=serviceMethod").marshal().json(lJsonLib);
Run Code Online (Sandbox Code Playgroud)
如您所见,我有特殊处理(基于内容的路由)来处理具有空主体的请求.有没有办法更优雅地处理这个问题?我正在写几种这种类型的服务,看起来它们可以更清洁.
我想要一个像 my.company.bindy 这样的包,其中有几个类都用 Bindy 注释进行注释。然后我想要 Camel 路线可以将 CSV 解组为这些类型之一。我已经完成了所有工作,但是如果包中有多个带有 Bindy 带注释的类,则解组会失败。这是因为 Bindy 试图将 CSV 行解组到包中的每个类中。并且特定的行不会正确地编组到多个类中。我的数据格式在 Spring 中声明如下:
<bean class="org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat">
<property name="packages" value="my.company.bindy"/>
</bean>
Run Code Online (Sandbox Code Playgroud) java ×8
apache-camel ×4
freemarker ×2
junit ×2
unit-testing ×2
bindy ×1
build ×1
code-cleanup ×1
exception ×1
gradle ×1
java-8 ×1
pojo ×1
report ×1
sorting ×1
source-sets ×1
tomcat ×1
xml ×1
xslt ×1
xstream ×1