עקוף נכסים במבחני האביב

1. סקירה כללית

במדריך זה נבחן דרכים שונות לעקוף מאפיינים במבחני אביב.

אביב מספק למעשה מספר פתרונות לכך, כך שיש לנו לא מעט לחקור כאן.

2. תלות

כמובן שכדי לעבוד עם מבחני אביב עלינו להוסיף תלות במבחן:

 org.springframework.boot spring-boot-starter-test 2.1.6.RELEASE test 

ה מבחן אביב-אתחול-התחלה התלות מכילה את כל מה שאנחנו צריכים כדי לעקוף את ערך המאפיין במבחנים.

3. התקנה

ראשית, נצטרך ליצור מחלקה ביישום שתשתמש במאפיינים שלנו:

@Component מחלקה ציבורית PropertySourceResolver {@Value ("$ {example.firstProperty}") פרטי מחרוזת firstProperty; @Value ("$ {example.secondProperty}") פרטי מחרוזת secondProperty; מחרוזת ציבורית getFirstProperty () {להחזיר firstProperty; } מחרוזת ציבורית getSecondProperty () {return secondProperty; }}

לאחר מכן, בואו נקצה להם ערכים. אנו יכולים לעשות זאת על ידי יצירת ה- application.properties בתוך ה src / main / resources:

example.firstProperty = defaultFirst example.secondProperty = defaultSecond

4. עקיפת קובץ נכס

כעת נעקוף מאפיינים על ידי הכנסת קובץ המאפיין למשאבי הבדיקה. קובץ זה חייב להיותבאותו מסלול כיתה כברירת המחדל.

בנוסף, זה צריך מכילים את כל מפתחות המאפיינים שצוין בקובץ ברירת המחדל. לכן, נוסיף את application.properties התיק לתוך src / test / resources:

example.firstProperty = קובץ example.secondProperty = קובץ

בואו נוסיף גם את המבחן שיעשה שימוש בפתרון שלנו:

@RunWith (SpringRunner.class) @SpringBootTest המחלקה הציבורית TestResourcePropertySourceResolverIntegrationTest {@Autowired PrivateSourceResolver propertySourceResolver; בטל ציבורי @Test צריך TestResourceFile_overridePropertyValues ​​() {String firstProperty = propertySourceResolver.getFirstProperty (); מחרוזת secondProperty = propertySourceResolver.getSecondProperty (); assertEquals ("קובץ", firstProperty); assertEquals ("קובץ", secondProperty); }}

שיטה זו יעילה מאוד כאשר אנו רוצים לעקוף מספר מאפיינים מהקובץ.

ואם לא שמנו את example.secondProperty בקובץ, הקשר היישום לא יגלה מאפיין זה.

5. פרופילי אביב

בחלק זה נלמד כיצד להתמודד עם הבעיה שלנו באמצעות פרופילי אביב. בניגוד לשיטה הקודמת,זה ממזג מאפיינים מקובץ ברירת המחדל וקובץ צדודית.

ראשית, בואו ניצור יישוםtest.properties קובץ ב- src / test / resources:

example.firstProperty = פרופיל

לאחר מכן, ניצור מבחן שישתמש ב- מִבְחָן פּרוֹפִיל:

@RunWith (SpringRunner.class) @SpringBootTest @ ActiveProfiles ("test") מחלקה ציבורית ProfilePropertySourceResolverIntegrationTest {@Autowired פרטי PropertySourceResolver propertySourceResolver; בטל ציבורי @Test צריךProfiledProperty_overridePropertyValues ​​() {String firstProperty = propertySourceResolver.getFirstProperty (); מחרוזת secondProperty = propertySourceResolver.getSecondProperty (); assertEquals ("פרופיל", firstProperty); assertEquals ("defaultSecond", secondProperty); }}

גישה זו מאפשרת לנו להשתמש גם בערכי ברירת מחדל וגם בערכי בדיקה. לכן זו שיטה נהדרת כשאנחנו צריכים עקוף מאפיינים מרובים מקובץ אך אנו עדיין רוצים להשתמש בברירת המחדל יחידות.

בנוסף, אנו יכולים ללמוד עוד על פרופילי אביב בפורמט שלנו פרופילי אביב הדרכה.

6. @ SpringBootTest

דרך נוספת לעקוף את ערך הנכס היא להשתמש ב- @ SpringBootTest ביאור:

@RunWith (SpringRunner.class) @SpringBootTest (מאפיינים = {"example.firstProperty = ביאור"}) מחלקה ציבורית SpringBootPropertySourceResolverIntegrationTest {@Autowired פרטי PropertySourceResolver propertySourceResolver; @Test ציבור בטל צריךSpringBootTestAnnotation_overridePropertyValues ​​() {String firstProperty = propertySourceResolver.getFirstProperty (); מחרוזת secondProperty = propertySourceResolver.getSecondProperty (); Assert.assertEquals ("הערה", firstProperty); Assert.assertEquals ("defaultSecond", secondProperty); }}

כמו שאנו יכולים לראות,ה example.firstProperty הושג בזמן שה- example.secondProperty לא היה. לכן, זהו פיתרון נהדר כאשר עלינו לעקוף רק מאפיינים ספציפיים לבדיקה. זוהי השיטה היחידה הדורשת שימוש באביב אתחול.

7. TestPropertySourceUtils

בחלק זה נלמד כיצד לעקוף מאפיינים באמצעות ה- TestPropertySourceUtils בכיתה ב ApplicationContextInitializer.

ה TestPropertySourceUtils מגיע עם שתי שיטות שבהן אנו יכולים להשתמש כדי להגדיר ערך נכס שונה.

בואו ניצור כיתת אתחול בה נשתמש במבחן שלנו:

מחלקה ציבורית PropertyOverrideContextInitializer מיישמת את ApplicationContextInitializer {static final String PROPERTY_FIRST_VALUE = "contextClass"; @Override חלל ציבורי מאותחל (ConfigurableApplicationContext configurableApplicationContext) {TestPropertySourceUtils.addInlinedPropertiesToEnvironment (configurableApplicationContext, "example.firstProperty =" + PROPERTY_FIRST_VALUE); TestPropertySourceUtils.addPropertiesFilesToEnvironment (configurableApplicationContext, "context-override-application.properties"); }}

לאחר מכן נוסיף את ההקשר- override-application.properties לתיק לתוך src / test / resources:

example.secondProperty = contextFile

לבסוף, עלינו ליצור כיתת בדיקה שתשתמש באתחול שלנו:

@RunWith (SpringRunner.class) @ContextConfiguration (initializers = PropertyOverrideContextInitializer.class, classes = Application.class) class class ContextPropertySourceResolverIntegrationTest {@Autowired private PropertySourceResolver propertySourceResolver; @Test הציבור בטל צריךContext_overridePropertyValues ​​() {final String firstProperty = propertySourceResolver.getFirstProperty (); מחרוזת סופית secondProperty = propertySourceResolver.getSecondProperty (); assertEquals (PropertyOverrideContextInitializer.PROPERTY_FIRST_VALUE, firstProperty); assertEquals ("contextFile", secondProperty); }}

ה example.firstProperty הושג מהשיטה המוטבעת.

ה example.secondProperty בוטל מהקובץ הספציפי בשיטה השנייה. גישה זו מאפשרת לנו להגדיר ערכי נכסים שונים בעת אתחול ההקשר.

8. מסקנה

במדריך זה התמקדנו בדרכים המרובות שבהן אנו יכולים להשתמש כדי לעקוף מאפיינים במבחנים שלנו.

גילינו גם מתי להשתמש בכל פתרון או במקרים מסוימים מתי לערבב אותם.

יש לנו, כמובן, את @TestPropertySource ביאור העומד לרשותנו גם כן.

כמו תמיד, הקוד לדוגמאות זמין ב- GitHub.


$config[zx-auto] not found$config[zx-overlay] not found