I am attempting to mock intent inside the function I made. Here is the function below
fun learningUnitTest(context: Context) {
val str = context.getString(R.string.app_name)
val intent = Intent(context, TodoFragment::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
} // it resulting null. how to mock this?
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
Log.d("AA", str)
Log.d("BB", pendingIntent.toString())
}
And this is my unit test file
import android.content.Context
import android.content.Intent
import android.util.Log
import com.example.todoapp.controller.worker.learningUnitTest
import io.mockk.*
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito.*
class ExampleUnitTest {
private val FAKE_STRING = "FAKE"
#Mock
private lateinit var mockContext: Context
#Test
fun test_function() {
mockkStatic(Log::class)
mockkStatic(Intent::class)
mockContext = mock(Context::class.java)
every { Log.d(any(), any()) } returns 0
`when`(mockContext.getString(R.string.app_name))
.thenReturn(FAKE_STRING)
// every { Intent("aa") } returns mockIntent // Missing mocked calls inside every { ... }
learningUnitTest(mockContext)
verify(mockContext, times(1)).getString(R.string.app_name)
}
}
And not forgetting the dependencies which I used for testing, hopefully it has the correct version. I don't think I have to show any depedencies or any setup which are not related to the test file.
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:1.10.19'
testImplementation 'io.mockk:mockk:1.12.2'
testImplementation 'org.mockito:mockito-inline:3.8.0'
When I ran the test, it resulting an error like this
Method setFlags in android.content.Intent not mocked. See http://g.co/androidstudio/not-mocked for details.
java.lang.RuntimeException: Method setFlags in android.content.Intent not mocked. See http://g.co/androidstudio/not-mocked for details.
at android.content.Intent.setFlags(Intent.java)
at com.example.todoapp.controller.worker.WorkerUtilsKt.learningUnitTest(WorkerUtils.kt:97)
at com.example.todoapp.ExampleUnitTest.test_function(ExampleUnitTest.kt:35)
I even tried to mock using every for only the Intent::class.java accessing Intent("test") for the code like this every { Intent("aa") } returns mockIntent. But it still resulting an error.
Related
How to configure dagger to inject groovy classes, and to inject into groovy classes?
I was initially trying to get dagger to inject a groovy class into my java app, and I found dagger was complaining the groovy class is not found. Looking at the log, it seems that compileGroovy happens after compileJava. And the annotation processing of dagger compiler seems to be in compileJava. I guessed that might be the problem -- no groovy classes are available at this time. But I've yet figured out a way to coerce either of dagger or groovy to work with the other.
It seems I could not upload a .tar.gz. But if anyone needs a minimal demo code for what I meant to achieve, these might help (with gradle 7):
build.gradle:
plugins {
id 'groovy'
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
annotationProcessor 'com.google.dagger:dagger-compiler:2.+'
implementation 'com.google.dagger:dagger:2.+'
implementation 'org.codehaus.groovy:groovy-all:3.+'
}
settings.gradle:
rootProject.name = 'groovy-dagger1'
src/main/groovy/org/example/dagger/MainComponent.groovy:
package org.example.dagger
import dagger.Component
#Component(modules = [
MainModule,
])
interface MainComponent {
String message();
}
src/main/groovy/org/example/dagger/MainModule.groovy:
package org.example.dagger
import dagger.Module
import dagger.Provides
#Module
final class MainModule {
#Provides
static String message() {
return 'Hello Groovy Dagger!'
}
}
src/main/groovy/org/example/main/Main.groovy:
package org.example.main;
class Main {
static void main(String[] args) {
// Dagger component does not exist :/
// println DaggerMainComponent.create().message()
}
}
By default, the groovy compiler will not run the java annotation processors...
You can add this to your build.gradle:
compileGroovy {
groovyOptions.javaAnnotationProcessing = true
}
You will of course need to add an import
import org.example.dagger.DaggerMainComponent
To Main.groovy
I'm trying to write a test case for a query dsl, I'm getting null pointer exception when I run the test case
Dsl Class
QMyClass myClass= QMyClass.myClass;
queryFactory = new JPAQueryFactory(em);
JPAQuery<?> from = queryFactory.from(myClass);
JPAQuery<?> where = from
.where(prepdicates);
orderBy(orderSpecifier).offset(sortOrder.getOffset())
.limit(sortOrder.getPageSize());
**Junit test case:**
import javax.inject.Provider;
import javax.persistence.EntityManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.data.jpa.repository.support.QueryDslRepositorySupport;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.JPQLTemplates;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
#RunWith(PowerMockRunner.class)
public class MyClass{
#Mock
QueryDslRepositorySupport queryDslRepositorySupport;
#Mock
EntityManager entityManager;
#Mock
JPAQueryFactory queryFactory;
#Mock
JPAQuery step1;
#InjectMocks
MyClass myClass;
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
Provider<EntityManager> provider = new Provider<EntityManager>() {
#Override
public EntityManager get() {
return entityManager;
}
};
queryFactory = new JPAQueryFactory(JPQLTemplates.DEFAULT, provider);
}
#SuppressWarnings({ "rawtypes", "unchecked" })
#Test
public void sampleTest() throws Exception {
QMyClass class= Mockito.mock(QMyClass.class);
Mockito.when(queryFactory.from(class)).thenReturn(step1);
Predicate step2 = Mockito.mock(Predicate.class);
Mockito.when(step1.where(step2)).thenReturn(step1);
OrderSpecifier step3 = Mockito.mock(OrderSpecifier.class);
Mockito.when(step1.orderBy(step3)).thenReturn(step1);
Mockito.when(step1.offset(Mockito.anyLong())).thenReturn(step1);
Mockito.when(step1.limit(Mockito.anyLong())).thenReturn(step1);
myClass.method("");
}
}
When I run this test case I'm getting null pointer exception at line number 2 in sampleTest() method. I googled but did't find any article for this, not sure why this NE, even after mocking the queryfacotry
Here is the trace :
java.lang.NullPointerException
at com.querydsl.core.DefaultQueryMetadata.addJoin(DefaultQueryMetadata.java:154)
at com.querydsl.core.support.QueryMixin.from(QueryMixin.java:163)
at com.querydsl.jpa.JPAQueryBase.from(JPAQueryBase.java:77)
at com.querydsl.jpa.impl.JPAQueryFactory.from(JPAQueryFactory.java:116)
at
As I mentioned in the comment, your problem is that you do not use a mock, but the real object instead.
Remove the queryFactory initialisation from your setup method.
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
Provider<EntityManager> provider = new Provider<EntityManager>() {
#Override
public EntityManager get() {
return entityManager;
}
};
// remove this line
// queryFactory = new JPAQueryFactory(JPQLTemplates.DEFAULT, provider);
}
Also you need to change your implementation, you can not use
queryFactory = new JPAQueryFactory(em); inside your code, as it can not be mocked.
What you could do instead is having a method that returns the JPAQueryFactory,
either from another class - which you can mock -
or in the same method, then you would need to spy on your class instead.
As you didnt add the code for the class you want to test (MyClass - hopefully a different one from the identical named UnitTest?), another possibility would be that you try to use Field or Constructor Injection (as indicated by the use of your annotations), but then there should not be an object creation for the JPAQueryFactory in your code at all.
This also seems to be wrong:
You should inject mocks into your class under test, not in your UnitTest class.
#RunWith(PowerMockRunner.class)
public class MyClass{
...
#InjectMocks
MyClass myClass;
Maybe this is very simple, but I couldn't find any examples on the web:
I'd like to use JUnit 5 to run a unit test implemented as a Groovy class. My current setup seems to launch JUnit 5, but fail to detect the test case. IntelliJ recognizes the test, but fails to run it. If I add a Java unit test, it is launched correctly.
Here's what I have now:
Project structure
src
main
groovy
# production code
test
groovy
UnitTest.groovy
build.gradle
...
build.gradle
plugins {
id 'groovy'
}
dependencies {
compile localGroovy()
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.1'
}
test {
useJUnitPlatform()
}
UnitTest.groovy
import org.junit.jupiter.api.Test
class UnitTest {
#Test
def shouldDoStuff() {
throw new RuntimeException()
}
}
I'm using Gradle 4.10.
Any ideas?
JUnit requires all testing method to use return type void. Groovy's def keyword is compiled to an Object type, so your method compiles to something like this in Java:
import org.junit.jupiter.api.Test
public class UnitTest {
#Test
Object shouldDoStuff() {
throw new RuntimeException();
}
}
If you try this out as a Java test, it won't find the test case neither. The solution is very simple - replace def with void and your Groovy
test case will be executed correctly.
src/test/groovy/UnitTest.groovy
import org.junit.jupiter.api.Test
class UnitTest {
#Test
void shouldDoStuff() {
throw new RuntimeException()
}
}
Demo:
I have no idea why this error exists:
Program type already present: org.hamcrest.CoreMatchers
Message{kind=ERROR, text=Program type already present: org.hamcrest.CoreMatchers, sources=[Unknown source file], tool name=Optional.of(D8)}
My code in in the scope if dependency of build.gradle (Module: app) is:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation ('junit:junit:4.12'){
exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.monkeylearn:monkeylearn-java:0.1.4'
}
MainActivity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.monkeylearn.MonkeyLearn;
import com.monkeylearn.MonkeyLearnException;
import com.monkeylearn.MonkeyLearnResponse;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
MonkeyLearn ml = new MonkeyLearn("*******************************");
String moduleId = "*********";
String[] textList = {"This is a text to test your classifier", "This is some more text"};
MonkeyLearnResponse res = ml.classifiers.classify(moduleId, textList, true);
System.out.println( res.arrayResult );
} catch (MonkeyLearnException e) {
e.printStackTrace();
}
}
}
Do you have any ideas?
There is a similar problem with simple-json in another question that I answered here.
I suggest you do the same for monkeylearn-java and junit or any other non-google dependencies one by one, I mean download their jar files and put them in the libs folder one by one, to find out which one is the problem, and leave it be in libs folder.
I think this is a bug either in Android Studio or in Gradle.
I had a similar problem with simple-json. For that the solution was to exclude the JUnit dependency.
I am not an expert on Android programming but I find it strange that you exclude hamcrest-core from the JUnit testImplementation. I would rather suggest to exclude transitive dependencies from external libs.
For simple-json this was my solution:
implementation('com.googlecode.json-simple:json-simple:1.1.1') {
exclude group: 'junit', module: 'junit'
}
Maybe you can do the same for monkeylearn-java?
What is wrong with my test framework, that I cannot run my test?
my build.gradle
version '1.0-SNAPSHOT'
//task wrapper(type: Wrapper) {
// gebVersion = '0.13.1'
// seleniumVersion = '2.52.0'
//
// distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
//}
apply plugin: 'groovy'
repositories {
mavenCentral()
}
sourceCompatibility = 1.5
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
// geb
compile 'org.codehaus.geb:geb-core:0.7.2'
compile 'org.codehaus.geb:geb-spock:0.7.2'
compile "org.seleniumhq.selenium:selenium-firefox-driver:2.52.0"
compile "org.seleniumhq.selenium:selenium-chrome-driver:2.52.0"
compile "org.seleniumhq.selenium:selenium-safari-driver:2.52.0"
compile "org.seleniumhq.selenium:selenium-support:2.52.0"
// spock
compile "org.codehaus.groovy:groovy-all:2.4.1"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
}
My GebConfig.groovy
//import geb.driver.SauceLabsDriverFactory
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.safari.SafariDriver
//baseUrl = 'http://madison.craigslist.org'
// default driver...
System.setProperty('webdriver.chrome.driver', "../resources/chromedriver")
driver = {new ChromeDriver()}
environments {
'chrome' {
def chromeDriver = new File('src/test/resources/chromedriver') // add .exe for Windows...
System.setProperty('webdriver.chrome.driver', chromeDriver.absolutePath)
driver = { new ChromeDriver() }
}
// 'ff' {
// driver = { new FirefoxDriver() }
// driver.manage().window().maximize()
// }
'safari' {
driver = { new SafariDriver() }
}
}
waiting {
timeout = 6
retryInterval = 0.5
slow { timeout = 12 }
reallySlow { timeout = 24 }
}
reportsDir = "geb-reports"
page class
package pages
import geb.Page
import geb.Browser
class LoginPage extends Page{
static url = "https://qmdev.quickblox.com"
// static at = {heading.isDisplayed()}
static at = {title == "Q-municate"}
static content = {
heading {$("div#l-welcome_block l-welcome_text_description")}
logInByEmailOrSocial {$("button", text:"login by email or social")}
logInPageTitle {$("div.l-welcome_block l-welcome_text_login")}
}
}
test class
import geb.spock.GebReportingSpec
import pages.LoginPage
import spock.lang.*
import geb.spock.GebSpec
#Stepwise
class LoginPageTest extends GebReportingSpec{
def "log in Q-municate"(){
given: "Open Log In page"
to LoginPage
when: "chose log in by email"
LoginPage.logInByEmailOrSocial.click()
then: "Ensure that we are on LogIn page"
LoginPage.logInPageTitle.text() == "Log In"
}
}
About my framework:
I use web-spock-groovy-gradle bundle for web UI automation and I'm a fresh-user with Gradle.
Please tell me, what is wrong with my build.gradle and GebConfig.groovy.
your Geb-Spock version of 0.7.2 is quite old and could be the cause of your problem. I suggest changing those 2 dependencies up to version 1.1.1:
compile 'org.codehaus.geb:geb-core:0.7.2' //change to 1.1.1
compile 'org.codehaus.geb:geb-spock:0.7.2' //change to 1.1.1
Further more I check the Spock API version 1.1 for "org/spockframework/mock/MockController" and it does not appear to exist.
(http://spockframework.org/spock/javadoc/1.1-SNAPSHOT/index.html)
As you can see in this picture there are only 2 classes that are part of the spock.mock package, and MockController is not one of them. try updating your geb version and let us know if that helps