"not all expectations were satisfied" missing under Eclipse - java

We use two different IDEs, Netbeans 8.2 and Eclipse 4.7.2. We are running JMock 2.8.3 with JUnit 4.11 and have a test that fails under Netbeans and Jenkins (using the Netbean's Ant scripts), but passes under Eclipse.
The error is "not all expectations were satisfied".
However, if I add an assertIsSatisfied() call to the end of the test, it will fail with the correct error message under Eclipse.
I can reproduce this with a trivial example:
public class FailureExample {
private static class Example {
public void doSomething() { }
public void doSomethingElse() { }
}
// Mocks
#Rule public JUnitRuleMockery context = new JUnitRuleMockery(){{
setThreadingPolicy(new Synchroniser());
setImposteriser(ClassImposteriser.INSTANCE);
}};
public Example instance;
#Before
public void setUp() throws Exception {
// Mocks
instance = context.mock(Example.class);
}
#Test
public void testExample() {
context.checking(new Expectations() {{
oneOf(instance).doSomething();
oneOf(instance).doSomethingElse();
}});
instance.doSomething();
}
}
Is there something else I need to do in Eclipse to make JMock behave as expected?
Update
Adding screenshot of our project's libraries:
UPDATE
I tried create a new Java project as well as a new Maven project (as described below by Till Brychcy) as those worked. I tried removing all the jar files listed for my project and then readding them, but it failed.
I'm very close to abandoning Eclipse in favor of Netbeans, simply because I have real work to do, not just fighting with Eclipse.

I cannot reproduce your problem.
With both Eclipse 4.7.2 and Eclipse built from the current master I get:
java.lang.AssertionError: not all expectations were satisfied
expectations:
expected once, already invoked 1 time: example.doSomething()
! expected once, never invoked: example.doSomethingElse()
what happened before this:
example.doSomething()
I used the following 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>test</groupId>
<artifactId>jmockbug</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jmockbug</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-junit4</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-legacy</artifactId>
<version>2.8.3</version>
</dependency>
</dependencies>

Related

How to import java project as module in java web project using intelijj?

I wanted to use the class from basic java project inside java servlet class which is defined in another project.
I tried importing project as module through the module dependency InteliJ menu.
At compile time ,it is not giving any error ,but after running the server(Glassfish) and calling the servlet it is giving below error.
java.lang.NoClassDefFoundError: com/practise/LogFileCreator at UserLoginValidator.dbConnectionMaker(UserLoginValidator.java:31)>
Please find below code which causing error.
below class is from web project
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
import com.practise.LogFileCreator;
public class UserLoginValidator extends HttpServlet
{
public String LogFilePath="D:\\Logs";
public PrintWriter out;
String errormsg="";
//********************
LogFileCreator l ;
#Override
public void init() throws ServletException {
try {
this.l = new LogFileCreator(LogFilePath); // here i am trying to create object of my class which causing the mentioned error.
l.WriteLog("Hello");
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
}
}
below class is from normal java project
package com.practise;
import java.io.*;
public class LogFileCreator
{
private String filepath;
private StringBuffer sb = new StringBuffer("Log");
private File file;
private FileWriter fileWriter;
private BufferedWriter bufferedWriter;
public PrintWriter p;
public LogFileCreator(String filepath) throws IOException
{
this.filepath=filepath;
String filename=sb.toString().concat(java.time.LocalDate.now().toString());
this.file = new File(this.filepath,filename);
this.fileWriter= new FileWriter(file,true);
if(!file.exists())
{
file.createNewFile();
}
p= new PrintWriter(fileWriter);
}
public void WriteLog(String logMessage){
p.println(java.time.LocalDateTime.now() + " : " + logMessage);
p.flush();
}
}
Here is the image for module dependency I used .
Image
Earlier I was using LogFileCreator.java class from same web project and it was working fine
Here what i am trying to acheive is ,without writing the LogFileCreator class again in web project ,wants to reuse the class written already inside normal java project to print the logs in desired text file .
Any solution/suggestions would be appreciated.
Thank you!
[Edit 1]
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
<artifactId>webapp</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>9.4.0.jre11</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>Logging</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<!-- String Driver= "com.microsoft.sqlserver.jdbc.SQLServerDriver";-->
<!-- String dbusername="sa";-->
<!-- String dbpassword="Admin#123";-->
<!-- String connectionString="jdbc:sqlserver://localhost:1433;databasename=Users;";-->
<!-- -->
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
Here What I did which resolved the above problem.
First I converted my first basic java project to maven project .
secondly I added maven dependency for the 2nd project (web project) and also added the jar file (of first project classes ) inside the web INF/lib directory of web project using below option from InteliJ .
Image
The server's classloader does not have a copy of LogFileCreator.class so the class is not on it's classpath.
It is probably how the deployment file (assuming war) was packaged. How are you building the file? Maven, Gradle, neither, etc.?
Edit
I wanted to give a bit more clarity for others who might stumble upon a similar issue. During compile time, the project/module in question was imported; however, maven does not know to package that library in the generated war. When it was deployed to Glassfish, the war did not contain the LogFileCreator class because it was not packaged in the war. The authors solution worked because maven is packaging the module into the war and Glassfish can now find the class.

My JUnit tests don't work when I execute them via maven

I use Maven on my test project and I wanted to test test option in Maven's lifecycle, but my JUnit test failed. I have a class named Arithmetics in src.main.java package and a class named ArithmeticsTest in src.test.java package.
When I run ArithmeticsTest on my own using IntelliJ IDEA everything works OK, and I have expected java.lang.AssertionError, so why I don't have such when I run test option in maven?
Console output:
T E S T S
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
src.main.java.Arithmetics.java
public class Arithmetics
{
public static int add(int a, int b)
{
return a + b;
}
}
src.test.java.ArithmeticsTest.java
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class ArithmeticsTest
{
#Test
public void testAdd()
{
assertEquals(4, Arithmetics.add(2, 3));
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
<artifactId>Test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
There are three wrong things I can spot based on your question, while they might not provide a complete answer I hope they'll will be a step in a right direction:
src/main/java and src/test/java is not a package as you write. Its a folder layout supported by maven, basically , if you have a code in com.myorg.somepackage.Arithmetics
Make sure you have the following layout at the level of actual physical folders:
src
main
java
com
myorg
somepackage
Arithmetics.java
test
java
com
myorg
somepackage
ArithmeticsTest.java
For both java files the package must be com.myorg.somepackage:
package com.myorg.somepackage;
public class Arithmetics {}
And
package com.myorg.somepackage;
public class ArithmeticsTest {}
I know it might be a wrong naming that you've used in the question, but still I have to state it because it might cause an actual issue
The second issue is that for some reason you seem to configure your surefire plugin to use test ng which is an alternative to junit. It can happen because testng is placed as a dependency - I can only speculate because you don't really show the full surefire plugin configure and do not provide a full list of dependencies, but you've got and idea I believe :)
This is wrong because you use the junit5 dependencies as well as the imports that correspond to the junit 5.
The dependencies on the junit 5 are completely messy:
You seem to have two dependencies on just the same with the different scope, its really a wrong thing to do. Make sure you use only the test scope and have all the relevant dependencies. Read here for instruction of how to configure the surefire plugin
In addition, for the sake of completeness of the answer, check that you use the recent version of surefire plugin, if its too old the chances are that it won't be able to run jupiter engine (junit 5)
As other answer already pointed out few things which may go wrong in your case, I am just adding the solution to your pom xml.
The surefire plugin version is the main culprit. Default with maven (2.12.4) will not work with junit-5 jupiter engine.
So just add the plugin in your with version 2.22.1 in your pom, it should work after that, assuming your folder structure as per required (see other answer).
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>

OutOfMemoryError while reproducing BioGrakn Text Mining example with client Java

I'm trying to reproduce the BioGrakn example from the White Paper "Text Mined Knowledge Graphs" with the aim of building a text mined knowledge graph out of my (non-biomedical) document collection later on. Therefore, I buildt a Maven project out of the classes and the data from the textmining use case in the biograkn repo. My pom.xml looks like that:
<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>TextMining-BioGrakn</groupId>
<artifactId>TextMining-BioGrakn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>TextMining-BioGrakn</name>
<repositories>
<repository>
<id>repo.grakn.ai</id>
<url>https://repo.grakn.ai/repository/maven/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.grakn.client</groupId>
<artifactId>api</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>io.grakn.core</groupId>
<artifactId>concept</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>io.graql</groupId>
<artifactId>lang</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.9.2</version>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.9.2</version>
<classifier>models</classifier>
</dependency>
</dependencies>
</project>
Migrating the schema, inserting the pubmed articles and training the model works perfectly, but then I got an java.lang.OutOfMemoryError: GC overhead limit exceeded, which is thrown in the mineText() method in the CoreNLP class. This is how the main method in the Migrator class looks like:
public class Migrator {
public static void main(String[] args) {
GraknClient graknClient = new GraknClient("localhost:48555");
GraknClient.Session session = graknClient.session("text_mining");
try {
loadSchema("schema/text-mining-schema.gql", session);
PubmedArticle.migrate(session);
CoreNLP.migrate(session);
} catch (Exception e) {
e.printStackTrace();
session.close();
}
session.close();
graknClient.close();
}
}
Do you have any idea on what could cause this error? Am I missing something fundamental here? Any help is highly appreciated.
It may be you need to allocate more memory for your program.
If there is some bug that is causing this issue then capture a heap dump (hprof) using the HeapDumpOnOutOfMemoryError flag. (Make sure you put the command line flags in the right order: Generate java dump when OutOfMemory)
Once you have the hprof you can analyze it using Eclipse Memory Analyzer Tool
It has a very nice "Leak Suspects Report" you can run at startup that will help you see what is causing the excessive memory usage. Use 'Path to GC root' on any very large objects that look like leaks to see what is keeping them alive on the heap.
If you need a second opinion on what is causing the leak check out the IBM Heap Analyzer Tool, it works very well also.
Good luck!

junit Test with SerenityParameterizedRunner in RunWith annotation: does not find tests (java.lang.Exception: No tests found matching Method)

I'm trying to run a junit test with Serenity BDD framework, using IntelliJ IDEA.
I get an error when I try to run the test:
java.lang.Exception: No tests found matching Method ... from org.junit.internal.requests.ClassRequest#71e693fa
This appears to be due to using RunWith annotation invoking SerenityParameterizedRunner
#RunWith(SerenityParameterizedRunner.class)
When the RunWith annotation is commented out, the test is found and starts executing (though that is not of much use, since we're relying on the Parameterized runner for building data).
I'm able to reproduce the problem with a simple project in order to demonstrate the problem.
package com.home;
public class Doorbell {
private int ringCount = 0;
public Doorbell() {
}
public void ring(){
System.out.println("Ring!");
ringCount++;
}
public int getRings() {
return ringCount;
}
}
Test Class:
package com.home;
import net.serenitybdd.junit.runners.SerenityParameterizedRunner;
import net.serenitybdd.junit.runners.SerenityRunner;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
#RunWith(SerenityParameterizedRunner.class)
public class DoorbellTest {
#Test
public void testRings()
{
Doorbell db = new Doorbell();
db.ring();
db.ring();
Assert.assertEquals(2,db.getRings());
}
}
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>homeproject</groupId>
<artifactId>mytestproject</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<serenity.version>1.9.31</serenity.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.serenity-bdd/serenity-core -->
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-core</artifactId>
<version>1.9.31</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.serenity-bdd/serenity-junit -->
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit</artifactId>
<version>1.9.31</version>
</dependency>
</dependencies>
</project>
Plesae try running the single unit test in the project. Any help much appreciated.
Answer 11 from this Stack Overflow question solved my problem
Apparently you have to run the class containing the test, and not the test itself.

Selenium Maven Setup - One Empty Jar

I'm trying to set up a Java Selenium test using the recommended Maven instructions found here:
http://docs.seleniumhq.org/docs/03_webdriver.jsp
and here:
http://docs.seleniumhq.org/download/maven.jsp
I have maven installed and working.
I've copied the example pom.xml, changing only the project name
<?xml version="1.0" encoding="UTF-8"?>
<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>SeleniumTest</groupId>
<artifactId>SeleniumTest</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.44.0</version>
</dependency>
<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
<version>1.5</version>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Using
mvn clean install
runs without any errors. The target directory is created, containing SeleniumTest-1.0.jar and the maven-archiver directory. The problem is that my Eclipse project can't resolve the Selenium classes. I've copied the example Java driver class, modifying the imports based on my project layout:
import Selenium.*;
import Selenium.target.*;
public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Alternatively the same thing can be done like this
// driver.navigate().to("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "cheese! - Google Search"
System.out.println("Page title is: " + driver.getTitle());
//Close the browser
driver.quit();
}
}
The classes "WebDriver", "WebElement", "WebDriverWait", and "ExpectedCondition" can't be resolved. Trying to use the imports in the example
import Selenium.By;
import Selenium.WebDriver;
import Selenium.WebElement;
import Selenium.firefox.FirefoxDriver;
import Selenium.support.ui.ExpectedCondition;
import Selenium.support.ui.WebDriverWait;
all fail.
I looked into the jar downloaded by Maven, SeleniumTest-1.0.jar, and it is effectively empty. It only contains the META-INF directory.
I feel like I'm missing something obvious, but I just can't figure it out. I feel like I'm missing something in my pom.xml, but I can't find anything on Selenium's site that helps. Can anyone give me a hand?

Categories

Resources