How to resolve “You have not started an Objectify context” in JUnit?

后端 未结 5 747
旧巷少年郎
旧巷少年郎 2021-01-17 16:55

I\'ve got some Objectify test code running in JUnit and I\'m getting this error:

java.lang.IllegalStateException: You have not started an Objectify context.          


        
5条回答
  •  爱一瞬间的悲伤
    2021-01-17 17:29

    Improving michael-osofsky answer, I add this to my ofy helper class

    public static void registerDataModel() {
        try {
            factory().register(Profile.class);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    

    and remplace

    ObjectifyRegistrar.registerDataModel();
    

    for this

    OfyService.registerDataModel();
    

    OfyService.java

    public static void registerDataModel() {
        try {
            factory().register(Profile.class);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    

提交回复
热议问题