powermock

PowerMock java.lang.ClassCastException: sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection

人盡茶涼 提交于 2019-12-13 13:21:29
问题 I created a mock HttpsURLConnection based on an StackExchange answer: import java.net.URL; import javax.net.ssl.HttpsURLConnection; ... @RunWith(PowerMockRunner.class) public class DialogTest { public void mockHttpsUrlConnectionExample() throws Exception { URL mockUrl = PowerMockito.mock(URL.class); PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockUrl); HttpsURLConnection mockUrlConnection = PowerMockito.mock(HttpsURLConnection.class); PowerMockito.when(mockUrl.openConnection

J-Unit Test: Make static void method in final class throw exception

不羁岁月 提交于 2019-12-13 06:36:25
问题 I am writing J-Unit Tests for my project and now this problem occured: I am testing a servlet which uses a Utility class (class is final, all methods are static). The used method returns void and can throw an IOException (httpResponse.getWriter). Now I have to force this exception... I have tried and searched a lot, but all the solutions I found did not worked, because there was no combination of final, static, void, throw . Has anyone did this before? EDIT: Here is the code snippet Servlet:

Powermock throws ClassNotPreparedException when using JUnit 5

て烟熏妆下的殇ゞ 提交于 2019-12-13 04:22:55
问题 I have a sample test class where I want to mock a static class.My build.gradle is like testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0' testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.2.0' testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.2.0' testRuntime("org.junit.platform:junit-platform-launcher:1.1.1") testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0-beta

Mock private method using PowerMockito

女生的网名这么多〃 提交于 2019-12-13 03:26:19
问题 I'm using PowerMockito to mock the private method call (privateApi) but it still makes the privateApi call which in turn makes another thirdPartCall. I'm getting into problem when thirdPartyCall throws exception. As far as I understand, if I'm mocking the privateApi, it shouldn't get into method implementation detail and return the mock response. public class MyClient { public void publicApi() { System.out.println("In publicApi"); int result = 0; try { result = privateApi("hello", 1); } catch

Using PowerMock to obtain the ATG Nucleus in testing results in NPE

╄→尐↘猪︶ㄣ 提交于 2019-12-13 03:24:55
问题 I am trying to test the following bit of code: GSARepository productCatalog = (GSARepository) Nucleus.getGlobalNucleus().resolveName("/atg/commerce/catalog/ProductCatalog"); for (RepositoryItem orderItem : orderItems) { String product = (String) orderItem.getPropertyValue(PropertyNameConstants.PRODUCTID); if (!ProductUtils.isSpecial(product, productCatalog)) { isSpecial = false; break; } } clearly Nucleus.getGlobalNucleus() is static. According to the docs I should be able to use:

PowerMock fails with JerseyTest

拜拜、爱过 提交于 2019-12-13 02:25:34
问题 We're writing unit tests for a Jersey Java REST service using PowerMock. We have to use PowerMock because we use numerous static methods in a legacy system. We're using PowerMock with EasyMock. Whenever we try to inherit from JerseyTest, the whole framework fails. It's a gradle based project, and the pertinent build.gradle lines are below. testCompile 'junit:junit:4.12' testCompile group: 'org.easymock', name: 'easymock', version: '3.5.1' testCompile group: 'org.easymock', name:

mocking nested function is giving NPE

倾然丶 夕夏残阳落幕 提交于 2019-12-12 23:16:33
问题 Hi I am getting Null Pointer Exception while trying to write unit test cases Here is the class detail: public CreateDraftCampaignResponse createDraftCampaign(CreateDraftCampaignRequest request) throws InvalidInputsException, DependencyException, UnauthorizedException { CreateDraftCampaignResponse draftCampaignResponse = null; try { DraftCampaignDetails createdDraft = draftCampaignI.createDraftCampaign(ConvertionUtil .getDraftCampaignDetailsfromCreateDraftRequest(request));

Trouble using power mock with camel

[亡魂溺海] 提交于 2019-12-12 14:19:47
问题 Since I have to mock a static method, I am using Power Mock to test my application. My application uses * Camel 2.1 *2. I define routes in XML that is read by camel-spring context. There were no issues when Junit alone was used for testing. While using power mock, I get the error listed at the end of the post. I have also listed the XML used. Camel is unable to recognize any of its tags when power mock is used. I wonder whether the byte-level manipulation done by power mock to mock static

How to unit testing spring boot rest controller and exception handler using power mock

懵懂的女人 提交于 2019-12-12 13:35:28
问题 I am having a simple Spring boot application which contains Employee controller which returns the Employee names if the year passed is greater than 2014 and if the it is not less than 2014 then I am throwing a custom exception and handling it in Exception Handler. I want to unit test the exception flow using powermock but I am not sure how to do it. I have gone through some links but unable to understand. Currently I am getting java.lang.IllegalArgumentException: WebApplicationContext is

Unable to mock URL class using PowerMockito/Mockito

核能气质少年 提交于 2019-12-12 10:46:30
问题 I am trying to use PowerMockito to mock the creation of the java.net.URL class in my code that I'm testing. Basically, I want to prevent the real HTTP request from occurring and instead 1) check the data when the request is made and 2) supply my own test data back on a mocked response. This is what I'm trying: @RunWith(PowerMockRunner.class) @PrepareForTest({ URL.class, MockedHttpConnection.class }) public class Test { URL mockedURL = PowerMockito.mock(URL.class); MockedHttpConnection