Nitin Agrawal
Contact -
  • Home
  • Interviews
    • InterviewFacts
    • Resume Thoughts
    • Companies >
      • InvestmentBanks >
        • ECS
        • Bank Of America
        • WesternUnion
        • WellsFargo
      • ProductBasedCompanies >
        • CA Technologies
        • Model N India
        • Verizon Media
        • Oracle & GoJek
        • IVY Computec
        • Samsung
        • ClearWaterAnalytics
        • ADP
        • ServiceNow
        • Pubmatic
        • Expedia
        • Amphora
        • CDK Global
        • Delphix
        • Fractal Enterprises LLP
        • CDK Global
        • Tide-Banking
        • Epic
      • ServiceBasedCompanies >
        • ASG World Wide Pvt Ltd
        • Paraxel International & Pramati Technologies Pvt Ltd
        • MitraTech
        • Intelizest Coding Round
        • ZeMoSo
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems
      • AnagramsSet
    • Core Java >
      • Codility
      • Java14
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
      • Important Points
      • Immutability
      • Dictionary
      • Sample Code Part 1 >
        • PatternLength
        • Serialization >
          • Kryo2
          • JAXB/XSD
          • XStream
        • MongoDB
        • New methods in Collections
        • MethodReferences
        • Complex Objects Comparator >
          • Performance
        • NIO >
          • NIO 2nd Sample
        • Date Converter
        • Minimum cost path
        • Find File
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • Decorator
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • RequestChaining
        • Singleton >
          • Singletons
  • Frameworks
    • AngularJS
    • Apache Velocity
    • Spring >
      • Spring Boot >
        • Issues
      • Quick View
    • Rest WebServices >
      • Interviews
      • Swagger
    • Cloudera BigData >
      • Ques_Ans
      • Hive
      • Apache Spark >
        • SparkCode
        • Sample1
        • DataFrames
        • RDDs
        • SparkStreaming
        • SparkFiles
    • Integration >
      • Apache Camel
    • Testing Frameworks >
      • JUnit >
        • JUnit Runners
      • EasyMock
      • Mockito >
        • Page 2
      • TestNG
    • Blockchain >
      • Ethereum Smart Contract
      • Blockchain Java Example
    • Microservices >
      • Design Patterns
    • AWS >
      • Honeycode
  • Databases
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search >
      • StudySources
  • Random issues
    • TOAD issue
    • Architect's suggestions
    • ApacheSpark Installation
  • Your Views
While working with JUnit, it is good to have the idea about the JUnit runners around, as we use one or the other such runners.
So good to know that one can write such Runners for the custom usage.
Try to google around this & check. -
https://www.baeldung.com/junit-4-custom-runners
https://putridparrot.com/blog/writing-a-custom-junit-test-runner/

Followed above both URLs to have below a basic Custom Runner

public class CustomJUnitRunner extends Runner {

private Class testClass;
private HashMap<Method, Description>  methodDescriptions;
/**
* 
*/
public CustomJUnitRunner(Class testClass) {
this.testClass = testClass;
methodDescriptions = new HashMap<>();
}

@Override
public Description getDescription() {
Description description = 
Description.createSuiteDescription(
testClass.getName(), 
testClass.getAnnotations());

for(Method method : testClass.getMethods()) {
Annotation annotation = 
method.getAnnotation(Test.class);
if(annotation != null) {
Description methodDescription =
Description.createTestDescription(
testClass,
method.getName(), 
annotation);
description.addChild(methodDescription);

methodDescriptions.put(method, methodDescription);
}
}

return description;
}

@Override
public void run(RunNotifier notifier) {
System.out.println("running the tests from CustomJUnitRunner: " + testClass);
try {
Object testObject = testClass.getDeclaredConstructor().newInstance();
methodDescriptions.forEach((method, description) -> {
try {
if (method.isAnnotationPresent(Test.class)) {
notifier.fireTestStarted(Description
.createTestDescription(testClass, method.getName()));
method.invoke(testObject);
notifier.fireTestFinished(Description
.createTestDescription(testClass, method.getName()));
}
} catch (Exception e) {
Failure failure = new Failure(description, e.getCause());
            notifier.fireTestFailure(failure);
} finally {
notifier.fireTestFinished(description);
}
});
} catch(Exception e) {
e.printStackTrace();
}
}
}
Powered by Create your own unique website with customizable templates.