So I'm getting this persistent error using netbeans. I've got a LinkedList class which I am testing via a JUnit test, which I created by clicking on LinkedList.java: Tools -> Create/Update Tests and this LinkedListTest.java class is now located in test packages.
My LinkedList.java file works correctly when tested in a file with a main method.
public class LinkedListTest {
#Test
public void testAddFirst() {
LinkedList linkedList = new LinkedList();
Country c1 = new Country("Australia");
linkedList.addFirst(c1);
assertEquals("Australias", linkedList.getValue(0)); // Should fail a test
} // default test methods beneath
All my imports check out. JUnit 5.3.1 and I had to download apiguardian1.1.0.jar from MVN repository to clear an error for:
reason: class file for org.apiguardian.api.API$Status not found
I right-click in this file and select Test File, or use Ctrl+F6, I've selected Test File from the original LinkedList file, I've even used Alt+F6 which tests the whole project. Yet I'm met with 'No tests executed.', an empty Test Results window, and no Notifications. What am I doing wrong?
Thanks for any help
Edit: I just switched from netbeans to eclipse.
You forget to extend Runner with class --
use like below with class -
public class LinkedListTest extends Runner {
}
Hope this help you.
Related
I am running into trouble with JUnit 5 (5.0 or 5.1) and custom extension.
We are using service loader to load all implementations which then modify how our extension is bootstrapped. These implementations can be loaded just once, so I was thinking of using ExtensionContext.Store and placing it there. Every subsequent test instance would then just load it from Store instead of via service loader.
Now, I am even aware of the hierarchical context structure and I know that there is some "root" context which you can get through ExtensionContext.getRoot(). But this "root" context (instance of JupiterEngineExtensionContext) isn't really root - there is different one for every test instance.
Say you have FooTest and BarTest, then printing out getRoot() for each of them yields:
org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext#1f9e9475
org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext#6c3708b3
And hence trying to retrieve previously stored information from Store fails.
Is having this limitation intended? It makes the borderline between ClassExtensionContext and JupiterEngineExtensionContext pretty blurred.
Is there another way to globally store some information via extension?
Here is a (very) simplified version of how I tried working with the store (cutting out all other information basically). I also added some System.out.print() calls to underline what I am seeing. Executing this extension on two test classes results in what I described above:
public class MyExtension implements BeforeAllCallback {
#Override
public void beforeAll(ExtensionContext context) throws Exception {
System.out.println(context.getRoot());
if (context.getRoot().getStore(Namespace.create(MyExtension.class)).get("someIdentifier", String.class) == null) {
context.getRoot().getStore(Namespace.create(MyExtension.class)).put("someIdentifier", "SomeFooString");
} else {
// this is never executed
System.out.println("Found it, no need to store anything again!");
}
}
}
EDIT: Here is a minimal project on GH(link), run by mvn clean install, which displays the behaviour I see.
I just copied your MyExtension verbatim (i.e., with zero changes) and ran both FooTest and BarTest.
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
#ExtendWith(MyExtension.class)
class FooTest {
#Test
void test() {
}
}
and
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
#ExtendWith(MyExtension.class)
class BarTest {
#Test
void test() {
}
}
And the result is:
org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext#2280cdac
org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext#2280cdac
Found it, no need to store anything again!
Thus, getRoot() works as documented.
The only explanation for why you see two different roots is that you must be executing the tests in different processes.
Please keep in mind that the root ExtensionContext instance is bound to the current execution of your test suite.
So if you run FooTest and BarTest one after the other in an IDE, that will actually result in two "test suites" with different roots. The same is true if you configure your build tool to fork between test classes.
Whereas, if you execute both test classes together in a single "test suite" (e.g., by telling your IDE to run all tests in the same package or same source tree) you will then see that there is one root like in the output I provided above.
Note, however, that there was an issue with the junit-platform-surefire-provider prior to version 1.0.3, whereby the provider launched the JUnit Platform for each test class. This would give the appearance of forking even though Surefire did not actually start a new JVM process. For details, see https://github.com/junit-team/junit5/pull/1137.
I've set up a test file in src/main/java with my cucumber annotations containing class A, as well as a test file extending class A in src/test/java with the following annotation on class B:
#ContextConfiguration(locations = {"classpath:META-INF/application-config.xml", "classpath:META-INF/overrule.xml" })
This is working fine when I do a maven clean install.
What I would like to achieve though is being able to run a feature file through the cucumber setup of class A and see its output. So far I've managed to find a method which should allow me to run the cucumber test, but I can't seem to figure out what its arguments should be. Could anyone provide me with an example of how to implement the function cucumber.api.cli.Main.run()?
#Override
public void buttonClick(final ClickEvent event) {
try {
final String[] arguments = {"foo", "bar" };
cucumber.api.cli.Main.run(arguments, ClassLoader.getSystemClassLoader());
} catch (final Throwable e) {
e.printStackTrace();
}
}
I would invoke the command line version using cucumber.api.cli.Main.main(args);
where args is a String array with the parameters set. I would not use the run command you refer to.
The documentation describes all available options.
Another source may be the getting started project supplied by the Cucumber team: https://github.com/cucumber/cucumber-java-skeleton
It may be of specific interest to look into the Ant build script https://github.com/cucumber/cucumber-java-skeleton/blob/master/build.xml to see what arguments they supply to Cucumber.
I'm getting started with DbUnit lately and I'm trying to write a very simple integration test just to populate a table with 3 rows. Reading the DbUnit Getting Started Guide, it tells me to create a dataset file. My dataset xml file looks exactly like this:
<dataset>
<notaFiscal cliente="Cliente 1" valor="26.5" data='2016-04-04'/>
<notaFiscal cliente="Cliente 2" valor="30.5" data='2016-05-01'/>
<notaFiscal cliente="Cliente 3" valor="28.2" data='2015-08-11'/>
</dataset>
Then, I have to create a test class which extends DBTestCase and implement my test methods (annotated with #Test, like any other JUnit test case). The class I created is as follows:
public class GerenciadorNFTest extends DBTestCase {
private GerenciadorNotaFiscal gerenciador = new GerenciadorNotaFiscal();
public GerenciadorNFTest(String name)
{
super( name );
// PBJDT is an abbreviation of PropertiesBasedJdbcDatabaseTester
// just for a better visualization
System.setProperty(PBJDT.DBUNIT_DRIVER_CLASS,
"org.postgresql.Driver" );
System.setProperty(PBJDT.DBUNIT_CONNECTION_URL,
"jdbc:postgresql://localhost:5432/dbunit" );
System.setProperty(PBJDT.DBUNIT_USERNAME, "postgres" );
System.setProperty(PBJDT.DBUNIT_PASSWORD, "123456" );
}
protected IDataSet getDataSet() throws Exception {
IDataSet dataSet = new FlatXmlDataSetBuilder().build(
new FileInputStream("notas_fiscais.xml"));
return dataSet;
}
#Test
public void geraPedido() {
Pedido p = new Pedido("Diogo", 26d, 5);
gerenciador.gera(p);
NotaFiscal notaFiscal = gerenciador.recupera("Diogo");
Assert.assertEquals(notaFiscal.getCliente(), "Diogo");
}
}
After that, I tried to run the test case but got the following error:
junit.framework.AssertionFailedError: No tests found in teste.GerenciadorNFTest
at junit.framework.Assert.fail(Assert.java:57)
at junit.framework.TestCase.fail(TestCase.java:227)
If I tried to remove the extend DBTestCase, JUnit recognize the test case and runs normally, but with the extend it didn't. I tried to clean and re-compile, but it didn't work. I also tried to run the test outside the IDE I use (Intellij Idea) but again I had no sucess.
Has anyone been through this same problem?
Thank you very much in advance. Any help will be appreciated.
There are JUnit 3 vs 4 runner differences that may be the cause (you don't mention JUnit and dbUnit versions, nor how dependency managing them). And different tooling has different running default requirements (e.g. Maven defaults to only running classes as tests with class name suffix of "Test").
Note that it is not required to extend a dbUnit class (I don't) and not doing so should eliminate the encountered problem. Just further down that page you mentioned are two sections describing how:
Using your own parent class or a dbUnit IDatabaseTester instance: http://dbunit.sourceforge.net/howto.html#Database_setup_with_your_own_TestCase_subclass
Using no parent class: http://dbunit.sourceforge.net/howto.html#Database_setup_with_no_parent_class
And combining both is what I've done for years - have my own parent test class for common stuff and then DI (or instantiate) the desired DBTestCase (usually PrepAndExpectedTestCase).
I am trying to get gwt-test-utils to work. I set up the project in the following way:
src/main/java : all the java source code
src/test/java : the test source code
src/test/resources : resource files for the tests
I am building my project with gradle and eclipse. Gradle uses these directories correctly by default and I added all three of them as source directories to Eclipse.
I have successfully built and run the project and was able to execute some plain old JUnit tests as well as a GWTTestCase, so I think I set up the project and its dependencies correctly.
Now I wanted to use gwt-test-utils for some more advanced integration tests. To do so I did the following:
Add the gwt-test-utils and gwt-test-utils-csv to my dependencies
gwtTestUtilsVersion = '0.45'
testCompile group:'com.googlecode.gwt-test-utils', name:'gwt-test-utils', version:gwtTestUtilsVersion
testCompile group:'com.googlecode.gwt-test-utils', name:'gwt-test-utils-csv', version:gwtTestUtilsVersion
Add a gwt-test-utils.properties file to the directory src/test/resources/META-INF with the following content:
path/to/my/module = gwt-module
Added a class that extends GwtCsvTest to a package in the src/test/java directory. It is modeled after the second example in HowToWriteCsvScenario from the gwt-test-utils project wiki, replacing occurrence of their example classes with mine. It looks like this
#CsvDirectory(value = "gwtTests")
public class LoginLogoutTest extends GwtCsvTest
{
#Mock
private MainServiceAsync mainService;
private AppController appController = new AppController();
#CsvMethod
public void initApp()
{
appController.onModuleLoad();
}
#Before
public void setup()
{
GwtFinder.registerNodeFinder("myApp", new NodeObjectFinder()
{
#Override
public Object find(Node node)
{
return csvRunner.getNodeValue(appController, node);
}
});
GwtFinder.registerNodeFinder("loginView", new NodeObjectFinder()
{
#Override
public Object find(Node node)
{
return csvRunner.getNodeValue(appController.getRootPresenter().getCurrentlyActiveSubPresenters().iterator().next().getView(), node);
}
});
addGwtCreateHandler(createRemoteServiceCreateHandler());
}
}
added a csv-file for configuring the test to src/test/resources/gwtTests with the following content
start
initApp
assertExist;/loginView/emailTextBox
I tried executing it via the Eclipse's Run As > JUnit Test and indirectly via gradle build (which executes all the test cases, not just this one). Both lead to the same error:
ERROR GwtTreeLogger Unable to find type 'myPackage.client.AppController'
ERROR GwtTreeLogger Hint: Check that the type name 'myPackage.client.AppController' is really what you meant
ERROR GwtTreeLogger Hint: Check that your classpath includes all required source roots
The AppController class is the entry-point configured in the module I configured in gwt-test-utils.properties, which makes me think that configuration works correctly and the rest of the setup (dependencies and all) work as well.
In an earlier version I used the same file as a subclass of GWTTestCase and created an AppController instance in the same way. That worked, so I'm pretty sure the class path is setup correctly to include it as well. I also tried changing it back to the previous version just now and it still works.
I have no clue why the class is not found. Is there anything gwt-test-utils does differently which means I need to specifically set the class path for it? Otherwise it should just work, since both gradle and eclipse know about all the relevant source folders and dependencies.
When I run just my Testclass in Eclipse I get the JUnit view showing the tree structure and if the test was successful. If I start my Test from code:
JUnitCore core = new JUnitCore();
core.run(SimpleTests.class);
the view does not show. Can I change this?
On your toolbar click Windows-->Show View-->Others.
Type "Junit" without quotes. Select from list and click OK.
I suppose you run your code in the main method like this example :
public class MiniTest extends TestCase
{
#Test
public void test()
{
System.out.println("Running test");
}
public static void main(String[] args)
{
JUnitCore core = new JUnitCore();
core.run(MiniTest.class);
}
In this case, it will not show your view because you launch your program as a java application (alt-shift-X-J). You will see "Running test" in the console but that is.
If you want to see the Junit View you must launch your program as a Junit test (alt-shit-X-T).
Note that with this manner the main() is not necessary, Eclipse will directly launch the method tagged with #Test. You can also group tests with #Suite.
You can't interact with the JUnit view from your code using JUnitCore.
You can however, add your tests to the JUnitView if you use a #Parameterized test, or if you implement your own test runner. Try extending the Suite class and reimplement one of the constructors with the tests that you want to execute.
I get this problem from time to time.
There is something corrupted in the eclipse workspace.
The only solution for me is to create a new workspace and bring in the projects.
It's a serious pain when there are fifty projects in the workspace.
If the view doesn't show, go to Window -> Perspective -> Reset Perspective..