Best Assertj code snippet using org.assertj.core.api.AbstractMapAssert.containsOnlyKeys
Source:JSONLogFormatterTest.java  
...44        String message = logFormatter.format(record);45        ZonedDateTime zdtEnd = ZonedDateTime.ofInstant(record.getInstant(), ZoneId.systemDefault());46        CustomAssertions.assertThat(message).isJsonFormat();47        AbstractMapAssert mapAssert = CustomAssertions.assertThat(message).asMap();48        mapAssert.containsOnlyKeys("LoggerName",49                "LogMessage",50                "ThreadID",51                "ThreadName",52                "Level",53                "TimeMillis",54                "Timestamp",55                "LevelValue");56        mapAssert.containsEntry("LoggerName", "JUnit.test");57        mapAssert.containsEntry("LogMessage", "Just a message");58        mapAssert.containsEntry("ThreadID", "1");59        mapAssert.containsEntry("ThreadName", "main");60        mapAssert.containsEntry("Level", "INFO");61        mapAssert.containsEntry("LevelValue", "800");62        CustomAssertions.assertThat(message).hasTimeStamp(zdtStart, zdtEnd);63    }64    @Test65    void format_excludedFields() {66        JSONLogFormatter logFormatter = new JSONLogFormatter("tid,timeMillis,levelValue");67        LogRecord record = new LogRecord(Level.INFO, "Just a message");68        record.setLoggerName("JUnit.test");69        // Ignored at level INFO70        record.setSourceClassName("be.atbash.runtime.Foo");71        record.setSourceMethodName("bar");72        String message = logFormatter.format(record);73        CustomAssertions.assertThat(message).isJsonFormat();74        AbstractMapAssert mapAssert = CustomAssertions.assertThat(message).asMap();75        mapAssert.containsOnlyKeys("LoggerName",76                "LogMessage",77                "Level",78                "Timestamp");79        mapAssert.containsEntry("LoggerName", "JUnit.test");80        mapAssert.containsEntry("LogMessage", "Just a message");81        mapAssert.containsEntry("Level", "INFO");82    }83    @Test84    void format_levelFINE() {85        JSONLogFormatter logFormatter = new JSONLogFormatter("");86        LogRecord record = new LogRecord(Level.FINE, "Just a message");87        record.setLoggerName("JUnit.test");88        record.setSourceClassName("be.atbash.runtime.Foo");89        record.setSourceMethodName("bar");90        ZonedDateTime zdtStart = ZonedDateTime.ofInstant(record.getInstant(), ZoneId.systemDefault());91        String message = logFormatter.format(record);92        ZonedDateTime zdtEnd = ZonedDateTime.ofInstant(record.getInstant(), ZoneId.systemDefault());93        CustomAssertions.assertThat(message).isJsonFormat();94        AbstractMapAssert mapAssert = CustomAssertions.assertThat(message).asMap();95        mapAssert.containsOnlyKeys("LoggerName",96                "LogMessage",97                "ThreadID",98                "ClassName",99                "MethodName",100                "ThreadName",101                "Level",102                "TimeMillis",103                "Timestamp",104                "LevelValue");105        mapAssert.containsEntry("LoggerName", "JUnit.test");106        mapAssert.containsEntry("LogMessage", "Just a message");107        mapAssert.containsEntry("ThreadID", "1");108        mapAssert.containsEntry("ThreadName", "main");109        mapAssert.containsEntry("Level", "FINE");110        mapAssert.containsEntry("LevelValue", "500");111        mapAssert.containsEntry("ClassName", "be.atbash.runtime.Foo");112        mapAssert.containsEntry("MethodName", "bar");113        CustomAssertions.assertThat(message).hasTimeStamp(zdtStart, zdtEnd);114    }115    @Test116    void format_withException() {117        JSONLogFormatter logFormatter = new JSONLogFormatter("");118        LogRecord record = new LogRecord(Level.INFO, "Just a message");119        record.setLoggerName("JUnit.test");120        RuntimeException exception = new RuntimeException("RuntimeException message");121        record.setThrown(exception);122        String message = logFormatter.format(record);123        CustomAssertions.assertThat(message).isJsonFormat();124        AbstractMapAssert mapAssert = CustomAssertions.assertThat(message).asMap();125        mapAssert.containsOnlyKeys("LoggerName",126                "Throwable",127                "LogMessage",128                "ThreadID",129                "ThreadName",130                "Level",131                "TimeMillis",132                "Timestamp",133                "LevelValue");134        mapAssert.containsEntry("LoggerName", "JUnit.test");135        mapAssert.containsEntry("ThreadID", "1");136        mapAssert.containsEntry("ThreadName", "main");137        mapAssert.containsEntry("LogMessage", "Just a message");138        mapAssert.containsEntry("Level", "INFO");139        mapAssert.containsEntry("LevelValue", "800");140        Object parsed = new JSONParser().parse(message);141        Assertions.assertThat(parsed).isInstanceOf(JSONObject.class);142        JSONObject jsonObject = (JSONObject) parsed;143        Object throwable = jsonObject.get("Throwable");144        Assertions.assertThat(throwable).isInstanceOf(JSONObject.class);145        jsonObject = (JSONObject) throwable;146        Assertions.assertThat(jsonObject).containsOnlyKeys("StackTrace", "Exception");147        //Any reasonable way to test we have a stacktrace here?148        Assertions.assertThat(jsonObject).containsEntry("Exception", "RuntimeException message");149    }150    @Test151    void format_withException_nullMessage() {152        JSONLogFormatter logFormatter = new JSONLogFormatter("");153        LogRecord record = new LogRecord(Level.INFO, "");154        record.setLoggerName("JUnit.test");155        RuntimeException exception = new RuntimeException();156        record.setThrown(exception);157        String message = logFormatter.format(record);158        System.out.println(message);159        CustomAssertions.assertThat(message).isJsonFormat();160        AbstractMapAssert mapAssert = CustomAssertions.assertThat(message).asMap();161        mapAssert.containsOnlyKeys("LoggerName",162                "Throwable",163                "ThreadID",164                "ThreadName",165                "Level",166                "TimeMillis",167                "Timestamp",168                "LevelValue");169        //Log Message is not included when exception is thrown but part of Throwable170        mapAssert.containsEntry("LoggerName", "JUnit.test");171        mapAssert.containsEntry("ThreadID", "1");172        mapAssert.containsEntry("ThreadName", "main");173        mapAssert.containsEntry("Level", "INFO");174        mapAssert.containsEntry("LevelValue", "800");175        Object parsed = new JSONParser().parse(message);176        Assertions.assertThat(parsed).isInstanceOf(JSONObject.class);177        JSONObject jsonObject = (JSONObject) parsed;178        Object throwable = jsonObject.get("Throwable");179        Assertions.assertThat(throwable).isInstanceOf(JSONObject.class);180        jsonObject = (JSONObject) throwable;181        Assertions.assertThat(jsonObject).containsOnlyKeys("StackTrace");182        //Any reasonable way to test we have a stacktrace here?183    }184    @Test185    void format_mdc() {186        JSONLogFormatter logFormatter = new JSONLogFormatter("");187        LogRecord record = new LogRecord(Level.INFO, "Just a message");188        record.setLoggerName("JUnit.test");189        EnhancedLogRecord enhancedLogRecord = EnhancedLogRecord.wrap(record, true);190        MDC.put("key", "value");191        enhancedLogRecord.captureMDC();192        String message = logFormatter.format(enhancedLogRecord);193        CustomAssertions.assertThat(message).isJsonFormat();194        AbstractMapAssert mapAssert = CustomAssertions.assertThat(message).asMap();195        mapAssert.containsOnlyKeys("LoggerName",196                "LogMessage",197                "ThreadID",198                "ThreadName",199                "Level",200                "TimeMillis",201                "Timestamp",202                "LevelValue",203                "key");204        mapAssert.containsEntry("LoggerName", "JUnit.test");205        mapAssert.containsEntry("LogMessage", "Just a message");206        mapAssert.containsEntry("ThreadID", "1");207        mapAssert.containsEntry("ThreadName", "main");208        mapAssert.containsEntry("Level", "INFO");209        mapAssert.containsEntry("LevelValue", "800");...Source:ODLLogFormatterTest.java  
...44        ZonedDateTime zdtEnd = ZonedDateTime.ofInstant(record.getInstant(), ZoneId.systemDefault());45        CustomAssertions.assertThat(message).isODLFormat();46        CustomAssertions.assertThat(message).hasMessage("[INFO] [] [JUnit.test] Just a message\n");47        AbstractMapAssert mapAssert = CustomAssertions.assertThat(message).asMap();48        mapAssert.containsOnlyKeys("levelValue", "tid", "timeMillis");49        mapAssert.containsEntry("tid", "_ThreadID=1 _ThreadName=main");50        mapAssert.containsEntry("levelValue", "800");51        CustomAssertions.assertThat(message).hasTimeStamp(zdtStart, zdtEnd);52    }53    @Test54    void format_levelFINE() {55        ODLLogFormatter logFormatter = new ODLLogFormatter("");56        LogRecord record = new LogRecord(Level.FINE, "Just a message");57        record.setLoggerName("JUnit.test");58        // Ignored at level INFO59        record.setSourceClassName("be.atbash.runtime.Foo");60        record.setSourceMethodName("bar");61        ZonedDateTime zdtStart = ZonedDateTime.ofInstant(record.getInstant(), ZoneId.systemDefault());62        String message = logFormatter.format(record);63        ZonedDateTime zdtEnd = ZonedDateTime.ofInstant(record.getInstant(), ZoneId.systemDefault());64        CustomAssertions.assertThat(message).isODLFormat();65        CustomAssertions.assertThat(message).hasMessage("[FINE] [] [JUnit.test] Just a message\n");66        AbstractMapAssert mapAssert = CustomAssertions.assertThat(message).asMap();67        mapAssert.containsOnlyKeys("CLASSNAME", "METHODNAME", "tid", "levelValue","timeMillis");68        mapAssert.containsEntry("CLASSNAME", "be.atbash.runtime.Foo");69        mapAssert.containsEntry("METHODNAME", "bar");70        mapAssert.containsEntry("tid", "_ThreadID=1 _ThreadName=main");71        mapAssert.containsEntry("levelValue", "500");72        CustomAssertions.assertThat(message).hasTimeStamp(zdtStart, zdtEnd);73    }74    @Test75    void format_excludeFields() {76        ODLLogFormatter logFormatter = new ODLLogFormatter("tid,timeMillis,levelValue");77        LogRecord record = new LogRecord(Level.INFO, "Just a message");78        record.setLoggerName("JUnit.test");79        String message = logFormatter.format(record);80        CustomAssertions.assertThat(message).isODLFormat();81        CustomAssertions.assertThat(message).hasMessage("[INFO] [] [JUnit.test] Just a message\n");82        AbstractMapAssert mapAssert = CustomAssertions.assertThat(message).asMap();83        mapAssert.isEmpty();84    }85    @Test86    void format_withException() {87        ODLLogFormatter logFormatter = new ODLLogFormatter("");88        LogRecord record = new LogRecord(Level.INFO, "Just a message");89        record.setLoggerName("JUnit.test");90        // Ignored at level INFO91        Throwable exception = new RuntimeException("Exception message");92        record.setThrown(exception);93        ZonedDateTime zdtStart = ZonedDateTime.ofInstant(record.getInstant(), ZoneId.systemDefault());94        String message = logFormatter.format(record);95        ZonedDateTime zdtEnd = ZonedDateTime.ofInstant(record.getInstant(), ZoneId.systemDefault());96        CustomAssertions.assertThat(message).isODLFormat();97        CustomAssertions.assertThat(message).hasMessage("[INFO] [] [JUnit.test] Just a message\n");98        AbstractMapAssert mapAssert = CustomAssertions.assertThat(message).asMap();99        mapAssert.containsOnlyKeys("levelValue", "tid", "timeMillis");100        CustomAssertions.assertThat(message).hasTimeStamp(zdtStart, zdtEnd);101    }102    @Test103    void format_withMDC() {104        ODLLogFormatter logFormatter = new ODLLogFormatter("");105        LogRecord record = new LogRecord(Level.INFO, "Just a message");106        record.setLoggerName("JUnit.test");107        EnhancedLogRecord enhancedLogRecord = EnhancedLogRecord.wrap(record, true);108        MDC.put("key", "value");109        enhancedLogRecord.captureMDC();110        String message = logFormatter.format(enhancedLogRecord);111        CustomAssertions.assertThat(message).isODLFormat();112        CustomAssertions.assertThat(message).hasMessage("[INFO] [] [JUnit.test] Just a message\n");113        AbstractMapAssert mapAssert = CustomAssertions.assertThat(message).asMap();114        mapAssert.containsOnlyKeys("levelValue", "tid", "timeMillis", "key");115        mapAssert.containsEntry("key", "value");116    }117}...Source:MapAssert.java  
...54    return super.containsKeys(keys);55  }56  @SafeVarargs57  @Override58  public final MapAssert<K, V> containsOnlyKeys(K... keys) {59    return super.containsOnlyKeys(keys);60  }61  @SafeVarargs62  @Override63  public final MapAssert<K, V> containsValues(V... values) {64    return super.containsValues(values);65  }66  @SafeVarargs67  @Override68  public final MapAssert<K, V> doesNotContainKeys(K... keys) {69    return super.doesNotContainKeys(keys);70  }71  72  @SafeVarargs73  @Override...containsOnlyKeys
Using AI Code Generation
1package org.example;2import java.util.HashMap;3import java.util.Map;4import org.assertj.core.api.Assertions;5public class App {6    public static void main(String[] args) {7        Map<String, String> map = new HashMap<>();8        map.put("name", "John");9        map.put("age", "30");10        map.put("country", "USA");11        Assertions.assertThat(map).containsOnlyKeys("name", "age", "country");12    }13}14package org.example;15import java.util.HashMap;16import java.util.Map;17import org.assertj.core.api.SoftAssertions;18public class App {19    public static void main(String[] args) {20        Map<String, String> map = new HashMap<>();21        map.put("name", "John");22        map.put("age", "30");23        map.put("country", "USA");24        SoftAssertions softly = new SoftAssertions();25        softly.assertThat(map).containsOnlyKeys("name", "age", "country");26        softly.assertAll();27    }28}29package org.example;30import java.util.HashMap;31import java.util.Map;32import org.assertj.core.api.WithAssertions;33public class App implements WithAssertions {34    public static void main(String[] args) {35        Map<String, String> map = new HashMap<>();36        map.put("name", "John");37        map.put("age", "30");38        map.put("country", "USA");39        assertThat(map).containsOnlyKeys("name", "age", "country");40    }41}42package org.example;43import java.util.HashMap;44import java.util.Map;45import org.assertj.core.api.SoftAssertionsProvider;46public class App implements SoftAssertionsProvider {47    public static void main(String[] args) {48        Map<String, String> map = new HashMap<>();49        map.put("name", "John");50        map.put("age", "30");51        map.put("country", "USA");52        softly.assertThat(map).containsOnlyKeys("name", "age", "country");53        softly.assertAll();54    }55}containsOnlyKeys
Using AI Code Generation
1import org.assertj.core.api.AbstractMapAssert;2import org.assertj.core.api.Assertions;3import java.util.HashMap;4import java.util.Map;5public class ContainsOnlyKeys {6    public static void main(String[] args) {7        Map<String, Integer> map = new HashMap<>();8        map.put("a", 1);9        map.put("b", 2);10        map.put("c", 3);11        AbstractMapAssert<?, ?> assertion = Assertions.assertThat(map);12        assertion.containsOnlyKeys("a", "b", "c");13    }14}15Test Passed: containsOnlyKeys()containsOnlyKeys
Using AI Code Generation
1import org.assertj.core.api.MapAssert;2import java.util.HashMap;3import java.util.Map;4public class Main {5   public static void main(String[] args) {6      Map < String, Integer > map = new HashMap < > ();7      map.put("A", 1);8      map.put("B", 2);9      map.put("C", 3);10      map.put("D", 4);11      MapAssert < String, Integer > mapAssert = new MapAssert < > (map);12      mapAssert.containsOnlyKeys("A", "B", "C", "D");13   }14}15Previous: Java AssertJ MapAssert containsExactlyKeys() Method16Next: Java AssertJ MapAssert containsKey() MethodcontainsOnlyKeys
Using AI Code Generation
1import static org.assertj.core.api.Assertions.*;2import java.util.*;3public class MapContainsOnlyKeys {4   public static void main(String[] args) {5      Map<String, String> map = new HashMap<>();6      map.put("A", "a");7      map.put("B", "b");8      map.put("C", "c");9      map.put("D", "d");10      assertThat(map).containsOnlyKeys("A", "B", "C", "D");11   }12}13Expected :{A=a, B=b, C=c, D=d}14Actual   :{A=a, B=b, C=c, D=d}15at org.junit.Assert.assertEquals(Assert.java:115)16at org.junit.Assert.assertEquals(Assert.java:144)17at org.assertj.core.internal.Maps.assertContainsOnlyKeys(Maps.java:334)18at org.assertj.core.api.AbstractMapAssert.containsOnlyKeys(AbstractMapAssert.java:151)19at org.assertj.core.api.AbstractMapAssert.containsOnlyKeys(AbstractMapAssert.java:44)20at MapContainsOnlyKeys.main(MapContainsOnlyKeys.java:12)Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
