Tag: unit testing

如何在java中编写测试用例

嗨,我创建了一个类ABC.java,并有一个构造函数ABC()如下: public class ABC { private static String host; private static String port; —— public ABC(){ try { File file = new File(“Element.xml”); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(file); 我传递Element.xml作为输入,我想测试这个类,所以我创建了ABCTest类,请告诉我如何为构造函数ABC()编写测试用例以及如何编写assert(),用于上面的代码

使用PowerMock进行每次测试后,模拟行为都会重置

我正在使用PowerMock编写unit testing,模拟一些util类的行为。 为测试类定义一次行为(通过@BeforeClass注释)会导致: 第一次测试调用返回模拟值 第二次测试调用返回实际方法返回值 示例代码: import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest( {A.class, B.class}) public class TestMockedMethods { private static B b; @BeforeClass public static void setUp() { PowerMockito.mockStatic(A.class); PowerMockito.when(A.getVal()).thenReturn(“X”); b = PowerMockito.mock(B.class); PowerMockito.when(b.getVal()).thenReturn(“Y”); } @Test public void test1() { // PASS Assert.assertEquals(“X”, A.getVal()); Assert.assertEquals(“Y”, […]

如何在unit testing中模拟InitialContext构造函数

当我尝试为Junit测试模拟以下方法(Method使用远程EJB调用业务逻辑)时,它给出了javax.naming.NoInitialContextException private void someMethod(int id1, int id2, HashMap map){ ……some code…….. Context ctx = new InitialContext(); Object ref = ctx.lookup(“com.java.ejbs.MyEJB”); EJBHome ejbHome = (EJBHome)PortableRemoteObject.narrow(ref, EJBHome.class); EJBBean ejbBean = (EJBBean)PortableRemoteObject.narrow(ejbHome.create(), EJBBean.class); ejbBean.someMethod(id1,name); …….some code…….} 我对上述方法的unit testing @Test public void testsomeMethod() throws Exception { …….setting initial code… //Mock context and JNDI InitialContext cntxMock = PowerMock.createMock(InitialContext.class); PowerMock.expectNew(InitialContext.class).andReturn(cntxMock); expect(cntxMock.lookup(“com.java.ejbs.MyEJB”)).andReturn(refMock); […]

Junit是否在每次测试方法调用时重新初始化该类?

当我运行以下代码时,两个测试用例都成为现实: import static junit.framework.Assert.assertEquals; import org.junit.Test; public class MyTest{ private int count; @Before public void before(){ count=1; } @Test public void test1(){ count++; assertEquals(2, count); } @Test public void test2(){ count++; assertEquals(2, count); } } 预期的行为 test1 – 成功 test2 – 失败(正如预期的那样,计数将变为3) 实际行为 test1 – 成功 test2 – 成功 为什么junit会在每次测试方法调用时reinitializing class/variable 。 这是junit中的错误或故意提供。

ant junit任务不报告细节

我试着用JUnit测试写一个ant,但得到以下结果: unittest: [junit] Running com.mytest.utiltest [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec [junit] Test com.mytest.utiltest FAILED 它只是显示没有打印细节的错误,我在build.xml中指定下面的参数也尝试从ant -v or ant -debug ,但没有得到任何运气。 有人可以帮忙吗? ant 1.8.2,sun jdk1.6.0_20,junit 4.8.2 为了缩小问题范围,我创建了一个单独的项目,这是我的build.xml 下面是simpletest.java package com.mytest.unittest; import junit.framework.TestCase; public class SimpleTest extends TestCase{ public void testFirst() { assertTrue(true); } } C:\ TestPrj>蚁 Buildfile: C:\TestPrj\build.xml unittest: […]

模拟对象使用Mockito调用final类静态方法

我刚刚开始模拟我们应用程序的不同层。 当我调用最终类静态方法时,我的一个模拟对象返回NPE。 有没有解决的办法? 例如 when(mockObject.someMethod(FinalClass.staticMethod(someParam)).anotherMethodCall) .thenReturn(“someString”);

有没有一种简单的方法来匹配使用Hamcrest的字段?

我想测试一个对象的特定字段是否与我指定的值匹配。 在这种情况下,它是S3Bucket对象中的存储桶名称。 据我所知,我需要为此编写一个自定义匹配器: mockery.checking(new Expectations() {{ one(query.s3).getObject(with( new BaseMatcher() { @Override public boolean matches(Object item) { if (item instanceof S3Bucket) { return ((S3Bucket)item).getName().equals(“bucket”); } else { return false; } } @Override public void describeTo(Description description) { description.appendText(“Bucket name isn’t \”bucket\””); } }), with(equal(“key”))); … }}); 如果有一种更简单的方法可以做到这一点会很好,例如: mockery.checking(new Expectations() {{ one(query.s3).getObject( with(equal(methodOf(S3Bucket.class).getName(), “bucket”)), with(equal(“key”))); … }}); […]

春季junit测试

我有一个maven spring项目(最新版本),我想写一些junit测试(最新版本)。 我的问题是我的spring bean是自动assembly的,当我从junit测试中调用它们时,我得到空指针exception,因为spring不会自动assembly它们。 如何加载上下文以便自动连接?

部分模拟很糟糕,为什么呢?

脚本 我有一个类称为Model ,它表示不同类型的许多其他对象的复杂的复合对象。 您可以将其视为具有Door[] , Tire[] , Engine , Driver等的Car 。而这些对象又具有子对象,例如Engine具有SparkPlug , Clutch , Generator等。 我有一个Metrics类,它可以计算一些或多或少复杂的关于Model指标,实质上它看起来像这样: public class Metrics{ private final Model model; public Metrics(Model aModel){model = aModel;} public double calculateSimpleMetric1(){…} public double calculateSimpleMetric2(){…} public double calculateSimpleMetricN(){…} public double calculateComplexMetric(){ /* Function that uses calls to multiple calculateSimpleMetricX to calculate a more complex metric. */ […]

如何使用JUnit对JavaFX控制器进行unit testing

什么是初始化JavaFX运行时的正确方法,以便您可以使用并发工具和Platform.runLater(Runnable)进行unit testing(使用JUnit)控制器? 从@BeforeClass方法调用Application.launch(…)导致死锁。 如果未调用Application.launch(…)则抛出以下错误: java.lang.IllegalStateException: Toolkit not initialized at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:121) at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:116) at javafx.application.Platform.runLater(Platform.java:52) at javafx.concurrent.Task.runLater(Task.java:1042) at javafx.concurrent.Task.updateMessage(Task.java:987) at com.xyz.AudioSegmentExtractor.call(AudioSegmentExtractor.java:64) at com.xyz.CompletionControllerTest.setUp(CompletionControllerTest.java:69) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) […]