Mock endpoint ignores message - java

I have the following test class:
#RunWith(Arquillian.class)
public class MyTest {
#Inject
#Uri("mock:repositoryRoute")
private ProducerTemplate producer;
#Inject
#Mock("direct:someStart")
private MockEndpoint endRoute;
#Inject
private ModelCamelContext modelContext;
#Before
public void init() throws Exception {
modelContext.start();
}
#After
public void stopContext() throws Exception {
modelContext.stop();
}
#Deployment
public static Archive<?> deploy() {
// deployment code
}
#Test
public void test1() throws Exception {
endRoute.expectedMessageCount(1);
producer.sendBody("direct:someStart", someBody);
endRoute.assertIsSatisfied();
}
#Test
public void test2() throws Exception {
endRoute.expectedMessageCount(1);
producer.sendBody("direct:someStart", someBody);
endRoute.assertIsSatisfied();
}
}
After I run the test class:
First test passes
Second test fails, even though it goes through the route.
However, if I run the test class with test1 ignored, then test2 passes.
Could someone please explain the reason for this behaviour and advise me how I can fix it?
EDIT
While debugging MockEndpoint and having I have noticed that counter field changes unexpectedly changes from number of tests run to zero while performing one test. So I wonder whether there is one more instance of MockEndpoint created?

Related

TestNg is running a test in a class after commenting #Test annotation

I have a class with list of tests annotated with #Test. I commented #Test annotation for one of the test however when I am running the suit the commented test is also running. Any idea on what's happening?
Example:
public class TestCasesClass {
#BeforeSuite
public void testSetup() throws Exception
{
super.testSetup();
}
#Test
public void test1() {
//Some test code
}
#Test
public void test12() {
//Some test code
}
//#Test
public void test3() {
//Some test code
}
}
When running the test suit all the test are running, including test3.
I tried #Test(enable-false) then also test running in the suit.

how to write Unit test for Singleton class in Java?

I am stuck while writing unit test for singleton class. I tried below things -
when Singletone.getInstance(1) run it is returning Null point exception. Can you please let me know what should be the solution for this?
#RunWith(PowerMockRunner.class)
#PrepareForTest({Singletone.class})
public class SingletoneTest {
#Before
public void setup() {
PowerMockito.mockStatic(Singletone.class);
}
#Test
public void test() {
AddUserDAO addUser = mock(AddUserDAO.Class);
when(Singletone.getInstance(1).getDAO()).thenReturn(addUser);
}
}

How to call tests from another test?

How can I call tests from another test?
I have a class in the jar that I have added as dependency into my project:
public class Tests{
private MockMvc mockMvc;
#Test
public void test1() throws Exception {
.....
mockMvc.perform(get(myRequest)
.content(dataFromDB)
.......
}
}
#Test
public void test2() throws Exception {
.....
mockMvc.perform(get(myRequest)
.content(dataFromDB)
.......
}
}
.......
And in my project I have:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest(classes = MyApp.class)
public class MyTests {
private MockMvc mockMvc;
#Autowired
private WebApplicationContext context;
#Before
public void init() throws Exception {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
}
#Test
public void test() throws Exception {
CALL SOMEHOW TESTS FROM THE JAR HERE
}
I want those tests from the jar to test my project's database (for example: dataFromDB should be some data from the project where this dependency has been added).
I have already added this jar and I can call class Tests inside my project,so I have access to it. I am just not sure how to run those tests inside it.
What should I change so it works well? Thanks
Updated:
*I want all tests from the jar to call at the same time, not individually.
*I want to give jar access to my db, so it can get all needed testing data in the db table of my project.
From what is see, you have 2 sets of environment, and 1 set of tests.
So one way to solve this is that you make the environment passable, the mockmvc, the dataFromDb, etc, so that the tests can execute independently of the environment.
I would suggest having the test methods in another class, like this very simplified example for easy reading:
class MyTestMethods {
void test1(TestEnv env, Req myRequest) {
env.getMockMvc()
.perform(env.get(myRequest)
.content(env.getDataFromDB());
// assertions here
}
}
class OldTestInJar {
#Test
public void test1() {
new MyTestMethods().test1(myEnv, myReq);
}
}
class MyNewTest {
#Test
public void test1() {
new MyTestMethods().test1(myNewEnv, myNewReq);
}
}

Arquillian: combining local and container code

I'm working with Arquillian with the JBoss 7 managed container. I'm writing a test to do the following:
Prepare the test locally, not on the JBoss server.
Run the test on the JBoss server.
Validate the output, not on the JBoss server.
Here is my first attempt at this:
#RunWith(Arquillian.class)
public class NotWorking {
#Inject
private Service service;
#Deployment
public static Archive<?> createDeployment() {
// ...
}
#Test
public void testService() throws Exception {
prepare();
service.executeService();
validate();
}
#RunAsClient
public void prepare() throws Exception {
LocalOnlyClass.prepare();
}
#RunAsClient
public void validate() throws Exception {
LocalOnlyClass.validate();
}
}
Unfortunately this doesn't work. Arquillian tries to run the preparation and validation on the server and fails to find the LocalOnlyClass. I can get this to work as follows but its ugly:
#RunWith(Arquillian.class)
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Working {
#Inject
private Service service;
#Deployment
public static Archive<?> createDeployment() {
// ...
}
#Test
#RunAsClient
public void testService1Prepare() throws Exception {
LocalOnlyClass.prepare();
}
#Test
public void testService2Test() throws Exception {
service.executeService();
}
#Test
#RunAsClient
public void testService3Validate() throws Exception {
LocalOnlyClass.validate();
}
}
Does anyone know of a better way to do this that avoids the "fake" tests?
I am not so sure about your comment:
Unfortunately this doesn't work. Arquillian tries to run the
preparation and validation on the server and fails to find the
LocalOnlyClass.
If your problem really lies in where the code gets executed, I don't see how JUnits #FixMethodOrder would make a difference. It just forces a execution order of the tests. The same can be achieved with Arquillian's #InSequence annotation which is what I would recommend in your case.

'assert' is ignored on Junit Tests with InstrumentationTestCase

I set up JUnit Test from android unit testing support and get "FAIL" result from the following test class.
public class FooTest extends AndroidTestCase {
#Before
public void setUp() throws Exception { super.setUp(); }
#After
public void tearDown() throws Exception { super.tearDown(); }
#Test
public void testCase1() { assertTrue(false); }
}
However, after replacing 'AndroidTestCase' with 'InstrumentationTestCase', I got "SUCCESS" result in spite of containing assertion that obviously returns 'FAIL'.
I would like to get to know the reason why I had got such a result, which are different by super classes and how to use Context in JUnit framework tests.

Categories

Resources