How to make a test case as fail in Junit? - junit

When i use fail()[Junit] in the script, the scripts stops running and Skipped the next steps.
In TestNG, We can do that using "org.testng.Assert.fail("");" .
my requirement is to continue to run the next scenario even if my previous case was failure .
Please help me .

You need to use soft asssertions. Something like this
public static void verifyFalse(boolean condition) {
try {
assertFalse(condition);
} catch(Throwable e) {
e.printStackTrace();
throw new YourException("your message");
}

JUnit has the ErrorCollector rule for soft assertions.
public class ATest {
#Rule
public final ErrorCollector collector = new ErrorCollector();
#Test
public void test() {
//do some stuff
collector.addError(new Throwable("something went wrong"));
//do other stuff
}
}

Related

junit for multi threaded class with mockito

Please, help me write a JUnit test for this code using Mockito.
class A{
private BlockingQueue<Runnable> jobQueue;
public void methodA(List<String> messages) {
try {
jobQueue.put(() -> methodB(message));
} catch(InterruptedException e) {}
}
private void methodB(Message message) {
//other logic
}
}
Your example lacks context as to what it is methodB is doing... Without knowing what the functionality is that you want to verify, just verifying that methodB gets called wouldn't be a particularly useful test, nor is mocking the BlockingQueue. I'm going to go out on a limb and assume that methodB interacts with another object, and it's this interaction that you really want to verify, if that's the case my code and test would look something like:
class A {
private BlockingQueue<Runnable> jobQueue;
private B b;
public void methodA(Message message) {
try {
jobQueue.put(() -> methodB(message));
} catch (InterruptedException e) {
}
}
private void methodB(Message message) {
b.sendMethod(message);
}
}
class B {
public void sendMethod(Message message) {
// other logic
}
}
And my test would potentially look something like:
class Atest {
private A testSubject;
#Mock
private B b;
#Test
public void testASendsMessage() {
Message message = new Message("HELLO WORLD");
testSubject.methodA(message);
verify(b, timeout(100)).sendMethod(message);
}
#Before
public void setup() throws Exception {
testSubject = new A();
}
}
In general you want to avoid needing to verifying bits with multiple threads in a unit test, save tests with multiple running threads mainly for integration tests but where it is necessary look at Mockito.timeout(), see example above for how to use. Hopefully this helps?

How do you test exceptions using mockito in RCP Application?

I have the following code in my performFinish() method of my Wizard Class :
public boolean performFinish() {
try {
getContainer().run(true, false, changeArtifactRunnable());
}
catch (InvocationTargetException | InterruptedException e) {
LoggerClass.logException(e);
}
I want to test Exception for InvocationTargetException and InterruptedException using Mockito.
In the above code, getContainer() method is from org.eclipse.jface.wizard.Wizard class and
public void run(boolean fork, boolean cancelable,
IRunnableWithProgress runnable) throws InvocationTargetException,
InterruptedException;
method is from org.eclipse.jface.operation.IRunnableContext class.
How do I test both the exceptions in performFinish() method?
You can use the expected keyword in order to do so. For example:
#Test(expected = InvocationTargetException.class)
public void testInvocationTargetException() {
\\Invoke the method to be tested under the conditions, such that InvocationTargetException is thrown by it. No need of any assert statements
}
===========================================================================
Edit:
#RunWith(MockitoJUnitRunner.class)
public class EditArtifactWizardTest {
#Spy
//use correct constructor of EditArtifactWizard
private EditArtifactWizard editArtifactWizardSpy=Mockito.spy(new EditArtifactWizard ());
#Test(expected = InvocationTargetException.class)
public void testInvocationTargetException() {
\\Invoke the method to be tested under the conditions, such that InvocationTargetException is thrown by it. No need of any assert statements
Mockito.when(editArtifactWizardSpy.getContainer()).thenThrow(InvocationTargetException.class);
editArtifactWizardSpy.performFinish();
}
}
You can create the Spy of EditArtifactWizard class and mock the behavior of the getContainerMethod.
P.S: Please excuse for typos or compilation error as I am not using any editor.

JMockit incompatableClassChangeError when using NonStrictExpectations result =

Ok I am having this issue at work where I am using JMockit 1.19 and trying to use NonStrictExpectations to make a method throw an exception. However I keep getting this "IncompatableClassChangeError." I'm not allowed to share my exact code at work but I have recreated the problem with example code below that is giving me the same issue
Class Being Tested
public class TestClassRemove {
A a = new A();
public String testMethod(){
String s = null;
try{
s = a.getA();
}
catch(Exception e){
System.out.println("caught exception");
}
return s;
}
}
Class A whose method is called by TestClassRemove
public class A {
public String getA(){
return "hello";
}
}
Test Class where Im having the problem
#RunWith(JMockit.class)
public class TestingTests {
private TestClassRemove testClassRemove = new TestClassRemove();
#Mocked
A a;
#Test (expected=Exception.class)
public void testResultsEqual(){
new NonStrictExpectations(){{
a.getA();
result = new Exception(); //This throws "IncompatableClassChangeError"
}};
testClassRemove.testMethod();
}
}
for some reason the line " result = new Exception(); " is throwing this "IncompatableClassChangeError." Any ideas????????
Sorry for the poorly named classes
Ok, so I FINALLY solved this issue after almost a week of working on this and asking everyone in the office. I had linked another project on my build path and that's what caused this issue. Not only did it affect this but this prevented eclEmma from working as well as Mockito. Linking projects on your build path will cause TONS of errors using JMockit, Mockito, and eclEmma.

Re-run failed Test Script 'completely' using JUnit

I found some solution to rerun failed #Test in this forum at How to Re-run failed JUnit tests immediately?. In my case i execute the test from command line. And i want to rerun the complete test if it fails. Given below is my Test Script template and i want to rerun everything (from start to end) if it fails
#BeforeClass
public static void Start(){
...
}
#Test
public void Test_One(){
...
}
#Test
public void Test_Two(){
...
}
#AfterClass
public static void End(){
...
}
I will get to know in End() method if my test script has failed. If it fails, i would like to run everything like
#BeforeClass
#Test (all #Test)
#AfterClass
Is it possible with JUnit?
I am not sure if the template that i am using is correct :(
JanakiL,
It is a very good question. I tried to find some solution but i didn't manage to find clean solution for this task.
I can only propose to do some workaround that eventually will work.
So, in order to re-run suite you need to do following steps:
You need to create #ClassRule in order to execute whole suite.
All Suite you can retry using following code:
public class Retrier implements TestRule{
private int retryCount;
private int failedAttempt = 0;
#Override
public Statement apply(final Statement base,
final Description description) {
return new Statement() {
#Override
public void evaluate() throws Throwable {
base.evaluate();
while (retryNeeded()){
log.error( description.getDisplayName() + " failed");
failedAttempt++;
}
}
}
retryNeeded() – method that determines whether you need to do retry or not
This will retry all tests in your suite. Very important thing that retry will go after #AfterClass method.
In case you need to have “green build” after successful retry you need to write a bunch of another gloomy code.
You need to create #Rule that will not allow “publish” failed result. As example:
public class FailedRule extends TestWatcher {
#Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
#Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
try {
base.evaluate();
} catch (AssumptionViolatedException e) {
log.error("", e.getMessage());
if (isLastRun()) {
throw e;
}
} catch (Throwable t) {
log.error("", t.getMessage());
if (isLastRun()) {
throw t;
}
}
};
};
}
}
isLastRun() – methods to verify whether it is last run and fail test only in case ir is last run.
Only last retry needs to be published to mark you test failed and build “red”.
3. And finally in your test class you need do register two rules:
#Rule
public FailedRule ruleExample = new FailedRule ();
#ClassRule
public static Retrier retrier = new Retrier (3);
In the Retrier you can pass count of attempts to retry.
I hope that somebody could suggest better solution !
More flexible solution is to write custom runner.
I described this answer in the following post:
How to Re-run failed JUnit tests immediately?
Using Eclipse, in the Junit view, you have a button "ReRun Tests - Failures First"
You can also see this answer: How to Re-run failed JUnit tests immediately?

Apache Camel Integration Test - NotifyBuilder

I am writing integration tests to test existing Routes. The recommended way of getting the response looks something like this (via Camel In Action section 6.4.1):
public class TestGetClaim extends CamelTestSupport {
#Produce(uri = "seda:getClaimListStart")
protected ProducerTemplate producer;
#Test
public void testNormalClient() {
NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create();
producer.sendBody(new ClientRequestBean("TESTCLIENT", "Y", "A"));
boolean matches = notify.matches(5, TimeUnit.SECONDS);
assertTrue(matches);
BrowsableEndpoint be = context.getEndpoint("seda:getClaimListResponse", BrowsableEndpoint.class);
List<Exchange> list = be.getExchanges();
assertEquals(1, list.size());
System.out.println("***RESPONSE is type "+list.get(0).getIn().getBody().getClass().getName());
}
}
The test runs but I get nothing back. The assertTrue(matches) fails after the 5 second timeout.
If I rewrite the test to look like this I get a response:
#Test
public void testNormalClient() {
producer.sendBody(new ClientRequestBean("TESTCLIENT", "Y", "A"));
Object resp = context.createConsumerTemplate().receiveBody("seda:getClaimListResponse");
System.out.println("***RESPONSE is type "+resp.getClass().getName());
}
The documentation is a little light around this so can anyone tell me what I am doing wrong with the first approach? Is there anything wrong with following the second approach instead?
Thanks.
UPDATE
I have broken this down and it looks like the problem is with the mix of seda as the start endpoint in combination with the use of a recipientList in the Route. I've also changed the construction of the NotifyBuilder (I had the wrong endpoint specified).
If I change the start endpoint to
direct instead of seda then the test will work; or
If I comment out the recipientList
then the test will work.
Here's a stripped down version of my Route that reproduces this issue:
public class TestRouteBuilder extends RouteBuilder {
#Override
public void configure() throws Exception {
// from("direct:start") //works
from("seda:start") //doesn't work
.recipientList(simple("exec:GetClaimList.bat?useStderrOnEmptyStdout=true&args=${body.client}"))
.to("seda:finish");
}
}
Note that if I change the source code of the NotifyTest from the "Camel In Action" source to have a route builder like this then it also fails.
Try to use "seda:getClaimListResponse" in the getEndpoint to be sure the endpoint uri is 100% correct
FWIW: It appears that notifyBuilder in conjunction with seda queues are not quite working: a test class to illustrate:
public class NotifyBuilderTest extends CamelTestSupport {
// Try these out!
// String inputURI = "seda:foo"; // Fails
// String inputURI = "direct:foo"; // Passes
#Test
public void testNotifyBuilder() {
NotifyBuilder b = new NotifyBuilder(context).from(inputURI)
.whenExactlyCompleted(1).create();
assertFalse( b.matches() );
template.sendBody(inputURI, "Test");
assertTrue( b.matches() );
b.reset();
assertFalse( b.matches() );
template.sendBody(inputURI, "Test2");
assertTrue( b.matches() );
}
#Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
#Override
public void configure() throws Exception {
from(inputURI).to("mock:foo");
}
};
}
}