I want to mock userRepository using when() method but the method itself is not available
The package import static org.mockito.Mockito.when is not available in the jar downloaded by Spring boot
Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.bapan</groupId>
<artifactId>SpringBoot-Mockito</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
</project>
/**
*
*/
package org.bapan.api.test;
import org.bapan.repository.UserRepository;
import org.bapan.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
/**
*
*
*/
#RunWith(SpringRunner.class)
#SpringBootTest
public class SpringBootApiTest {
/**
*
*/
#Autowired
private UserService userService;
/**
*
*/
#MockBean
private UserRepository userRepository;
#Test
public void getAllUsers() {
//when(userRepository.findAll()).thenReturn();
}
}
When method it self is not present.
In which jar I can find the when method()?
Which exact dependencies need to be added to get the when method
According to the mvnrepo page, spring-boot-starter-parent includes mockito-core 2.23.4.
As usual it should be found in that jar.
Javadoc of the Mockito class Version 2.23.4.
If you can't resolve any mockito related class in your testsource directory, you might need to properly configure maven (or your project) to tell it which the testSourceDirectory is (under the build section / see link).
If that does not help check the jar from your classpath, maybe its broken and you need to force maven to redownload it.
(Note: I am not seeing an import in your file, but I guess you removed it, because it was not working)
Related
I am getting the exception in subject on IntelliJ and have no idea why.
This is the runner file:
#RunWith(Cucumber.class)
#CucumberOptions(
features = "src/main/resources/features",
glue = {"DemoDefinitions"},
tags = "#tests"
)
public class CucumberRunner {}
This is the definitions file:
import cucumber.api.java.en.Given;
public class DemoDefinitions {
#Given("Login to Azure Succeeded")
public void login_to_Azure_Succeeded() {
// Write code here that turns the phrase above into concrete actions
throw new cucumber.api.PendingException();
}
}
This is the feature file:
#tests
Feature: PoC Feature
Scenario: PoC Operations Scenario
Given Login to Azure Succeeded
And the maven dependencies are:
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.2.6</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
When I run execute the runner class I get:
java.lang.NoSuchMethodError:
cucumber.runtime.formatter.Plugins.(Ljava/lang/ClassLoader;Lcucumber/runtime/formatter/PluginFactory;Lcucumber/api/event/EventPublisher;Lio/cucumber/core/options/PluginOptions;)V
When I run the feature file itself I get:
Undefined scenarios:
/C:/Users/talt/IdeaProjects/Poc/src/main/resources/features/poc.feature:5
PoC Operations Scenario
1 Scenarios (1 undefined) 1 Steps (1 undefined)
Can you please advise what is the problem?
You are mixing different versions of Cucumber. Take a careful look at the version numbers.
You are also including more dependencies then strictly needed. Merely using cucumber-java and cucumber-junit would be sufficient. Both cucumber-core and junit are transitive dependencies.
After you fix your dependencies makes sure to reimport the maven project.
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.4.0</version>
</dependency>
</dependencies>
The solution was to set glue to empty string:
glue = {""}
We shall not mix direct & transitive dependencies, specially their versions! Doing so can cause unpredictable outcome.Below are few errors being reported by people due to wrong use of dependencies.
The import cucumber.api.junit cannot be resolved
java.lang.NoClassDefFoundError: gherkin/IGherkinDialectProvider
import cucumber.api.DataTable; cannot be resolved
Solution: You shall add right set of cucumber dependencies.
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>4.2.6</version>
<scope>test</scope>
</dependency>
Second you could say we did not have correct path pointing to glue. But just making it empty shall not be one of the solution even though it worked. We shall have correct path here, not empty string.
I have a JEE project that is deployed on wildfly 10 smoothly.
I'm trying to write junit tests for my CDI beans with cdi-unit library (http://bryncooke.github.io/cdi-unit/)
My pom.xml looks like the following:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jglue.cdi-unit</groupId>
<artifactId>cdi-unit</artifactId>
<version>4.0.1</version>
<scope>test</scope>
</dependency>
However my sample test:
#RunWith(CdiRunner.class)
public class SampleTest {
#Inject
private Foo foo;
#Test
public void testFoo(){
foo.bar();
}
}
fails to initialize due to missing weld class:
java.lang.NoClassDefFoundError: org/jboss/weld/environment/se/Weld
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getConstructor0(Class.java:3075)
at java.lang.Class.getConstructor(Class.java:1825)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.jboss.weld.environment.se.Weld
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 17 more
CDI-UNIT manual says here that it automatically uses it's own version of Weld, hence there is no need to explicitly add any weld libraries in pom.xml.
Any help please?
You should include weld-se into your maven dependencies.
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
</dependency>
add
as is fitting the version your wildfly-weld uses.
If you want more than the Simulation of the CDI-Container but also some have a realistic simulation of EJB-Functionality like persistence, transactionality,
message driven beans, try ejb-cdi-unit
It is derived from cdi-unit.
There you can also find some example-projects which might help with your cdi-unit-issues.
After have upgraded primefaces form 2.2.1 to 4.0 version (same with 5.0 version), when navigating to /abtest.xhtml page deployed on tomcat 7, getting the following error :
javax.servlet.ServletException: /abtest.xhtml: Property 'onFlowProcess' not found on type com.web.faces.abtest.ABTestWizard
javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)
In pom.xml have:
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.7</version>
</dependency>
In abtest.xhtml have:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
...
xmlns:p="http://primefaces.prime.com.tr/ui"
...
<p:wizard id="abtestwizard" widgetVar="wiz" flowListener="#{abwizard.onFlowProcess}" showStepStatus="false" effect="false" showNavBar="false" onback="jQuery('.equalHeight').equalHeights();" onnext="jQuery('.equalHeight').equalHeights();">
And in com.web.faces.abtest.ABTestWizard:
#Component("abwizard")
#Scope("view")
public class ABTestWizard implements Serializable {
...
public String onFlowProcess(org.primefaces.event.FlowEvent event) { ...
}
...
}
The flowListener attribute of the p:wizard tag seems to be set properly, so no idea why this error occurs.
Would appreciate any any idea or hint to solve the problem.
Do you have this in your pom.xml as well?
<repository>
<id>prime-repo</id>
<name>PrimeFaces Maven Repository</name>
<url>http://repository.primefaces.org</url>
<layout>default</layout>
</repository>
You also might be missing some imports in your bean. Which imports do you have listed?
Im trying to add the modelgen jar to my maven project to enable automatic Metamodel class generation however I am getting the following error when I try to compile the project:
Failed to execute goal org.bsc.maven:maven-processor-plugin:2.2.4:process (process) on project Project-per: Error executing: java.lang.RuntimeException: java.lang.SecurityException: class "org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProperties"'s signer information does not match signer information of other classes in the same package
As best as I can determine there is most likely a miss match in the JAR signatures causing this. However I don't understand why or how to fix the problem.
My POM (Relevant parts only)
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0-RC2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.nosql</artifactId>
<version>2.5.0-RC2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen</artifactId>
<version>2.4.2</version>
</dependency>
NB. Everything works fine with the nosql and the eclipselink, its just when I add the modelgen.
OK, as best I can tell I was right, the problem was caused by a signature difference in the JAR files. How this can happen on a library as popular as eclipselink is beyond me but hey.
My solution was to use a snapshot as they are generally not signed. Given that I was unable to obtain snapshot from central I used the following repo to obtain it;
<repository>
<id>sonatype-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen</artifactId>
<version>2.6.0-SNAPSHOT</version>
</dependency>
Add following dependency into pom
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<versionRange>[2.2.4,)</versionRange>
<goals>
<goal>process</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
I'm trying to push a message from server to client that way :
PushContext pushContext = PushContextFactory.getDefault().getPushContext();
pushContext.push("/registrationEvent", "There was another registration");
My problem is that i've the following error
ava.lang.NoClassDefFoundError: Could not initialize class org.primefaces.push.PushContextFactory
But I think that this is due to a issue at the initialization of the projet :
java.lang.NoClassDefFoundError: org/atmosphere/cpr/AsyncSupportListenerAdapter
I've try to add jar atmosphere file... Without success. Have I done something wrong ? I'm using glassfish 3.1.
Thanks !
Primefaces MigrationGuide inform: "PrimeFaces Push is reimplemented, PushContext is deprecated, use EventBus instead along with the new Push API."
In this case, on pom.xml, put the 2.2.1 atmosphere version. Probably you are using a old atmosphere version. If you trying to use the Primefaces 5.0, put the code below:
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.0</version>
</dependency>
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-runtime</artifactId>
<version>2.2.1</version>
</dependency>