TeamCity - Testing with JUnit - testing

I am using Intellij Idea version 12 (ultimate). Just installed Team City (version 8). One default agent, running in linux.
I've created a very simple test application:
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
public int sum(int x, int y) {
return x+y;
}
}
... and a very simple test...
import junit.framework.Assert;
import org.junit.Test;
public class MainTest {
#Test
public void testSum() throws Exception {
Main test=new Main();
Assert.assertEquals("Sum should be 7",7,test.sum(4,4));
}
}
If I run this in IntelliJ, the test gets run and fails just like it should.
If instead I commit this project and push it up to github, TeamCity sees the change and begins a build. The build fails fairly quickly with the following errors:
/home/ctb/TeamCity/buildAgent/work/742505fa88794219/test/MainTest.java:1: package junit.framework does not exist
import junit.framework.Assert;
^
/home/ctb/TeamCity/buildAgent/work/742505fa88794219/test/MainTest.java:2: package org.junit does not exist
import org.junit.Test;
^
/home/ctb/TeamCity/buildAgent/work/742505fa88794219/test/MainTest.java:12: cannot find symbol
symbol : class Test
location: class MainTest
#Test
^
/home/ctb/TeamCity/buildAgent/work/742505fa88794219/test/MainTest.java:15: cannot find symbol
symbol : variable Assert
location: class MainTest
Assert.assertEquals("Sum should be 7. Loser!!",7,test.sum(4,4));
^
So yeah, I see that TeamCity is not seeing JUnit.
On the TeamCity Discussion forum, one respondent to my question there asked me if junit.jar was added as a dependency (module or library) in the build. It was listed as a module dependency, but for kicks I tried it as a library dependency. I also tried checking and unchecking export and trying the compile and test scopes, but each time I get the same errors. My run configuration is shared.
I am not using Ant or Maven. Perhaps someday, but I'd like to start as simple as possible.
Clearly, I'm missing something, but the documentation on the subject is sparse.
Thank you.

So I heard back from Jetbrains tech support this and, in the interest of completeness and saving someone else the trouble, here's the response I received:
Seems the problem is that junit.jar is not placed in version control
under your project. In order to build your project on TeamCity agent,
the project ideally should be self contained. In your case junit.jar
only exists on your local machine, I suppose there is no such file on
agent at required location. So you have two options actually: put
junit.jar under version control into your project, or define global
library in IDEA and configure this global library on IDEA Project
runner page (Check/Reparse must be started), after that put library
files on all of the agents where your build will be executed.
Personally, I think the first approach is much simpler and better.
I added junit to version control and now the build works properly in TeamCity.

Related

TomEE Microprofile: microprofile-config.properties ignored

i'm having problems getting config values into my microprofile app. I have created a META-INF/microprofile-config.properties file like this:
configEntry=HelloWorld
I've got a simple test class like this:
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.inject.ConfigProperty;
#ApplicationScoped
public class ConfigTest {
#Inject
#ConfigProperty(name = "configEntry", defaultValue = "missing")
private String configValue;
#Inject
Config conf;
public void init(#Observes #Initialized(ApplicationScoped.class) final Object init) {
System.out.println("configEntry = " + configValue);
}
}
I run this on TomEE MP 8.0.10 (downloaded standalone binaries). But whatever I do, configEntry is always 'missing' and when I debug-inspect the Config instance, I can see that there are 3 ConfigSources loaded (SystemPropertyConfigSource, SystemEnvConfigSource,TomEEConfigSource) but none is containing any entry for configValue.
I have also tried to create my own ConfigSource via META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource. Same problem here: My test ConfigSource never gets included in the list of ConfigSoruces.
Is there any kind of secret (TomEE flag) to make this work? Did I miss something crucial, or is TomEE simply not supporting microprofile-config.properties (any longer)?
I found the problem:
This was due to an incompatible maven build configuration. All guides and tutorials I found usually specify the pre-build location of the file, not the location they must end up in the war file/dir. I did put them into the correct pre-build location, but maven placed them 'wron' (inside /META-INF/ not WEB-INF/classes/META-INF).
So, here for all with a similar confusion, after the build this is where the files must reside to be found:
microprofile-config.properties: /WEB-INF/classes/META-INF/
spi services directory: /WEB-INF/classes/META-INF/
spi custom ConfigSource text file: /WEB-INF/classes/META-INF/services/

IntelliJ Idea - Not compiling class - but resolves library

I have a peculiar problem with IntelliJ
The project libraries are showing as resolved meaning any import from the library works fine. For example, import javax.servlet.http.HttpServletRequest;
But for the java sources within the module - it fails to import them. Even a simple interface with one method , For example - public interface X { void go()} and a class implementing public Y implements X - fails to import X.
I have also an ant build which i run inside docker which compiles the project fine !
The older import of the same project works fine and I am unable to find the difference between the two.
The ant build runs fine!!. It does not compile any file , I put a next line and save the File - still it will not resolve it !!
Community Edition 2019.3

Maven install - SQL problem / suitable driver

I have two Maven Project with exactly same code.
I'm running project number one just from public static void main method and it connects to database and works perfectly, but when I use maven install and try to run it as a plugin in BukkitMC server it tells me all the time
"no suitable jdbc driver found"
I have dependencies at pom.xml in both project the same, so I'm pretty sure it works.
It's just all about this MAVEN INSTALL.
Have you got an idea?
public static void main(String[] args) {
App app = new App();
app.insertData("INSERT INTO users (login,password,email) VALUES ('a','b','c');");
}
Basically install goal do also package goal.
In package your project is archived (eg:jar) and after on install is deploy.
Could check what is inside archive(jar) and if not any sql-driver then you could search for the appropriate way to be packages from maven also with driver.
Other option is to add driver maybe manually on your server.

Intellij IDE Not Running Unit Tests

I am running IntelliJ IDEA 2018.3.1 and am attempting test a class with the integrated test runner. The test seems to compile but not run.
This is a multi-module Maven project, and other modules have tests that run. However, I have not been able to find any differences between the projects. The surefire plugin is specifically defined on this project, and <skipTests> is specifically set to false. I have reimported the project several times in case the maven configuration is affecting the built-in runner.
The image below is the only output I get. Debug/Breakpoints will not stop.
If anyone can help or throw possibilities at me, I would appreciate it.
Edit:
Here's a simplified version of the test I'm attempting to run:
package com.jason;
// imports
#RunWith(BlockJUnit4TestRunner.class)
public class MyTest {
private ClassUnderTest clazz;
private DaoClass dao;
#Before
public void setUp() {
// using Mockito to mock the DaoClass
// injecting the DAO into the ClassUnderTest
}
#Test
public void testMethod() {
Assert.assertTrue(true);
}
}
I attempt to run the test by right-clicking on the method annotated with #Test and clicking run. The option to run the test DOES appear in the context menu. When I do so, all that appears is the screenshot.
I have attempted to do the following to troubleshoot the issue:
In the pom.xml file for the appropriate module, I have manually specified the surefire plugin in the <build><plugins> section. I then did a reimport to pick up the changes.
I have put breakpoints in the code and run the test in debug mode.
I have attempted to log output, both with an slf4j logger and a System.out.println()
I have attempted to find any differences in the IDEA .iml file between a module where the tests run and this module where the tests do not run.
I have written a very simple test class, with a method annotated with #Test and containing the line Assert.assertTrue(true)
Edit 2
Attempting to run mvn test -Dcontrollername produces the following output:
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project rma-svc: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test failed: The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
Edit 3
I've updated my Maven surefire plugin to 2.22.2 and am not seeing the forked JVM issue any longer. However, running mvn test -DskipTests=false outputs No tests were executed!

java.lang.NoSuchMethodError when running a test in IntelliJ IDEA

I get a NoSuchMethodError on AssertJ when I try to execute a unit test. The make step works fine and when navigating to the implementation of assertThat(), it works just fine.
The module dependencies show that I am using org.assertj:assertj-code:1.2.0 as I have defined as compile dependency in my build.gradle (yes, compile and not test). The unit test resides inside the main classpath of my module since it is an integration test, so this is intentional. The iml file contains:
<orderEntry type="library" name="Gradle: org.assertj:assertj-core:1.2.0" level="project" />
My example test is:
import org.assertj.core.api.Assertions;
import org.junit.Test;
public class X {
#Test
public void x(){
Assertions.assertThat(1).isEqualTo(1);
}
}
and when I execute it, I get:
java.lang.NoSuchMethodError:
org.assertj.core.api.Assertions.assertThat(I)Lorg/assertj/core/api/IntegerAssert;
When I look in the run configurations, the classpath is set to the correct module (the module containing AssertJ).
I have tried invalidating caches but that did not help.
I am currently out of ideas (no pun intended).
Anyone out there who has seen this before? Or have a clue on how to proceed troubleshooting?
Found it.
I had another module importing a newer version of AssertJ. And somehow (seems like a bug in IntelliJ) my runtime was using that one instead.
I will keep track of my reported bug: https://youtrack.jetbrains.com/issue/IDEA-156718 and post updates.