Tag: junit

如何模拟私有dao变量?

我有一个dao.create()调用,我想在测试方法时进行模拟。 但我错过了一些东西,因为我还在接受NPE。 这有什么不对? class MyService { @Inject private Dao dao; public void myMethod() { //.. dao.create(object); // } } 如何模拟dao.create()调用? @RunWith(PowerMockRunner.class) @PrepareForTest(DAO.class) public void MyServiceTest { @Test public void testMyMethod() { PowerMockito.mock(DAO.class); MyService service = new MyService(); service.myMethod(); //NPE for dao.create() } }

有哪些方法可以测试依赖于静态方法的方法?

我需要测试一些依赖于Spring Security的安全相关类。 此代码使用静态的SecurityContextHolder.getContext() 。 如何在不设置整个安全上下文的情况下测试调用方法? 我们正在使用JUnit 4和Mockito。 Mockito在常见问题解答中非常清楚,静态方法不受支持。 还有其他选择吗? Spring Security案例的答案很好,但我正在寻找更普遍问题的解决方案。

PowerMock访问私人会员

阅读后: https : //code.google.com/p/powermock/wiki/BypassEncapsulation我意识到,我不明白。 请参阅此示例: public class Bar{ private Foo foo; public void initFoo(){ foo = new Foo(); } } 如何通过使用PowerMock访问私有成员foo (例如,validationfoo不为null)? 注意: 我不想要的是使用额外的get方法修改代码。 编辑: 我意识到我错过了链接页面上的示例代码块和解决方案。 解: Whitebox.getInternalState(bar, “foo”);

JUnit4根据自定义java注释跳过测试

我希望我的JUnit4测试可以根据我用Java创建的自定义注释来执行。 此自定义注释的目的是让JUnit4注意只有在机器的平台与注释中指定的平台匹配时才应运行测试。 说我有以下注释: public @interface Annotations { String OS(); … } 以下测试: public class myTests{ @BeforeClass public setUp() { … } @Annotations(OS=”mac”) @Test public myTest1() { … } @Annotations(OS=”windows”) @Test public myTest2() { … } @Annotation(OS=”unix”) @Test public myTest3() { … } } 如果我要在Mac机器上执行这些测试,那么只应执行myTest1(),其余部分应该被忽略。 但是,我目前仍然坚持我应该如何实现这一点。 如何让JUnit读取我的自定义注释并检查是否应该运行测试。

将私有方法更改为受保护以进行测试

将私有方法更改为受保护的JUNIT测试是否是个好主意。

如何在每个测试方法中强制运行静态块?

我发现静态块只在我执行多个JUnit测试时运行一次。 如何强制它为每个测试方法运行? 我正在使用最新的JUnit 4.8.2 另外,根据xUnit设计原则,每种方法都应该完全独立于其他方法。 为什么静态块只能执行一次? @Test TestMethod1 () { Accounts ac = new Accounts(); ac.method1(); //kill the thread inside } @Test TestMethod2 () { Accounts ac = new Accounts(); ac.method2(); // the thread is no longer available!! } class Accounts { static { // initalize one thread to monitor something } } 当TestMethod1和TestMethod2在不同的测试类中时,甚至会发生这种情况。

如何使用selenium和junit测试多个浏览器(版本)

我刚刚发现了selenium – 一个很棒的工具! 我计划运行/使用selenium-ide生成的junit4代码。 但我需要它与许多浏览器/网络驱动程序运行。 这个用例是否有junit / java-pattern? 我的第一个想法是使用@RunWith(Parameterized.class)并提供WebDrivers列表(该类的参数 – 可能作为列出浏览器和版本的外部文件提供?!)。 这是一个好主意吗? 是否有可能提供一个中心@Parameters -method供我所有的Selenium测试使用? 有什么替代品? 可能有可能更改Selenium导出的“格式”以最小化手动更改?

为什么在junit中assertEquals和assertSame为两个实例同一个类返回相同的结果?

根据文件 assertEquals()断言两个对象是相等的。 assertSame()断言两个对象引用同一个对象。 所以我期待如果我有一个类似下面的课程 class SomeClass {} 然后 SomeClass someClass1= new SomeClass(); SomeClass someClass2= new SomeClass(); assertSame(someClass1,someClass2); // fail assertEquals(someClass1,someClass2); // fail assertEquals传递和assertSame失败。 因为两个类的值都是等于但它们具有不同的参考位置。 由于我在两种情况下都失败了,那么我的问题是这两者之间有什么区别?

如何让我的Spring-JUnit测试认为它在GenericApplicationContext中运行?

我使用的是Spring 3.2.6.RELEASE , JUnit 4.11和DWR 3.0.0-rc2 。 我的问题是,在运行Spring-JUnit集成测试时,如何模拟org.springframework.context.support.GenericApplicationContext中运行的内容? 我试过这个: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ “classpath:test-context.xml”, “classpath:dwr-context.xml” }) @WebAppConfiguration public class MyServiceIT {} 我的“dwr-context.xml”文件设置为 但是,我的所有JUnit测试最终都会因以下exception而死: (“java.lang.IllegalStateException:WebApplicationObjectSupport实例[org.directwebremoting.spring.DwrController@11297ba3]不在WebApplicationContext中运行,而是在:org.springframework.context.support.GenericApplicationContext@4e513d61”)中运行。 如果我能弄清楚如何欺骗系统在GenericApplicationContext运行,我就会遇到这个问题。 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) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 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 […]

unit testing在调试模式下成功,但在正常运行时失败

为什么我的unit testing在调试模式下成功但在正常运行时失败? public class ExecutorServiceTest extends MockitoTestCase{ private int numThreads; private ExecutorService pool; private volatile boolean interruptedBitSet; @Override public void setUp() { numThreads = 5; pool = Executors.newFixedThreadPool(numThreads); } class TaskChecksForInterruptedBit implements Callable { @Override public String call() throws Exception { interruptedBitSet = false; while (!Thread.currentThread().isInterrupted()) { } interruptedBitSet = Thread.currentThread().isInterrupted(); return “blah”; } } […]