Powermock with Mockito - java

Im trying to use PowerMock with Mockito, but PowerMock.replayAll(); and PowerMock.verifyAll(); is not found in my Eclipse environment. Used this download link:
http://code.google.com/p/powermock/downloads/detail?name=powermock-mockito-junit-1.5.zip&can=2&q=
And downloaded EasyMock here:
http://sourceforge.net/projects/easymock/files/EasyMock/3.1/easymock-3.1.zip/download
Added all the jars to my libs directory (removed it from my build path). Anyone? Thanks!
Source code Android project and test project:
https://github.com/powder366/test
https://github.com/powder366/testtest
example.
package com.test
import static org.junit.Assert.*;
import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.PowerMockUtils;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest(Greeter.class)
public class MockStaticExampleTest {
#Test
public void mockStaticExample() throws Exception {
String expectedGreeting = "greeting";
String nameToGreet = "name";
PowerMockito.mockStatic(Greeter.class);
EasyMock.expect(Greeter.getGreeting(nameToGreet)).andReturn(expectedGreeting);
PowerMock.replayAll();
String actualGreeting = Greeter.getGreeting(nameToGreet);
PowerMock.verifyAll();
assertEquals("Expected and actual greeting did not match", expectedGreeting, actualGreeting);
}
}

You have not to add your jars to the Eclipse build path, but you have to place the jars inside the libs folder. ADT will do what takes to import it.

I finally downloaded:
http://code.google.com/p/powermock/downloads/detail?name=powermock-easymock-1.5-full.jar&can=2&q=
and it worked with PowerMock.replayAll(); and PowerMock.verifyAll();

Related

How can I call test method from jar file to run mock script

I have a deployment of a spring-based corporate project, I did everything and now I just need to run the data seeding script, but I don’t understand how to do it at all
The instructions say:
Run test build io.groobit.utils.mock_data_init.ScenarioTest#scenario1
run mock-init.jar in folder (not used run test in "Itelij idea" To avoid such an error .. see image)
Run build mock-init.jar "java -jar mock-init.jar"
I have run tests many times in intelij idea by clicking the desired method in the test folder, but how to proceed here in the command to call it?
package io.groobit.utils.mock_data_init;
import io.groobit.api.client_api.dto.BusinessClientBasicInfo;
import io.groobit.api.client_api.dto.Payment;
import io.groobit.utils.mock_data_init.*;
import io.groobit.utils.mock_data_init.utils.AuthUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import java.math.BigDecimal;
import java.time.Instant;
#Slf4j
#Disabled
#SpringBootTest
#DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class ScenarioTest {
#Test
public void scenario1() throws Exception {
//call this it
}
}
Path test: src/test/java/io/company/utils/mock_data_init/ScenarioTest.java
Any idea?

Cannot resolve symbol 'Before' - Intellij jUnit

I am new to Intellij IDEA. I'm trying to use jUnit annotations #Before and #After for my selenium tests. Even though I'm importing jUnit in my class, I 'm not able to use the annotations. Any help?
package Config;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
public class browserConfig {
public static WebDriver driver = null;
public static Properties config = new Properties();
public static FileInputStream fis;
#Before
public void initBrowser(){
if (driver ==null){
try{
fis = new FileInputStream(System.getProperty("user.dir") + "//src//main//java//Config//config.properties");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Your test should be in the test folder, otherwise idea will ignore the test annotations.
Make sure the class is placed in the src/test/ folder.
Also check in Project Settings -> Modules that your tests folder is present in the structure and has green color (marked as Tests). If it is missing - add it to the modules and mark as Tests.
If you are using gradle to build in Intellij, try adding this to build.gradle.
testCompile('org.springframework.boot:spring-boot-starter-test')
If you aren't using gradle do something similar with whatever controls your dependencies.

Why do I get an error "package org.mockito.runners does not exist"?

I have pluged up require dependency
testCompile 'org.mockito:mockito-core:1.10.19'
Then I put my test code to /src/test/java/ directory
then I have tried launch such test
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class PresenterActivityAcceptNotAcceptTest {
#Test
public void emailValidator_CorrectEmailSimple_ReturnsTrue() {
boolean dd = true;
assertThat(dd, is(true));
}
it works properly, but if I add anything witch associated with Mock lib
for example #RunWith
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
#RunWith(MockitoJUnitRunner.class)
public class PresenterActivityAcceptNotAcceptTest {
#Test
public void emailValidator_CorrectEmailSimple_ReturnsTrue() {
boolean dd = true;
assertThat(dd, is(true));
}
I got such error
Error:Execution failed for task ':Application:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Error:(10, 10) error: cannot find symbol class MockitoJUnitRunner
Error:(5, 27) error: package org.mockito.runners does not exist
/home/aleksey/Downloads/NTZ/FittingRoom/Application/src/test/java/com/fittingroom/newtimezone/presenters/PresenterActivityAcceptNotAcceptTest.java
What am I doing wrong?
If I forger about something feel free to ask
Thanks in advance!
It looks like Gradle is not doing it's job.
Manually adding the jars may fixed the problem.
How to Download and Install jar go here .
and for download mockito use this link
https://mvnrepository.com/artifact/org.mockito/mockito-core/1.10.19
As stated in Baeldung's article, starting from Mockito version 2.2.20, the package for MockitoJUnitRunner has changed. So change :
import org.mockito.runners.MockitoJUnitRunner;
To :
import org.mockito.junit.MockitoJUnitRunner;
As usual, you have to import the mockito-core library in your build.gradle :
dependencies {
testImplementation 'org.mockito:mockito-core:4.2.0'
}
As a side note, you will also need to change import for Matchers if you use them. For instance :
From :
import static org.mockito.Matchers.any;
To :
import static org.mockito.Mockito.any;
Got the same issue. Attempted to implement the developer.android documentation example.
Fixed by changed org.mockito version to the recent at the time in build.gradle :
dependencies {
testImplementation 'org.mockito:mockito-core:2.28.2'
}
Open File > Project Structure...
and then add it manually as library dependency:
I also got this exception when I tried to write instrumentalTests in src/androidTest/java/ but by default mockito's scope in dependencies is set as Unit Test implementation (you can see that in File => Project Structure => Modules => Dependencies). I've just changed this parameter to Test implementation and it works!
does your project look like this?:

Why is my import of the containsString method not working?

I've written the simple Java script below in order to learn more about TDD, IntelliJ and Java itself.
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.both;
public class JUnit_Dummy {
private StringJoiner joiner;
private List<String> strings;
#Before
public void setUp() throws Exception {
strings = new ArrayList<String>();
joiner = new StringJoiner();
}
....
#Test
public void shouldContainBothStringsWhenListIsTwoStrings() {
strings.add("one");
strings.add("two");
assertThat(joiner.join(strings),
both(containsString("A")).
and(containsString("B")));
}
}
_____________
import java.util.List;
public class StringJoiner {
public String join(List<String> strings) {
if(strings.size() > 0) {
return (strings.get(0);
}
return "";
}
}
I'm trying to use the "containsString" method inside an assertion, but IntelliJ keeps telling me that it "cannot resolve method 'containsString(java.lang.String)". This despite the fact that the jUnit docs (http://junit.sourceforge.net/javadoc/org/junit/matchers/JUnitMatchers.html#containsString(java.lang.String)) tell me that this method does accept a String parameter.
I've tried swapping out various import statements, including the following:
import static org.hamcrest.Matcher.containsString;
import static org.hamcrest.Matcher.*;
import static org.hamcrest.CoreMatchers.*;
The best that I get is a greyed-out import statement telling me that the import statement is unused. Not sure what the problem is, any help would be appreciated.
UPDATE:
Here is the exact compiler error:
java: cannot find symbol
symbol: method containsString(java.lang.String)
location: class JUnit_Dummy
I thought I had tried every worthwhile import statement already, but this one did the trick:
import static org.junit.matchers.JUnitMatchers.*;
I faced the same issue with a Spring Boot app.
Seems like this is a dependency ordering issue.. one of the dependencies mentioned in pom.xml before the "spring-boot-starter-test" artifact was overriding the hamcrest version.
So all I did was change the order (moved this dependency up):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
I'm using Spring Boot 1.5.7.RELEASE.
We are supposed to use containsString method of hamcrest library.
My suggestion would be to stick to Junit 4 and import hamcrest library 1.3 in your build path. This would do the trick.
This will allow you to access other features of hamcrest library as well.
The solution can also be found by adding the required static imports manually. Or you can configure the required static imports in favorites tab of eclipse.
try this instead
import static org.hamcrest.CoreMatchers.*;
I'm working with MAVEN - doing a tutorial and I ran into this same issue.
I used the "import static org.hamcrest.CoreMatchers.*;" solution and that failed.
So I then moved JUNIT to be first on the list in the POM file - and that solved it.

Unable to find/import Runwith Class in org.junit.runner?

The below error appears, when I import Runwith, in org.junit.runner...
The attribute value is undefined for the annotation type Runwith
It gave me the suggestion for creating a class in the Junit-Runner package...I don't know, what should I put in that code...
Junit-4.10 is there in libraries and Build Path. I am using it and am not sure whether it is an issue with Junit or Selenium...Any suggestions pls..??
This is the code, I am using and getting the error where the #Runwith is used.(Line #2 and #5)
package package1;
import org.junit.runner.Runwith;
import org.junit.runners.Suite.SuiteClasses;
#Runwith(Suite.Class)
#SuiteClasses({WPHomePage.class, WPRegisterpage.class})
public class BatchJavaFile {
}
#Runwith(Suite.Class) should be #RunWith(Suite.class) (note capitalization)
You probably also need another import
import org.junit.runners.Suite;

Categories