I am trying to use powermock to mock a private method, but my PowerMock is not recognized in MockitoBusinessOperation MockitoBusinessOperation = PowerMock.createPartialMock(MockitoBusinessOperation.class, "inTestMethod"); . I used maven and the dependencies for mockito and powermock are defined in my pom file
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-mockito-release-full</artifactId>
<version>1.4.9</version>
<scope>test</scope>
</dependency>
I don't know if the error is related to powermock with TestNG or I am doing some mistake in my code.
#PrepareForTest(MockitoBusinessOperation.class)
#Test(enabled = true)
public void testReCalculatePrepaids() throws Exception {
MockitoBusinessOperation MockitoBusinessOperation = PowerMock.createPartialMock(MockitoBusinessOperation.class, "inTestMethod");
PowerMock.expectPrivate(MockitoBusinessOperation, "inTestMethod", Id).andReturn("working fine");
when(MockitoBusinessService.creditReport(this.Id)).thenReturn(new String("Decline by only Me"));
String report = MockitoBusinessService.creditReport(this.Id);
String mainReport = MockitoBusinessOperation.creditAproved(this.Id);
}
someone has an idea or any clue lead to the solution
According to the documentation your maven file should have the following definitions:
<properties>
<powermock.version>1.5</powermock.version>
</properties>
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
please try this way
#Test
public void commandEndHandlerTest() throws Exception
{
Method retryClientDetail_privateMethod =yourclass.class.getDeclaredMethod("Your_function_name",null);
retryClientDetail_privateMethod.setAccessible(true);
retryClientDetail_privateMethod.invoke(yourclass.class, null);
}
Related
I upgraded my org.testcontainers:
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.15.1</version>
<scope>test</scope>
</dependency>
but then I can't use the start() method anymore:
public class ComponentTest {
public static final PostgreSQLContainer<?> postgreDBContainer = new
PostgreSQLContainer<>("postgres:13");
static {
postgreDBContainer.start();
}
}
Is there any solution to that issue?
May be you missed additional dependency?
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.15.1</version>
<scope>test</scope>
</dependency>
I'm trying to use DBCPConnectionPool service in my custom processor. So, how to use in-built controller services (which are already available on NiFi) in our custom processors.
Here are my properties in *-nar/->pom.xml
<dependencies>
<dependency>
<groupId>org.sample.com</groupId>
<artifactId>nifi-customprocessor-processors</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-standard-services-api-nar</artifactId>
<version>1.14.0</version>
<type>nar</type>
</dependency>
</dependencies>
Here are my dependencies in *processors/->pom.xml
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-utils</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-mock</artifactId>
<version>1.14.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-dbcp-service</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-dbcp-service-api</artifactId>
<version>1.14.0</version>
</dependency>
And here is my propertydescriptor for DBCPConnetionPool service :-
public static final PropertyDescriptor DBCPConnectionPool_SERVICE = new PropertyDescriptor.Builder()
.name("DBCPConnectionPoolLookup Service")
.description("The Controller Service to use in order to establish a connection")
.required(true)
.dynamic(true)
.identifiesControllerService(DBCPService.class)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.build();
and in my OnTrigger method I didn't used it as :-
DBCPService DBCPService = context.getProperty(DBCPConnectionPool_SERVICE)
.asControllerService(DBCPService.class);
Connection con = DBCPService.getConnection();
and was trying to use this con object as in regular passion createStatement->executeQuery.
When I tried to package it using mvn clean install it started throwing error as : A required class was missing while executing org.apache.nifi:nifi-nar-maven-plugi
n:1.3.1:nar: org/apache/nifi/record/sink/RecordSinkService
then I added the following dependencies in processors/->pom.xml
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-record-sink-service</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-record-sink-api</artifactId>
<version>1.14.0</version>
</dependency>
This time it was new error as :- Failed to create Extension Definition for CONTROLLER_SERVICE org.apache.nifi.record.sink.lookup.RecordSinkServiceLookup: Null
PointerException
I also checked by commenting out code which I have written, to check the way I'm using that property was wrong but, even that didn't worked.:-
DBCPService DBCPService = context.getProperty(DBCPConnectionPool_SERVICE)
.asControllerService(DBCPService.class);
Connection con = DBCPService.getConnection();
Can someone help me out in order to use DBCPConnectionPoolService controller service in my custom processor?
I had the same issues the other day and I solved it by using AbstractControllerService instead of DBCPConnectionPool therefore not needing org.apache.nifi.dbcp.DBCPService anymore. Therefore my code would look like this:
MyService DBCPService = context.getProperty(DBCPConnectionPool_SERVICE)
.asControllerService(MyService.class);
Connection con = DBCPService.getConnection();
MyService is the custom interface that extends ControllerService.(you have to use it when you also define the PropertyDescriptor DBCPConnectionPool_SERVICE)
And StandardMyService the class that extends AbstractControllerService and implements MyService and getConnection() from it.
We upgraded to spring-webmvc 5.2.3.RELEASE.
This is causing a JUnit to fail.
I think the issue is with the .build() method - it can't be found.
The code:
#RunWith(MockitoJUnitRunner.class)
public class CommunicationDeliveryControllerTest {
#InjectMocks
CommunicationDeliveryController controller = new CommunicationDeliveryController();
#Mock
private RequestHandler requestHandler;
#Mock
private HttpServletRequest httpServletRequest;
private MockMvc mockMvc;
private Gson gson = new Gson();
private CommunicationDeliveryController spiedController;
#Before
public void setup() {
spiedController = spy(controller);
mockMvc = standaloneSetup(spiedController).build();
}
#Test
public void initiateBatchRunTest() throws Exception{
MvcResult result = mockMvc.perform(post("/api/BatchRun").contentType(MediaType.APPLICATION_JSON_VALUE).content(gson.toJson(BulkCommunicationRequestData.getBulkEmailRequestForPositive()))).andReturn();
assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus());
}
#Test
public void deliverCommunicationTest() throws Exception{
MvcResult result = mockMvc.perform(post("/api/deliverCommunication").contentType(MediaType.APPLICATION_JSON_VALUE).content(gson.toJson(BulkCommunicationRequestData.populateAllEmailDetail()))).andReturn();
assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus());
}
}
The error:
java.lang.NoSuchMethodError: org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StandaloneConfiguration.getInterceptors()[Ljava/lang/Object;
at company.custcomm.service.communicationdelivery.controllers.CommunicationDeliveryControllerTest.setup(CommunicationDeliveryControllerTest.java:45)
Are there compatibility issues between our versions of spring-webmvc, mockito, and JUnit?
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
I upgrade all spring dependencies to 4.3.18-RELEASE, then there is no error any more.
Is there a specific version of spring-test in your dependencies?
I ran into a similar problem a while ago - turned out that there was a strict version limit for spring-test, and upgrading it to 5.2.3-RELEASE resolved the issue.
If you do not want to change the version of your Junit and spring-webmvc then you can specify spring-test version explicitly in your pom.xml. This works for me.
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.0.RELEASE</version>
<scope>test</scope>
</dependency>
I am trying to use gremlin to interact with my Neo4j community database. I am working in eclipse using maven.
Following the documentation in http://tinkerpop.apache.org/docs/3.1.0-incubating/#neo4j-gremlin i set up my dependencies to:
<dependencies>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-cypher</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ha</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-core</artifactId>
<version>3.1.0-incubating</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>neo4j-gremlin</artifactId>
<version>3.1.0-incubating</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-tinkerpop-api-impl</artifactId>
<version>0.1-2.2</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-tinkerpop-api</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
After executing:
package test.gremlin.maven;
import org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jGraph;
import org.apache.tinkerpop.gremlin.structure.Graph;
public class TestGremlin {
public static void main(String[] args) {
Graph graph = Neo4jGraph.open("/tmp/gremlintest");
}
}
i get the following error:
Exception in thread "main" java.lang.ClassCastException: org.neo4j.function.Functions$$Lambda$24/252651381 cannot be cast to org.neo4j.helpers.Function
at org.neo4j.helpers.Settings$DefaultSetting.apply(Settings.java:846)
at org.neo4j.kernel.configuration.ConfigurationValidator.validate(ConfigurationValidator.java:50)
at org.neo4j.kernel.configuration.Config.replaceSettings(Config.java:204)
at org.neo4j.kernel.configuration.Config.<init>(Config.java:92)
at org.neo4j.kernel.impl.factory.PlatformModule.<init>(PlatformModule.java:124)
at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.createPlatform(GraphDatabaseFacadeFactory.java:177)
at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.newFacade(GraphDatabaseFacadeFactory.java:124)
at org.neo4j.kernel.impl.factory.CommunityFacadeFactory.newFacade(CommunityFacadeFactory.java:40)
at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.newFacade(GraphDatabaseFacadeFactory.java:108)
at org.neo4j.graphdb.factory.GraphDatabaseFactory.newDatabase(GraphDatabaseFactory.java:130)
at org.neo4j.graphdb.factory.GraphDatabaseFactory$1.newDatabase(GraphDatabaseFactory.java:118)
at org.neo4j.graphdb.factory.GraphDatabaseBuilder.newGraphDatabase(GraphDatabaseBuilder.java:185)
at org.neo4j.tinkerpop.api.impl.Neo4jFactoryImpl.newGraphDatabase(Neo4jFactoryImpl.java:44)
at org.neo4j.tinkerpop.api.Neo4jFactory$Builder.open(Neo4jFactory.java:32)
at org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jGraph.<init>(Neo4jGraph.java:131)
at org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jGraph.open(Neo4jGraph.java:145)
at org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jGraph.open(Neo4jGraph.java:154)
at test.gremlin.maven.TestGremlin.main(TestGremlin.java:10)
Can you try to use the latest release: which is 0.3-2.3.3
see: https://github.com/neo4j-contrib/neo4j-tinkerpop-api-impl/tree/0.3-2.3.3
I have a custom JSP tag
public class HappyTag extends TagSupport { ... }
and now I need to test it.
So I've created a simple JUnit test:
#Test
public void testTag() {
HappyTag tag = new HappyTag();
}
and I get the following error:
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/jsp/tagext/TagSupport
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
(.....)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I am using maven to build and test my application, tags included.
My current dependencies of the tags sub-module are:
<dependencies>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
plus some dependencies from the parent module:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.portlet</groupId>
<artifactId>portlet-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.portletfaces</groupId>
<artifactId>portletfaces-bridge-impl</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
Those dependencies are enough to compile and use the tags in my JSP. They are not enough to test the tag :-(
So, what did I do wrong?
Kindest regards,
Q.
the Java EE maven dependencies only provides stubs to help compile code which requires those interfaces. IT doe snot provide the implementation. Hence you cannot run them. You need to either work around to mock the behavior (which might end up refactoring your code a lot), or you need to functionally run the test using a dependency which provides a full implementation of the API's.
Maybe this answer might help.
You can use mockito and or power mock to mock servlet context and jsp page context. Mock each method that your tag calls to properly function(doTag). You need to add the mocking framework and javax.el as test dependencies.
Neel is right. You need to mock the ServletContext if you don't want to use the full javaee.jar.
If you are using Spring Test you can do it in the following way:
public class MyCustomTagTest {
private MockServletContext mockServletContext;
private MockPageContext mockPageContext;
#BeforeEach
void setUp() {
mockServletContext = new MockServletContext();
mockPageContext = new MockPageContext(mockServletContext);
}
#Test
void testMyTag() throws Exception {
MyCustomTag myCustomTag = new MyCustomTag();
myCustomTag.setPageContext(mockPageContext);
myCustomTag.doStartTag();
String output = ((MockHttpServletResponse) mockPageContext.getResponse()).getContentAsString();
// do your assertions on output
}
}