powermock

Can't suppress DriverManager's static initializer block

谁说胖子不能爱 提交于 2019-12-06 07:23:57
I have a unit test that attempts to create a SQLException to simulate a database error. In SQLException 's constructor, there is a call to DriverManager , which has a static initialization block. I figured that I could suppress the static block with this type of setup: @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor({"java.sql.DriverManager"}) public class StaticTest { @Test public void testStaticSuppress() throws Exception { SQLException ex = new SQLException(); expect(...).andThrow(ex); } } When I run the test, the static block in DriverManager is still called. What am I

org.mockito.exceptions.misusing.UnfinishedStubbingException Unfinished stubbing detected

£可爱£侵袭症+ 提交于 2019-12-06 07:04:28
I have wrote following code: @RunWith(PowerMockRunner.class) @PrepareForTest(Integer.class) public class TestClass{ @Test public void test(){ PowerMockito.mockStatic(Integer.class); when(Integer.parseInt(anyString())).thenReturn(0); System.out.println(Integer.parseInt("12")); } } I got following error message : org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: -> at com.ctc.dime.services.autopublisher.stores.StoresPublishingServiceTest.test(StoresPublishingServiceTest.java:120) E.g. thenReturn() may be missing. Examples of correct stubbing: when

Is it possible to mock a single method in an already existing object?

夙愿已清 提交于 2019-12-06 03:24:21
For an integration test, I need to mock a specific method in a java service client without destroying the rest of the information in it. It has no self-constructor, so a solution like this is out of the question: private DBClient mockClient = new DBClient(alreadyExistingClient){ @Override void deleteItem(Item i){ //my stuff goes here } }; Is there a way to mock the deleteItem method such that the credentials, endpoints, etc... are preserved in an existing DBClient object? edit: mockito is not available for use in this case cahen You can use a Dynamic Proxy to intercept any method invocation

PowerMock学习(七)之Mock Constructor的使用

廉价感情. 提交于 2019-12-06 01:07:52
前言 我们在编码的时候,总习惯在构造器中传参数,那么在powermock中是怎么模拟带参数构造的呢,这并不难。 模拟场景 我们先模拟这样一个场景,通过dao中的传入一个是布尔类型(是否加载)和一个枚举类(使用哪种数据库),构造Dao这个类,在编写一个插入学生方法 dao部分的代码 具体示例代码如下: package com.rongrong.powermock.mockconstructor; /** * @author rongrong * @version 1.0 * @date 2019/11/28 23:12 */ public class StudentConstructorDao { public enum DataBaseType{ MYSQL, ORACLE, } /** * * @param isLoad 数据库是否加载即链接 * @param dataBaseType 数据库类型 */ public StudentConstructorDao(Boolean isLoad,DataBaseType dataBaseType) { throw new UnsupportedOperationException(); } public void insertStudent(StudentConstructor studentConstructor){ throw

How to mock protected subclass method inherited from abstract class?

我怕爱的太早我们不能终老 提交于 2019-12-05 23:13:24
问题 How to use Mockito or PowerMock to mock a protected method that is realized by a subclass, but inherited from an abstract super class? In other words, I want to test "doSomething" method while mocking the "doSomethingElse". Abstract super class public abstract class TypeA { public void doSomething() { // Calls for subclass behavior doSomethingElse(); } protected abstract String doSomethingElse(); } Subclass implementation public class TypeB extends TypeA { @Override protected String

您如何断言在JUnit 4测试中引发了某种异常?

久未见 提交于 2019-12-05 19:18:06
如何惯用JUnit4来测试某些代码引发异常? 虽然我当然可以做这样的事情: @Test public void testFooThrowsIndexOutOfBoundsException() { boolean thrown = false; try { foo.doStuff(); } catch (IndexOutOfBoundsException e) { thrown = true; } assertTrue(thrown); } 我记得在这种情况下,有一个批注或一个Assert.xyz或 一些 不那么杂乱无章的 东西 ,更是JUnit的精髓。 #1楼 我在这里尝试了许多方法,但是它们要么很复杂,要么不完全符合我的要求。 实际上,可以很简单地编写一个辅助方法: public class ExceptionAssertions { public static void assertException(BlastContainer blastContainer ) { boolean caughtException = false; try { blastContainer.test(); } catch( Exception e ) { caughtException = true; } if( !caughtException ) { throw new

您如何断言在JUnit 4测试中引发了某种异常?

喜夏-厌秋 提交于 2019-12-05 19:12:49
如何惯用JUnit4来测试某些代码引发异常? 虽然我当然可以做这样的事情: @Test public void testFooThrowsIndexOutOfBoundsException() { boolean thrown = false; try { foo.doStuff(); } catch (IndexOutOfBoundsException e) { thrown = true; } assertTrue(thrown); } 我记得在这种情况下,有一个批注或一个Assert.xyz或 一些 不那么杂乱无章的 东西 ,更是JUnit的精髓。 #1楼 我在这里尝试了许多方法,但是它们要么很复杂,要么不完全符合我的要求。 实际上,可以很简单地编写一个辅助方法: public class ExceptionAssertions { public static void assertException(BlastContainer blastContainer ) { boolean caughtException = false; try { blastContainer.test(); } catch( Exception e ) { caughtException = true; } if( !caughtException ) { throw new

PowerMock学习(五)之Verifying的使用

[亡魂溺海] 提交于 2019-12-05 18:12:20
前言 Verifying是一个非常强大的测试工具,在mock系列框架中使用广泛,主要用于验证方法是否被调用,下面将举例说明。 场景 模拟这样一个场景,通过Dao查询学生,如果存在更新原来学生,不存在则创建一个学生。 1、先来创建dao层的代码,具体示例代码如下: package com.rongrong.powermock.verifying; /** * @author rongrong * @version 1.0 * @date 2019/11/26 20:56 */ public class StudentVerifyDao { public int getStudentCount(Student student) { throw new UnsupportedOperationException(); } public void saveStudent(Student student) { throw new UnsupportedOperationException(); } public void updateStudent(Student student) { throw new UnsupportedOperationException(); } } 2、接着我们再来编写,service层的代码,具体示例代码如下: package com.rongrong

Unable to mock static methods in instrumentation tests

一曲冷凌霜 提交于 2019-12-05 17:53:05
I am having a hard time in mocking static methods for instrumentation ( Espresso ) tests. For mocking objects, I am using Mockito . But, since Mockito cannot mock static methods , I am using Powermock on top of it. This works fine for tests running on the JVM machine, but for UI Tests, this combination does not work fine. I have declared the following dependencies for instrumentation tests. androidTestCompile 'org.mockito:mockito-core:1.10.19' androidTestCompile 'org.powermock:powermock-api-mockito:1.6.5' androidTestCompile 'org.powermock:powermock-module-junit4:1.6.5' androidTestCompile 'com

PowerMock: How to unmock a method?

此生再无相见时 提交于 2019-12-05 15:02:33
问题 I have a static method that is mocked using PowerMock to throw an exception. (It deletes files.) Unfortunately, during my @After (after-each-test) method, I need to call this method without the mocks. How can I umock a method? I don't see an equivalent to Mockito.reset() . [ Ref: mockito : how to unmock a method? ] Example: @RunWith(PowerMockRunner.class) @PrepareForTest(PathUtils.class) // Important: This class has a static method we want to mock. public class CleaningServiceImplTest2