BankNext’s enorme productieomgeving bevat meer dan 300 live microservices. Meerdere squads die gelijktijdig aan deze SVC’s werken, vergroot het risico op het breken van functionaliteit. Het handmatig toevoegen van JUnits en code-dekking aan bestaande en nieuwe code is tijdrovend en pijnlijk traag.
Problemen met handmatige JUnits
- Tijdrovende activiteit om handmatig juiste en nuttige JUnits te schrijven.
- Lackt standaardisatie omdat elke JUnit een andere aanpak gebruikt.
- Ontbrekende/onjuiste JUnits worden gemaakt door tijdsgebrek.
- Handmatige synchronisatie van bestaande JUnits door veranderende code is onpraktisch.
- Het schrijven van JUnits handmatig voor legacy code is een nachtmerrie.
- De laagste prioriteit wordt toegekend aan JUnits vanwege deadlines; daarom worden ze vaak overgeslagen.
- De codekwaliteit lijdt enorm en technisch schuld stapelt zich op.
Oplossing: JUnit-Mockito Automatisering
- GitHub
- Automatisering neemt de klasse-naam in en creëert werkende JUnits.
- Deze gegenereerde JUnits bevatten de noodzakelijke Mockito mocks.
- Behandelt JUnits voor RestControllers, Services, Handlers, Kafka-klassen, enz.
- Zo wordt in bijna alle scenario’s > 70% code-dekking bereikt.
Automatisering mogelijkheden
- Werkt zowel voor SpringBoot 2.x als 3.x Maven-gebaseerde toepassingen.
- Bijna geen opstapproblemen.
- Neemt het lokale pad van uw toepassing & zeer basale gebruikersinvoer.
- Gebruikt Reflectie-hulpmiddelen om de structuurdetails van de toepassing af te leiden.
- Identificeert soepel de benodigde Mockbeans.
- Genereert automatisch “When-Then” Mockito-mocks.
- Genereert Jacoco-code-dekkingsrapporten.
Structuur van een Legale JUnit
- Verplichte delen: 3
- Roep de te testen doelmethode aan
- Mock elke interactie die extern is aan deze klasse
- Controleer of de werkelijke uitvoer overeenkomt met de verwachte assert/verify
- Identificeer en declareer alle externe klassen als MockBeans
- Stel de verwachte reacties in van deze MockBean-interacties
- Hier zijn de basisregels voor een basislegale werking JUnit
Shell
Junit-Mockito Ground Rules
1- target mtd to be tested createCustomer is a void returnType
2- external mtd invoked .delete is a void returnType
3- when-then : doNothing whenInvoke .delete
4- assertion : verify .delete called 1 times
1- target mtd to be tested createCustomer is a void returnType
2- external mtd invoked .save is a Customer returnType
3- when-then : when save then return new Customer
4- assertion : verify .save called 1 times
1- target mtd to be tested createCustomer is a Customer returnType
2- external mtd invoked .save is a Customer returnType
3- when-then : when save then return new Customer
4- assertion : assert result instanceof Customer / Customer is not null
1- target mtd to be tested createCustomer is a Customer returnType
2- external mtd invoked .findAll is a List returnType
3- when-then : when findAll then return new ArrayList
4- assertion : assert result instanceof List / List.size >0
Automatisering Demo
Shell
cd C:\Vijay\Java [your local machine path]
git clone https://github.com/vijayredkar/junit-mockito-automation.git
# Mijn uiteindelijke directorystructuur
# C:\Vijay\Java\AutomationJunitMockito\appsetup
# C:\Vijay\Java\projects\junit-mockito-mgt\test-projects\junit-automation
cd C:\Vijay\Java\AutomationJunitMockito\appsetup
setup.bat
------- Demo Stappen ------
https://vijayredkar.medium.com/banknext-case-study-junit-mockito-automation-ac9f6b72cfcc
https://github.com/vijayredkar/junit-mockito-automation
C:\Vijay\Java\AutomationJunitMockito\appsetup
demo scenario -1
com.banknext.customer.mgt.service.CustomerServiceImplType1
com.banknext.customer.mgt.controller.CustomerMgtControllerType1
demo scenario -2
com.banknext.customer.mgt.service.CustomerServiceImplType2
com.banknext.customer.mgt.controller.CustomerMgtControllerType2
demo scenario -3
com.banknext.customer.mgt.event.publisher.CustomerTxnPublisherType1
com.banknext.customer.mgt.event.consumer.CustomerTxnConsumerType1
demo scenario -4
https://github.com/bezkoder/spring-boot-h2-database-crud/tree/master
cd C:\Vijay\Java\projects\junit-mockito-mgt\test-projects
#git clone https://github.com/bezkoder/spring-boot-h2-database-crud.git
cd C:\Vijay\Java\projects\junit-mockito-mgt\test-projects\spring-boot-h2-database-crud
mvn clean install
cd C:\Vijay\Java\AutomationJunitMockito\appsetup
run setup.bat
cd C:\Vijay\Java\projects\junit-mockito-mgt\test-projects\spring-boot-h2-database-crud
mvn clean install
import in Eclipse
add POM dependencies
check current Jacoco code coverage
continue automation run
check/adjust generated Junits
check current Jacoco code coverage
cd C:\Vijay\Java\projects\junit-mockito-mgt\test-projects\spring-boot-h2-database-crud
check new Jacoco code coverage
run all tests
cd C:\Vijay\Java\projects\junit-mockito-mgt\test-projects\spring-boot-h2-database-crud
mvn clean install
check again all Jacoco code coverage
C:\Vijay\Java\projects\junit-mockito-mgt\test-projects\spring-boot-h2-database-crud\src\main\java\com\bezkoder\spring\jpa\h2
com.bezkoder.spring.jpa.h2.controller.TutorialController
com.bezkoder.spring.jpa.h2.model.Tutorial
TutorialController
Voor en na automatisering
Conclusie
- BankNext bespaart enorm veel tijd en moeite met automatische JUnit-creatie.
- Dit leidt rechtstreeks tot kostenbesparingen bij projecten.
- De code-dekkingsgraad neemt aanzienlijk toe.
- Het team kan volledig focussen op echte ontwikkelingsactiviteiten.
- JUnits worden consistent toegevoegd/onderhouden met bijna geen handmatige inspanning.
- Technische schuld wordt aanzienlijk verminderd.
- Verhoogt aanzienlijk het vertrouwen in de toepassingen die worden geïmplementeerd in productie.
Source:
https://dzone.com/articles/banknext-case-study-junit-mockito-automation-1