Other referenced package cannot be found in test - unit-testing

In my project (if you want detailed information, you can find it on Github) I've got two modules:
app (the default module generated when you start out with a new Android project)
dice (new module, which is a dice rolling domain model package)
I've started out fleshing out my MVP classes in the app module, and I want to reuse the PredefinedRandomIntegerGenerator class (on Github) from the dice module (residing the test folder of that module) in my unit tests in the app module.
When I started writing my test code I encountered no problems, until I started running my
C:\Projects\Dice-Statistics\app\src\test\java\com\xilconic\dicestatistics\viewmodels\SixSidedDieRollingViewModelTest.java:5: error: package com.xilconic.gaming.dice.test.mocks does not exist
import com.xilconic.gaming.dice.test.mocks.PredefinedRandomIntegerGenerator;
^
C:\Projects\Dice-Statistics\app\src\test\java\com\xilconic\dicestatistics\viewmodels\SixSidedDieRollingViewModelTest.java:21: error: cannot find symbol
RandomIntegerGenerator rng = new PredefinedRandomIntegerGenerator(results);
^
symbol: class PredefinedRandomIntegerGenerator
location: class SixSidedDieRollingViewModelTest
I have no clue why during the build the com.xilconic.gaming.dice.test.mocks package can't be found. Am I missing something in my Gradle script for the app module? That looks as follows:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.xilconic.dicestatistics"
minSdkVersion 8
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile project(':dice')
}
Any help would be very welcome!

Related

How can I fix this error when trying to deploy my svelte app on github pages?

I am trying to deploy my svelte app on GitHub Pages and cannot get rid of these errors I get from running 'npm run build'.
The exact errors are:
(node:15364) ExperimentalWarning: buffer.Blob is an experimental feature. This feature could change at any time
file:///C:/Isabella/personal-website/node_modules/#sveltejs/kit/dist/prerender.js:460
throw new Error(format_error(details, config));
^
Error: 404 /src/images/github.png (linked from /)
at file:///C:/Isabella/personal-website/node_modules/#sveltejs/kit/dist/prerender.js:460:11
at save (file:///C:/Isabella/personal-website/node_modules/#sveltejs/kit/dist/prerender.js:744:4)
at visit (file:///C:/Isabella/personal-website/node_modules/#sveltejs/kit/dist/prerender.js:635:3)
[vite-plugin-svelte-kit] Prerendering failed with code 1
error during build:
Error: Prerendering failed with code 1
at ChildProcess.<anonymous> (file:///C:/Isabella/personal-website/node_modules/#sveltejs/kit/dist/vite.js:2367:14)
at ChildProcess.emit (node:events:527:28)
at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12)
And here is my svelte.config.js file:
import preprocess from 'svelte-preprocess';
import adapter from '#sveltejs/adapter-static';
/** #type {import('#sveltejs/kit').Config} */
const config = {
preprocess: preprocess(),
kit: {
// target: '#svelte',
adapter: adapter({
pages: 'build', // path to public directory
assets: 'build', // path to public directory
fallback: null,
}),
prerender: {
// This can be false if you're using a fallback (i.e. SPA mode)
default: true
}
}
};
export default config;
The issue is that you're linking to /src/images/github.png in index.svelte but the prerenderer can't find the file because it tries to look for the file in the default assets folder static.
To fix it change the link to the correct path and put the image in the static folder (i.e. images/github.png if you put the images folder in the static folder).

Intellij 2021 Cannot resolve symbol if it's from a custom maven repo?

I have a project where I'm using a dependency from a custom repository. This is what my build.gradle looks like (I have blurred out some dependencies for privacy):
plugins {
id 'org.springframework.boot' version '2.5.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'groovy'
}
group = 'com.myproject.projectx.sms'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
maven {
url "s3://myproject-maven-repo"
authentication {
awsIm(AwsImAuthentication)
}
}
}
dependencies {
implementation 'org.postgresql:postgresql'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.security:spring-security-core'
implementation 'com.myproject.top:secret-project:1.2' <------------ Gradle won't download this. why?
testImplementation 'org.spockframework:spock-spring:2.0-groovy-3.0'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
exclude group: 'org.junit', module: 'junit'
}
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
test {
useJUnitPlatform()
}
There are red squiggly underlines when I hover over code that's using the top:secret-project dependencies. This used to work in old projects, but why can't intellij recognize it in this project? Also I can run the project successfully in ubuntu terminal with gradle bootRun. It's just intellij cannot recognize it. It also able to recognize spring, postgres, lombok & spock dependencies.
Cannot resolve symbol 'com.myproject.top.secret.TopSecretService'.
When click on build for gradle, I keep getting this error.
Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied;)
I already exported the correct secret and keys in my .bashrc file. I also already configured ~/.aws/credentials. But I'm still getting the error above.
Am not really familiar with AwsImAuthentication nor the s3:// , but as it mentioned in the official doc that you need to add user/pass , or user/token as following
maven {
url 'https://my_domain-111122223333.d.codeartifact.region.amazonaws.com/maven/my_repo/'
credentials {
username "aws"
password System.env.CODEARTIFACT_AUTH_TOKEN
}
}
where CODEARTIFACT_AUTH_TOKEN is enviroment variable to store your password/session .
You can also use the gradle.properties to retrive the value , just make sure to replace System.env.CODEARTIFACT_AUTH_TOKEN with
project.findProperty.CODEARTIFACT_AUTH_TOKEN where CODEARTIFACT_AUTH_TOKEN is store the password/token in your gradle properties .

Sonarqube gives 0% coverage on spring boot gradle project

So I am working with SonarQube and I keep getting stuck. My coverage is 0% while it should not be 0% (I made sure with an simple test that always should run correctly and covers at least one method).
I am reading a lot of different gradle.build files on the internet, but I can't find one that works for me.
Also I think the problem is that sonarqube can't find certain files, but I can't find a working directory of someone which I can compare with my own.
Gradle.build
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id "org.sonarqube" version "3.0"
id 'jacoco'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '15'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'
implementation('io.jsonwebtoken:jjwt:0.2')
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compile 'junit:junit:4.12'
implementation 'org.modelmapper:modelmapper:2.4.1'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.eclipse.jgit:org.eclipse.jgit:5.4.2.201908231537-r'
/**
* JUnit jupiter with mockito.
*/
testCompile group: 'org.mockito', name: 'mockito-junit-jupiter', version: '2.19.0'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.19.0'
testCompile group: 'org.springframework.security', name: 'spring-security-test', version: '5.1.6.RELEASE'
}
sonarqube{
properties{
property 'sonarjava.source', '1.8'
property 'sonar.java.coveragePlugin', 'jacoco'
}
}
test {
useJUnitPlatform()
}
This is the folder where my test reports are in (I think you will need this to help me):
The build directory:
The jacoco test directory
Can anyone help me with my problem? If you want to see directories or other files, just ask and add them to the post.
Thanks in advance!
you need to make sure jacoco runs before sonarqube, so add something like:
tasks["sonarqube"].dependsOn("jacocoTestReport")
Also, side note, compile is deprecated, so use implementation and testImplementation instead.
The answer was in a directory that I had not shown. My project was missing the Jacocoreportxml file. Once I added this with the gradle build jacocoreportxml command in my terminal it worked!

Can we run Spock Testcases and Junit 5 test cases together In one project?

We are not able to run test cases written with Junit 5 and Spock framework together in one gardle project?
We tried add dependencies given in https://www.baeldung.com/junit-5-gradle to our gradle file. Gradle version is 4.10.3 and Junit 5. Below is my build.gradle file
apply plugin: 'groovy'
apply plugin: 'java'
repositories {
mavenCentral()
maven {
url "http://repo.fusesource.com/nexus/content/groups/public/"
}
maven {
url "https://repository.jboss.org/nexus/content/groups/public"
}
jcenter()
}
dependencies {
compile group: 'com.google.inject', name: 'guice', version: '4.2.2'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
testCompile(
'org.codehaus.groovy:groovy-all:2.4.8',
'org.spockframework:spock-core:1.0-groovy-2.4',
'org.jmockit:jmockit:1.8',
'junit:junit:4.12'
)
testRuntime(
'cglib:cglib:2.2.2',
'com.athaydes:spock-reports:1.2.7'
)
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testCompileOnly 'junit:junit:4.12'
}
test {
useJUnitPlatform()
testLogging { showStandardStreams = true }
}
I have created two test cases, one is using spock framework and other is using junit 5. But when I do gradlew -test, it runs only test cases written with Junit 5. Below is build path.
You need the Vintage test engine to execute Spock tests since they are based on JUnit 4, and you need the Jupiter test engine to execute the JUnit Jupiter tests.
So you need dependencies on both engines.
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
I'd also recommend that you upgrade to JUnit 5.5.1 (i.e., the latest and greatest).

Why my Spock unit tests fail in Gradle build after upgrading to JDK 8?

I'm trying to compile my application source using JDK 8, Groovy 2.4.4, Spock 1.0-groovy-2.4, Gradle 1.11 (my firm hass custom plugins built on top of this)
gradle -version
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.9.2 compiled on July 8 2013
Ivy: 2.2.0
JVM: 1.7.0_51 (Oracle Corporation 24.51-b03)
OS: Windows 7 6.1 amd64
In my build environment, I need to retain my JAVA_HOME to Java 7 for other projects.
JAVA_HOME = C:\Program Files\Java\jdk1.7.0_51
JAVA8_HOME = C:\Program Files\Java\jdk1.8.0_60
Due to this, in my build.gradle, I explicitly tell Gradle to use JDK 8
tasks.withType(JavaCompile) {
options.fork = true;
options.forkOptions.executable = "${System.env.JAVA8_HOME}/bin/javac"
}
tasks.withType(Test) {
executable = "${System.env.JAVA8_HOME}/bin/java"
}
compileJava{
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
The source files compile successfully, however when it reached unit tests (written in Spock), it fails with the below error.
Caused by: java.lang.UnsupportedClassVersionError: org/model/SDA : Unsupported major.minor version 52.0
I confirmed that SDA.class is compiled using JDK 8.
javap -verbose SDA.class | findstr "major"
major version: 52
Any reason why only during unit tests I get the above error? Does that mean unit test is being run on JDK 7?
So if you're really into hacks there is something you can do. It does involve overriding some of the Gradle internals - so it may not work on any version of Gradle other than 1.11. There may also be unexpected side-effects.
import org.gradle.internal.Factory
import org.gradle.process.internal.WorkerProcessBuilder
import org.gradle.api.internal.tasks.compile.daemon.CompilerDaemonStarter
tasks.withType(GroovyCompile) {
options.fork = true
options.forkOptions.executable = "${System.env.JAVA8_HOME}/bin/javac"
doFirst {
def compilerFactory = compiler.compiler.compilerFactory
def clientsManager = compilerFactory.compilerDaemonManager.clientsManager
def oldDaemonStarter = clientsManager.compilerDaemonStarter
if (!(oldDaemonStarter.workerFactory instanceof CustomWorkerProcessFactory)) {
println "HACK, installing custom daemon worker"
clientsManager.compilerDaemonStarter = new CompilerDaemonStarter(
new CustomWorkerProcessFactory(oldDaemonStarter.workerFactory),
oldDaemonStarter.startParameter)
}
}
}
class CustomWorkerProcessFactory implements Factory<WorkerProcessBuilder> {
Factory<WorkerProcessBuilder> delegate
CustomWorkerProcessFactory(Factory<WorkerProcessBuilder> delegate) {
this.delegate = delegate
}
public WorkerProcessBuilder create() {
def newWorker = delegate.create()
newWorker.javaCommand.executable("${System.env.JAVA8_HOME}/bin/java")
newWorker
}
}
Add this to your gradle.properties:
org.gradle.java.home=/path/to/java8
Or when you launch gradle, you can specify it as a jvm parameter:
gradle -Dorg.gradle.java.home=/path/to/java8 task1 task2