How do I get JMeter Junit Request Sampler to run #BeforeClass? - jmeter

I have the following Test class...
#RunWith(JUnit4.class)
public class AdminSpecTest extends BaseTest{
#BeforeClass
public static void getLogin(){
System.out.println("getLogin");
...
}
#Test
public void testAdminPage(){
System.out.println("testAdmin");
...
}
}
When I try to create a JUnit sampler in JMeter I run the test and it fails. In the terminal window I see...
testAdmin
but there is no getLogin. Why isn't BeforeClass getting called? Can I get this to happen?

Accoding to JMeter's JUnit you can use #Before instead for setup before test executes.
The following JUnit4 annotations are recognised:
#Before
treated the same as setUp() in JUnit3
#BeforeClass, #AfterClass
treated as test methods so they can be run independently as required

Yes you should be able to do this, but this solution involves adding another class at least temporarily while you are performance testing with JMeter. You then use this class to call the other test class (AdminSpecTest). Make sure you select this new class and method in JMeter's Junit Sampler dropdowns.
package test;
import org.junit.Test;
import org.junit.runner.JUnitCore;
public class TestRunner
{
public TestRunner()
{
}
#Test
public void runTests()
{
JUnitCore junit = new JUnitCore();
junit.run(AdminSpecTest.class);
}
}

Related

#SpyBean with few Integration tests doesn't work correctly

I have a strange behavior of #SpyBean field in my integration tests.
For example, I have a few integration tests:
package a;
#SpringBootTest
public class A {
#SpyBean
public MySpyBeanCandidate spyBean;
#Test
public void test1 {
// some work
Mockito.verify(spyBean, Mockito.atLeastOnce()).methodName(eq("String value"), anyString());
}
}
package a;
#SpringBootTest
public class B {
#SpyBean
public MySpyBeanCandidate spyBean;
#Test
public void test2 {
// some work
Mockito.verify(spyBean, Mockito.atLeastOnce()).methodName(eq("String value"), anyString());
}
}
The problem is when I try to execute them separately they executed successfully, but if I'll run them together, in the second test Mockito.verify(..) will throw an exception: Wanted but not invoked. But I have debugged it and checked that method (methodName) called correctly. Who knows why this is happening?
#DirtiesContext on each #Test method for such cases worked for me.

springboottest how to prevent running application

I have the standard Application class which runs a Spring batch ETL:
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
with my Junit test doing something like:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest
public class MyServiceTest {
#Autowired
MyService myService;
#Test
public void testInsertions() {
//do stuff with assertions
}
}
My problem is that when I execute the junit test, the application also kicks off the ETL then executes the test. How to prevent the application from running?
I think there are a lot of alternatives and it really depends on what you are trying to achieve.
One of the options would be to run your tests with a specific profile, like testing, and configure your ETLs (I'm assuming they are just jobs) to be configured based on some property or specific profile.
For example:
Testing class
#ActiveProfiles("testing")
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest
public class MyServiceTest {
...
}
Job/ETL classes
#Component
#Profile("!testing")
public class JobEtlService {
}
Hope it helps.

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
}

SpringJUnit4ClassRunner with JUnit testsuite error

I am trying to setup Junit test suite with Spring for the first time and tried with couple of changes in my classes, but no luck and ended up with this error : "junit.framework.AssertionFailedError: No tests found in Myclass"
Briefly, I have 2 test classes both are from same base class which loads Spring context as below
#RunWith( SpringJUnit4ClassRunner.class )
#ContextConfiguration( locations =
{
"classpath:ApplicationContext.xml"
})
I tried adding those 2 test classes into a suite as below
#RunWith( SpringJUnit4ClassRunner.class )
#SuiteClasses({ OneTest.class, TwoTest.class })
public class MyTestSuite extends TestCase {
//nothing here
}
I am running this test suite from ant. But, this gives me an error saying "No tests found"
However, If I run the individual 2 test cases from ant, they work properly. Not sure why is this behaviour, I am sure missing something here. Please advice.
As mentioned in the comments, we run the TestSuite with #RunWith(Suite.class) and list all the test cases with #SuiteClasses({}). In order to not repeat the #RunWith(SpringJunit4ClassRunner.class) and #ContextConfiguration(locations = {classpath:META-INF/spring.xml}) in each test case, we create an AbstractTestCase with these annotations defined on it and extend this abstract class for all test cases. A sample can be found below:
/**
* An abstract test case with spring runner configuration, used by all test cases.
*/
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations =
{ "classpath:META-INF/spring.xml" })
public abstract class AbstractSampleTestCase
{
}
public class SampleTestOne extends AbstractSampleTestCase
{
#Resource
private SampleInterface sampleInterface;
#Test
public void test()
{
assertNotNull(sampleInterface);
}
}
public class SampleTestTwo extends AbstractSampleTestCase
{
#Resource
private SampleInterface sampleInterface;
#Test
public void test()
{
assertNotNull(sampleInterface);
}
}
#RunWith(Suite.class)
#SuiteClasses(
{ SampleTestOne.class, SampleTestTwo.class })
public class SampleTestSuite
{
}
If you don't want to have an AbstractSampleTest, then you need to repeat the spring runner annotations on each test case, until Spring comes up with a SpringJunitSuiteRunner similar to how they need to add a SpringJunitParameterizedRunner.

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