JUnit 5 test suite choose test methods - spring-boot

Having some test classes with Junit 5 and Spring Boot REST controller testing.
#SpringBootTest
#AutoConfigureMockMvc
public class TestClassA {
#Autowired
private MockMvc mockMvc;
#Test
#WithMockUser
public void testMethod1() {
...
}
#Test
#WithMockUser
public void testMethod2() {
...
}
// more test methods
}
#SpringBootTest
#AutoConfigureMockMvc
public class TestClassB {
#Autowired
private MockMvc mockMvc;
#Test
#WithMockUser
public void testMethod1() {
...
}
// more test methods
#Test
#WithMockUser
public void testMethodX() {
// requires that TestClassA.testMethod2()
// must be run first
}
}
The test classes run, each testing a specific REST controller, but how to achieve a new test method TestClassB.testMethodX()?
I thought to create a test suite in JUnit 5 and specify which test methods to run, also in order:
1. run TestClassA.testMethod2()
2. run TestClassB.testMethodX()
I know about two annotations:
- #SelectPackages
- #SelectClasses
But selecting specific test methods not possible?
Is this achievable with JUnit 5?

I cannot answer both of your questions, but at least I can give you a hint on how to choose a set of test methods which should be run.
For my understanding unit/integration tests should not rely on a specific order to pass successfully. Is it really necessary, or can you realize important requirements for tasks using things like #Before annotations?
In my setup, I run Spring Boot with Gradle.
JUnit Tags & Filtering
JUnit 5 allows to add tag(s) to your test methods, using the #Tag annotation. With this feature it is possible to filter your test methods on test execution. Have a look at baeldung.com for a short tutorial.
Tag your test methods
Mark your test methods with a tag fitting to your purpose, like this.
#Test
#Tag("IntegrationTest")
public void testMethod1() {
...
}
// more test methods
#Test
#Tag("UnitTest")
public void testMethodX() {
...
}
Run tagged methods
Run tagged methods using IntelliJ
If you use the IntelliJ IDE, it is rather simple to run test methods with specific tags. See jetbrains.com docs or stackoverflow.
Run -> Edit Configurations... -> + (Add new configuration) -> JUnit
As Test kinds choose Tags
Insert your desired tag at Tag expression
Run tagged methods using Gradle
In case you want to run your test methods for instance in a continuous integration pipeline you probably run the tests using Gradle or Maven. For Gradle I can also show you a possibility to run methods with specific tags.
Open build.gradle
For dependencies you probably already have integrated the JUnit 5 framework, similar to this:
dependencies {
// [...]
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
// [...]
}
You also should have a test task.
test {
useJUnitPlatform()
}
The command $ gradlew test would now run all defined test methods. So, to run just a specific set of methods, I suggest to create custom tasks, like this.
test {
useJUnitPlatform()
}
task unitTests(type: Test) {
useJUnitPlatform {
includeTags 'UnitTest'
}
}
task integrationTests(type: Test) {
useJUnitPlatform {
includeTags 'IntegrationTest'
}
}
After that, you are able run the tasks on the command line interface, e.g.: $ gradlew unitTests and it would just run the tasks you defined in your custom task.

Related

Arquillian test not failing

When dependency injection in Arquillian test class fails, maven still claims that all tests are run and are passed. This is major issue if we have hundreds of tests and some tests show as Green in Jenkins, but in reality they are not driven at all.
Is there a way to make the above test fail in Jenkins/Maven? One can use any Interface long as there is no implementation added to deployment.
#ExtendWith(ArquillianExtension.class)
class FailingArquillianTestIssueIT {
#Inject
DummyInterface dummyInterface;
#Deployment(testable = true)
public static JavaArchive create() throws Exception {
JavaArchive archive = ShrinkWrap.create(JavaArchive.class).addAsManifestResource(EmptyAsset.INSTANCE,
"beans.xml");
return archive;
}
#Test
void test1() {
assertTrue(false);
}
}

How to inject values from application.yaml to Junit 5 Test case

I am using Junit5 to write unit test cases in java. I have few values that I have added in application.yaml but I am not able to retrieve them in my test file eg. ownerMasterList value is coming as null but it is present in application.yaml
#ExtendWith(MockitoExtension.class)
public class OwnerServiceTest {
#Value("${owner.master-list}")
private ownerMasterList;
#Test
void findAllOwners(){
---test detail
}
}
Using ConfigFileApplicationContextInitializer alone does not provide support for #Value("${…​}") injection. Its only job is to ensure that application.properties files are loaded into Spring’s Environment. For #Value support, you need to either additionally configure a PropertySourcesPlaceholderConfigurer or use #SpringBootTest, which auto-configures one for you.
From doc
#RunWith(SpringRunner.class)
#ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
public class OwnerServiceTest {...}
Please try providing property source
By reading your code, it seems that you are using JUnit5 (#ExtendWith(MockitoExtension.class)).
The correct extension to use to trigger the launch of a Spring application context is (#ExtendWith(SpringExtension.class)). If you are using Spring Boot, you can use the #SpringBootTest annotation which is itself annotated with #ExtendWith(SpringExtension.class).
One point to note though: you should not wire an application context when unit testing your code. You should rather test your class in isolation and inject the needed property (here ownerMasterList) through the constructor (see #BeforeEach JUnit 5 annotation):
class OwnerServiceTest {
private OwnerService ownerService;
#BeforeEach
void setUp() {
this.ownerService = new OwnerService(new ArrayList<>(...));
}
}

How to order Tests while building Spring Boot application

I am building spring boot application with unit testing. I have entities those are dependent on each other, Eg: I have User and Roles. To create User i should need Roles. I am testing roles and user object resources with MockMvc. I created Test class for Each Entity. When i run test UserTest class is executing Before Role Test class. So, all my tests are failing. I need help to run Test classes in an order.
As I mentioned in the comments, the best solution to such a problem is to avoid dependencies between test classes.
This can be achieved via proper test fixture setup and tear down (e.g., #Before and #After methods in JUnit 4).
Having said that, however, it is possible to order test classes in JUnit 4 if you execute them via a suite as in the following example.
#RunWith(Suite.class)
#SuiteClasses({RoleTests.class, UserTests.class})
public class MyTestSuite {
public static class RoleTests {
#Test
public void roleTest() {
System.err.println("roleTest");
}
}
public static class UserTests {
#Test
public void userTest() {
System.err.println("userTest");
}
}
}
That always prints:
roleTest
userTest

Running multiple tests using RestAssured serially

I have a Spring Boot application which is capable of running Integration tests using Rest Assured.
There is a single test class which has multiple test cases. I wish to run the test cases serially as given in the class.
public class ItemControllerTest{
#Before
public void setUp(){
...
}
#Test
public void test1(){
...
}
#Test
public void test2(){
...
}
}
When I run integration test,it seems test2 is getting executed before test1.
But I want them to run in the order they are written
I am not familiar with spring-boot, but if you are using Junit to run your tests, then you can run them serially by adding the following annotation above your class:
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ItemControllerTest{
// tests will run in order here
}

Spring 3+ How to create a TestSuite when JUnit is not recognizing it

I'm using Spring 3.0.4 and JUnit 4.5. My test classes currently uses Spring's annotation test support with the following syntax:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration (locations = { "classpath:configTest.xml" })
#TransactionConfiguration (transactionManager = "txManager", defaultRollback = true)
#Transactional
public class MyAppTest extends TestCase
{
#Autowired
#Qualifier("myAppDAO")
private IAppDao appDAO;
...
}
I don't really need the line extends TestCase to run this test. It's not needed when running this test class by itself. I had to add extends TestCase so that I can add it in a TestSuite class:
public static Test suite() {
TestSuite suite = new TestSuite("Test for app.dao");
//$JUnit-BEGIN$
suite.addTestSuite(MyAppTest.class);
...
If I omit the extends TestCase, my Test Suite will not run. Eclipse will flag suite.addTestSuite(MyAppTest.class) as error.
How do I add a Spring 3+ test class to a Test Suite? I'm sure there's a better way. I've GOOGLED and read the docs. If you don't believe me, I'm willing to send you all my bookmarks as proof. But in any case, I would prefer a constructive answer. Thanks a lot.
You are right; JUnit4-style tests should not extend junit.framework.TestCase
You can include a JUnit4 test as part of a JUnit3 suite this way:
public static Test suite() {
return new JUnit4TestAdapter(MyAppTest.class);
}
Usually you would add this method to the MyAppTest class. You could then add this test to your larger suite:
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("AllTests");
suite.addTest(MyAppTest.suite());
...
return suite;
}
}
You can create a JUnit4-style suite by creating a class annotated with Suite
#RunWith(Suite.class)
#SuiteClasses( { AccountTest.class, MyAppTest.class })
public class SpringTests {}
Note that AccountTest could be a JUnit4-style test or a JUnit3-style test.

Resources