IllegalStateException when using Mockito with IntelliJ's Java - java

The error message I receive:
java.lang.IllegalStateException: Could not initialize plugin:
interface org.mockito.plugins.MockMaker
The code in question:
List<String> mockList = mock(List.class);
The build.gradle dependency:
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
def mockito_version = 'latest.release'
// For local unit tests on your development machine
testCompile "org.mockito:mockito-core:$mockito_version"
}
I have tried looking at other people with the same issue and I keep getting references to PowerMock. I have no idea what that means, so if this is a duplicate I apologize. It just seems like no other question had a solution that resolved my issue. The library is imported properly, as I do not have any compilation errors. Any help would be greatly appreciated.

i tried to create a new project and test this out. below is how my depdencies in gradle file looks:
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile "org.mockito:mockito-core:2.+"
}
below is my test class:
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class TestList {
#Test
public void Test(){
List<String> myList = mock(List.class);
when(myList.get(0)).thenReturn("hello world");
Assert.assertEquals("hello world",myList.get(0));
}
}
this works.

Related

Eclipse refuses to support lombok annotation

I'm using
Eclipse Version: 2021-09 (4.21.0) Build id: 20210910-1417
Lombok: 1.18.12
But Eclipse complains in the "Problems" tab:
"The method getTest() is undefined for the type RootResponse"
The problem class is:
#Getter
#Builder
#ToString
public class RootResponse extends Serializer {
private String test;
// for use in the cleanup routine
#Override
public String[] getDeletionIdentifiers() {
throw new UnsupportedOperationException("Deletion not supported: "+ this.getClass().getName());
}
}
I'm using Gradle, but am far from an expert at it. Nonetheless, I think this is relevant from the build.gradle file:
ext {
groupId = project.property('groupId')
version = project.property('version')
lombok_version='1.18.12'
functional_api_test_version='1.0-SNAPSHOT'
redwoodCommonVersion='3.5-SNAPSHOT'
}
dependencies {
compile group: 'com.tii', name: 'redwood-common', version: "${redwoodCommonVersion}"
implementation "org.projectlombok:lombok:${lombok_version}"
annotationProcessor "org.projectlombok:lombok:${lombok_version}"
testAnnotationProcessor "org.projectlombok:lombok:${lombok_version}"
implementation 'com.tii:java-functional-testlib:0.5-SNAPSHOT'
implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'
testCompile 'junit:junit:4.12'
}
I also tried adding an external library to Eclipse:
If I click on the #Getter annotation in Eclipse, it finds and opens the lombok.Getter annotation.
I'd like to fix this in Eclipse in a way that I don't need to check in and modify the source. How can I force Eclipse to do this?
Thx, Woodsman

assertj Assertions assertThat is not resolved

Weird behavior. I've got a vanilla spring boot project I made with initializr. The build.gradle is below and as you can see it loaded up the test dependency for spring boot starter test, which includes the assertj package.
plugins {
id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.acme'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
}
test {
useJUnitPlatform()
}
However, I have my first test component here
package com.foretold.astrocalc.app.controllers;
import org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
#SpringBootTest
public class ChartControllerTest {
#Autowired
private ChartController controller;
#Test
public void contextLoads() throws Exception{
assertThat(controller).isNotNull();
}
}
And IJ is telling me that it cannot resolve the symbol assertThat. It loads up to Assertions in the import statement and that's in. When I click into the Assertions class I see the public method assertThat. Is there some IJ setting that's screwing up the static import?
It does work to just import Assertions and use the method with dot notation, but why won't the static import work?
When we want to write assertions in AssertJ, we have to use static assertThat method instead.
This means that you have to import it like following instead:
import static org.assertj.core.api.Assertions.assertThat;

How to use Jailbreak from java manifold library correctly for junit testing?

I am using java's manifold extension library for junit testing and i cant figure what i am doing wrong even after following exactly their docs.
// My Class
package practice_junit;
public class SomeClass
{
public SomeClass()
{
}
private String get_string()
{
return "ABCDE";
}
}
// My Unit Test Class -- first way
package practice_junit;
import manifold.ext.api.Jailbreak;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class SomeClassTest
{
public SomeClassTest()
{
}
#Test
public void assert_equals_true_test()
{
#Jailbreak SomeClass sc = new SomeClass();
assertEquals("Error equals","ABCDE",sc.get_string());
}
}
// My Unit Test Class -- second way
package practice_junit;
import manifold.ext.api.Jailbreak;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class SomeClassTest
{
public SomeClassTest()
{
}
#Test
public void assert_equals_true_test()
{
SomeClass sc = new SomeClass();
assertEquals("Error equals","ABCDE",sc.jailbreak().get_string());
}
}
In both the cases i am getting same error log :-
PS C:\Users\> gradle build
> Task :compileTestJava FAILED
C:\Users\SomeClassTest.java:19: error: get_string() has private access in SomeClass
assertEquals("Error equals","ABCDE",sc.get_string());
^
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileTestJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1m 9s
3 actionable tasks: 1 executed, 2 up-to-date
I am using gradle and manifold extension dependency as compile group: 'systems.manifold', name: 'manifold-ext', version: '2019.1.12' from https://mvnrepository.com/artifact/systems.manifold/manifold-ext/2019.1.12
What version of Java are you using? If Java 9 or later, are you using the JPMS (modules)? If you post your Gradle script, I can help you set it up properly. Better, post an issue on the manifold github with a link to your project. It may be that the --module-path is not explicitly set, which is a very common problem with Gradle scripts using Java 9+. Here's are the relevant bits:
dependencies {
compile group: 'systems.manifold', name: 'manifold-ext', version: '2019.1.12'
testCompile group: 'junit', name: 'junit', version: '4.12'
// Add manifold to -processorpath for javac (for Java 9+)
annotationProcessor group: 'systems.manifold', name: 'manifold-ext', version: '2019.1.12'
}
compileJava {
doFirst() {
// If you DO NOT define a module-info.java file:
options.compilerArgs += ['-Xplugin:Manifold']
// if you DO define a module-info.java file:
//options.compilerArgs += ['-Xplugin:Manifold', '--module-path', classpath.asPath]
//classpath = files()
}
}
The Manifold project tends to use Maven everywhere; the Gradle setup docs are not as polished.

FireFox Driver not working with Selenium

WHAT I HAVE DONE SO FAR:
I have included selenium as dependency in build.gradle. It looks like
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.3.1'
}
And in my test class I have included a small code as below.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestClass {
#Test
public void Test1(){
System.setProperty("webdriver.gecko.driver", "/Users/xyz/Downloads/geckodriver");
DesiredCapabilities capabilities=DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("http://www.google.com");
driver.close();
driver.quit();
}
}
And I have created a Testng instance to run the test case in intellij.
PROBLEM I AM FACING
After I execute the test. I get the following error.
org.openqa.selenium.WebDriverException: failed to lookup address information: nodename nor servname provided, or not known
I am not sure how should i go about debugging it.
Check path when you setProperty
System.setProperty("webdriver.gecko.driver",
"D:\workspacess\src\config\geckodriver.exe"); WebDriver driver
= new FirefoxDriver();

Unit testing Cloud Datastore with Android Studio

I am trying to write unit tests for a Cloud Endpoints backend, specifically using Datastore. (Unit testing without access to any specific Android modules is working as expected.)
Per this question, I added the following to my backend's build.gradle:
testCompile 'com.google.appengine:appengine-api-labs:1.9.8'
testCompile 'com.google.appengine:appengine-api-stubs:1.9.8'
testCompile 'com.google.appengine:appengine-testing:1.9.8'
testCompile 'junit:junit:4.12+'
My code is minimized from this tutorial and located at backend/src/test/java/<package>:
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import org.junit.Test;
import static junit.framework.Assert.assertTrue;
public class ExampleDatastoreTest {
private final LocalServiceTestHelper datastoreHelper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
#org.junit.Before
public void setUp() throws Exception {
datastoreHelper.setUp();
}
#org.junit.After
public void tearDown() throws Exception {
datastoreHelper.tearDown();
}
#Test
public void testExample() {
assertTrue(true);
}
}
I am getting NoClassDefFoundError when I call tearDown on my LocalServiceTestHelper.
Per that tutorial, I still need to include:
${SDK_ROOT}/lib/impl/appengine-api.jar
${SDK_ROOT}/lib/impl/appengine-tools-sdk.jar
Using File > Project Structure in Android Studio 1.2.2, I found these options:
testCompile 'com.google.appengine:appengine-tools-sdk:1.9.25'
testCompile 'com.google.appengine:appengine-api-1.0-sdk:1.9.25'
testCompile 'com.google.appengine:appengine:1.9.25'
The appengine-tools-sdk looks good, but I don't seem to be able to find appengine-api. The two I listed above are the closest I found. I continue to get the same error.
What am I missing here?

Categories