mvn test does not find JUnit Tests - unit-testing

I have a a test class which is named xxxTest.java so the test class if found. But the unit tests within the class are not run when I execute mvn test.
I am using JUnit 4 and the test method's are annotated with #Test. eg.
#Test
public void shouldDoSomeAsserting() {
// unit test impl
}
If I rename that test method so it is name testShouldDoSomeAsserting() then mvn test does find and execute that unit test.
I was under the impression that when I use #Test as long as the method was public and void that it would be considered a test method.
Have I missed something?
Thanks.

Are you absolutely sure you are using JUnit 4? This sounds like JUnit 3 behaviour.
I made a minimal sample project and I couldn't recreate your problem. The test runs as expected.
The sample project is here: https://gist.github.com/1888802
Maybe you can get some kind of hint from it.

I've got it working by manually specifying a provider; more details here: Surefire JUnit 4 config

Related

Flag test as expected to fail in unit 5

I have a unit test, written with JUnit 5 (Jupiter), that is failing. I do not currently have time to fix the problem, so I would like to mark the test as an expected failure. Is there a way to do that?
I see #Disable which causes the test to not be run. I would like the test to still run (and ideally fail the build if it starts to work), so that I remember that the test is there.
Is there such an annotation in Junit 5? I could use assertThrows to catch the error, but I would like the build output to indicate that this is not a totally normal test.
You can disable the failing test with the #Disabled annotation. You can then add another test that asserts the first one does indeed fail:
#Test
#Disabled
void fixMe() {
Assertions.fail();
}
#Test
void fixMeShouldFail() {
assertThrows(AssertionError.class, this::fixMe);
}

scalatest run integration test separately from unit test

I am using scalatest maven plugin and I would like to run integration test separately from unit tests. The tests path are src/it and src/test for integration test and unit test respectively.
Which is the best approach to achieve this goal?
Thanks
One option is to create an object and then use it as a tag in each test:
object IntegrationTag extends Tag("Integration-Test")
test("Test for correct number of records", IntegrationTag) {
// some stuff
}
Then, if you want to test the Unit Tests simply run the command:
mvn test -DtagsToExclude=Integration-Test
This is a possible solution...sure that will be more.

To develop a testing tool

Now, I am trying to develop a testing tool, which can make unit testing. I mean I want to use JUnit in my testing tool to test other projects. But I don't know how to insert JUnit into my testing tool. Is it possible to do that and how? And is there any other open-source testing tool can be inserted into my testing tool?
To use Junit API make sure you got the jar in the classpath
To use Junit tests you need the testing class to extend SystemTestCase4
and your function to have #Test annotation above it
if you want code to run before your test so use function with #Before
and if you want after use function with #After
public class BaseTest extends SystemTestCase4 {
#Before
public void beforeEachTest() throws Exception {
}
#Test
#TestProperties(name = "test test ")
public void testTest() throws Exception {
//run your tested code
}
#After
public void afterEachTest() throws Exception {
}
as for how to test your projects it depend what tests you want to do?
For unit testing just add your own tests inside the projects
Integration, Functional or other tests need you to understand how to "Attack" it, meaning if it's UI tests for web so use tools for that (Selenium for example) if it's for checking network so use JMeter

How do I declare that I "expect" an exception in a unit test with Groovy, JUnit and Maven?

I have the following Groovy unit test code:
class MyTest extends GroovyTestCase {
#Test(expected = IllegalArgumentException.class)
public void testReadFileMissing() {
// does something which causes an IllegalArgumentException
}
// more tests
}
This works perfectly with my Java unit tests, but with groovy tests, mvn clean test runs the test, but the test fails because an IllegalArgumentException was thrown. In other words, my "expected" annotation attribute seems to be completely ignored.
I could of course simply use a try/catch block to check the behaviour I'm interested in, but I'd like to use the JUnit API if possible because that's what it's for and I find the resulting code simpler to read and understand.
So, can anyone tell me what I'm doing wrong?
either don't use GroovyTestCase and the according JUnit class instead as base or go full groovy and use shouldFail. examples are here http://mrhaki.blogspot.de/2009/11/groovy-goodness-testing-for-expected.html
You can use shouldFail or shouldFailWithCause showcasing exactly which kind of exception is expected from the code under test.

Can You Specify the Order of Test Execution in VS 2005 Test Engine?

I am using the provided Unit Test Engine in Visual Studio 2005 and am wondering if there is a way for me to specify the order of tests. I have numerous test classes and numerous test methods inside of each. I would like to control the order in which the test classes are executed and the order of the test methods in each.
Why? Actually tests are supposed to be able to run alone. If you need to execute some particular code before running a test you should have that in your test class:
//Use TestInitialize to run code before running each test
[TestInitialize()]
public void MyTestInitialize()
{
}
//Use TestCleanup to run code after each test has run
[TestCleanup()]
public void MyTestCleanup()
{
}
I don't think the order of your test execution should matter. But if you really need to order them fellow Matthew Whited link.
you can use Ordered Tests
http://msdn.microsoft.com/en-us/library/ms182629(VS.80).aspx