Tag: unit testing

如何使用MUnit在Mule Flow中模拟Java组件

我正在尝试使用MUnit对我的一个子流进行unit testing,我需要模拟一个自定义Java组件,但我无法这样做。 我的子流程如下所示 <!– –> 这是我失败的unit testing @Test public void givenAValidPayload_whenFlowIsInvoked_itShouldSendPayloadToDestinationSFTPOnlyOnce() throws Exception { destinationSFTP.thenReturnSameEvent(); sourceArchiveSFTP.thenReturnSameEvent(); deleteProcessedFileComponent.thenReturnSameEvent(); successEmail.thenReturnSameEvent(); MuleEvent testEvent = PropertyEnricher.enrich(testEvent(IOUtils.toInputStream(“hello,dummy,payload”))).get(); runFlow(PROCESS_CSV_FLOW, testEvent); verifyCallOfMessageProcessor(“outbound-endpoint”) .ofNamespace(“sftp”) .withAttributes(attribute(“name”).ofNamespace(“doc”).withValue(“DestinationSFTP”)) .times(1); } 我试图嘲笑组件 deleteProcessedFileComponent = whenMessageProcessor(“component”) .withAttributes(attribute(“name”).ofNamespace(“doc”).withValue(“Delete Read File”)); 我尝试了一些变化,没有一个工作,我猜一个组件不是MessageProcessor 我得到的例外情况如下 org.mule.api.lifecycle.InitialisationException: Component has not been initialized properly, no flow constuct. at org.mule.component.AbstractComponent.initialise(AbstractComponent.java:218) at org.mule.processor.chain.AbstractMessageProcessorChain.initialise(AbstractMessageProcessorChain.java:80) at org.mule.processor.chain.AbstractMessageProcessorChain$$FastClassByCGLIB$$38c17d88.invoke() at […]

对IO进行JUnit测试

我是新来的,也是junit测试的新手。 我有一个有两种方法的类,我想为它编写unit testing。 我不知道如何开始我阅读一些基本的教程,但我无法开始如何。 你们中的任何人都可以为我提供一些基本的骨架。 我的课是 public class CreateCSV { MyWriter csvOutput = null; public void createSortedSet ( final HashMap map, final long totalSize, final long totalSizewithHeader, File file ) { ArrayList messages = new ArrayList(); try { messages.addAll( map.values() ); map.clear(); for ( Signal signal : messages ) { signal.setBandwidth( ( signal.getSize() / ( […]

检查对象相等而不等于在java中重写

在java中是否有任何utils允许检查对象的相等性而不等于覆盖? 出于某些原因,我不想为我的class级提供equals方法。 我需要在unit testing中使用类似SomeUtils.equals(a,b)的东西来比较所有对象字段(通过reflection我猜)。

抛出Mockito和PowerMock MethodNotFoundException

使用Powermockito和Mockito为连接池构建一些简单的unit testing时遇到以下错误,我绕过了Hikari CP。 测试的设置如下。 令我感到困惑的是,我有一些未显示的unit testing,它们都使用相同的设置和方法。 只有这一个unit testing继续失败并出现该错误。 when我把它放在顶部的时候,它们都无法找到方法并不重要。 org.powermock.reflect.exceptions.MethodNotFoundException: No methods matching the name(s) getColumnCount were found in the class hierarchy of class java.lang.Object. at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1720) at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1745) at org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate(WhiteboxImpl.java:983) at org.powermock.core.MockGateway$MockInvocation.findMethodToInvoke(MockGateway.java:317) at org.powermock.core.MockGateway$MockInvocation.init(MockGateway.java:356) at org.powermock.core.MockGateway$MockInvocation.(MockGateway.java:307) at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:142) at org.powermock.core.MockGateway.methodCall(MockGateway.java:125) at com.datafiniti.utils.mysqlconnpool.MysqlConnPoolTests.executeStringQuery(MysqlConnPoolTests.java:149) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68) at […]

如何使用ANT在类或套件中运行所有测试时打印当前执行的junit测试方法?

我有一组JUnit测试用例,我使用junit任务从ANT执行它们。 在执行测试时,在控制台中我只能看到当前正在运行的测试用例(java类),而不是测试方法。 有没有办法打印当前正在执行的测试方法? 或者除了拥有自己的JUnit测试运行器之外还有其他方法吗? 控制台输出示例 [junit] Running examples.TestCase1 [junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 2.835 sec 相反,我希望得到输出 [junit] Running examples.TestCase1 [junit] Running examples.TestCase1.test1 [junit] Running examples.TestCase1.test2 [junit] Running examples.TestCase1.test3 [junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 2.835 sec

运行相同类但具有不同初始条件的测试套件

在JUnit 4中,我希望编写一个由同一测试用例的多种风格组成的测试套件,每种风格的初始条件都不同。 这是一个例子: import java.io.File; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({MultiInputClientServerIntegrationTest.NormalInput.class, MultiInputClientServerIntegrationTest.SimulationHashIssue.class}) public class MultiInputClientServerIntegrationTest { @RunWith(Suite.class) @SuiteClasses({TestClientServerIntegration.class}) public class NormalInput {} @RunWith(Suite.class) @SuiteClasses({TestClientServerIntegration.class}) public class SimulationHashIssue { public SimulationHashIssue() { TestClientServerIntegration.simulation = new File(“test\\BEECHA01\\sim2.zip”); TestClientServerIntegration.inputFile = “files\\config.in”; } } } 如您所见,两个内部类都具有TestClientServerIntegration.class SuiteClasses,但第二个内部类正在更改一些静态变量值。 我发现这个构造函数永远不会被调用,因此这些静态函数永远不会被更改。 我的最终目标是使用多种类型的输入反复运行此TestClientServerIntegration.class 。 如果我能以这种方式运行测试套件,那将是理想的 – 所以希望它是可能的。 我想尽可能少地攻击JUnit,但是需要完成的工作将会完成。

JUnit @AfterClass运行时添加到一个糟糕的测试用例:(

关于每个测试运行的运行时间,我面临轻微的JUnit不便。 我有一个@AfterClass注释,运行的最后一个测试将把它的运行时添加到它。 因此,报告将错误地显示最后运行的不良测试的长时间运行时间。 有没有办法从上次测试中排除其运行时间?

模拟使用外部类的方法,mockito

我是mockito的新手,只是想了解它是如何工作的。 我有一个我想测试的方法。 该方法实例化多个类以使用其方法。 例如 methodToTest{ class1 c1 = new class1(); class2 c2 = new class2(); class3 c4 = new class3(); c1.method1; c2.method2; c3.method3; more logic … return result } 我明白为了测试这个方法我需要模拟类。 这是否意味着我需要将其解耦并将每个类作为参数传递给方法? 我想避免使用一个使用大量参数的方法,这些参数只有在模拟时才真正需要。 也许我错过了什么。 感谢您的见解。

Mockito – 奇怪的包范围类inheritance问题

发现非常有趣的问题,并在调试后发现了重现它的场景。 所以,如果我有一个包含范围B的类,它有一些公共方法,公共类A扩展它: package somepackage; class B { public void someMethod() { throw NullPointerException(); } } package somepackage; public class A extends B { } 然后在测试中: A a = mock(A.class); a.someMethod(); 并猜测是什么,我得到了刚刚扔的NullPointerException,所以Mockito以某种方式创建了一个“真正的”对象并调用了一个真正的方法而不是模拟的方法。 为什么这样? java.lang.IllegalArgumentException at test.B.setProxy(B.java:6) at test.A.setProxy(A.java:1) at secretservice.service.TestFDSServiceImpl.testService(TestFDSServiceImpl.java:17) 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) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at […]

Java – 合同测试

我正在尝试为一些广泛使用的接口编写合同测试: 沿着: public abstract class MyInterfaceContractTest extends TestCase { private MyInterface _toTest; public void setUp(){ _toTest = getTestableImplementation(); } protected abstract MyInterface getTestableImplementation(); public void testContract(){ } } …和… public class MyInterfaceImplementationTest extends MyInterfaceContractTest { protected MyInterface getTestableImplementation(){ return new MyInterfaceImplementation(…); } } 但是,我希望能够测试MyInterfaceImplementation多个实例。 在我的用例中,这是一个包含数据集合的不可变对象(根据接口MyInterface指定访问MyInterface ),它可能是空的,或者有少量数据,甚至是大量数据。 所以问题是,我如何测试我的实现的多个实例? 目前,我必须初始化实现以将其传递给抽象合同测试。 一种方法是为每个实现提供多个测试类,其中每个测试类测试该实现的特定实例 – 但这似乎有点庞大且难以跟踪。 FWIW,我正在使用JUnit 3。