GVKun编程网logo

Java是否存在像Factory Girl这样的框架?(java有框架吗)

8

在本文中,我们将给您介绍关于Java是否存在像FactoryGirl这样的框架?的详细内容,并且为您解答java有框架吗的相关问题,此外,我们还将为您提供关于actorceoex3ndr介绍的acto

在本文中,我们将给您介绍关于Java是否存在像Factory Girl这样的框架?的详细内容,并且为您解答java有框架吗的相关问题,此外,我们还将为您提供关于actor ceo ex3ndr 介绍的actor,java,gwt,j2obj的框架体系、FactoryGirl在Rails中的使用、factory_girl_rails、inno-setup – 安装前检查Java是否存在的知识。

本文目录一览:

Java是否存在像Factory Girl这样的框架?(java有框架吗)

Java是否存在像Factory Girl这样的框架?(java有框架吗)

Factory Girl是Rails中的一个方便框架,可轻松创建模型实例进行测试。

在Factory Girl主页上:

factory_girl允许您快速定义每个模型的原型,并请求具有对于手头测试很重要的属性的实例。

一个示例(也来自主页):

Factory.sequence :email do |n|    "somebody#{n}@example.com"end# Let''s define a factory for the User model. The class name is guessed from the# factory name.Factory.define :user do |f|    # These properties are set statically, and are evaluated when the factory is    # defined.    f.first_name ''John''    f.last_name  ''Doe''    f.admin      false    # This property is set "lazily." The block will be called whenever an    # instance is generated, and the return value of the block is used as the    # value for the attribute.    f.email      { Factory.next(:email) }end

如果我需要用户,可以致电

test_user = Factory(:user, :admin => true)

这将使用户拥有工厂原型中指定的所有属性, 我明确指定的admin属性 除外 。还要注意,电子邮件工厂方法每次调用都会产生不同的电子邮件。

我认为为Java实现类似的东西应该很容易,但是我不想重新发明轮子。

PS:我对JMock和EasyMoc都了解,但是这里我不是在谈论模拟框架。

答案1

小编典典

一个可能的库是Usurper。

但是,如果要指定要创建的对象的属性,则Java的静态类型使框架毫无意义。您必须将属性名称指定为字符串,以便框架可以使用反射或Java
Bean内省来查找属性访问器。这将使重构更加困难。

仅更新对象并调用其方法要简单得多。如果要避免在测试中使用大量样板代码,则“
测试数据构建器”模式可以提供帮助。

actor ceo ex3ndr 介绍的actor,java,gwt,j2obj的框架体系

actor ceo ex3ndr 介绍的actor,java,gwt,j2obj的框架体系

原文来自: https://medium.com/@ex3ndr/ho...

actor ceo ex3ndr 介绍的actor,java,gwt,j2obj的框架体系, 这是actor架构中最具备吸引力,也是最强大的部分。 国内访问不到,因此转过来。

暂时原文原样转,未作翻译。

Stepan Korshakov
CEO of Actor.im
Jun 9, 2015
How Actor uses Java, GWT and J2ObjC for reuse 80% of code

We are a small startup, and we try to make big thing: implement messaging platform from scratch on modern technologies and one day we realized that implementing clients for three platforms is very hard. It is hard to implement correctly our custom networking protocol, implement all complex sync logic (right messaging app is not straightforward!) and adding new features fast. When we decided to find a way to unify our development we figured out that our Android application internally is very optimized and have the best internal architecture for iOS and Web too. This happens because of our experience in the development of mobile messaging: 5 messengers for Android and WinPhone, Telegram S for Android and our CEO won 3 contests of a rapid development of mobile messaging applications. In this way, this architecture is already 3-rd generation and time-proven.

At the core of the application, we have Actor Model implementation (that’s why we have this name of our project), and we also use Actors in our server: Scala Akka. In one week, we implemented a small subset of Akka Actors. Actors are excellent for web because we have divided work to very small pieces and it doesn’t affect UI. We are extremely lucky. We only removed support for Typed Actors because we can’t use reflection in Web, and we can stripe all reflection information in generated code to reduce a size and improve execution speed.

Good Actor Model implementation doesn’t allow direct working with UI. We created very simple MVVM implementation, and it works very well, reduced listener/subscriber amount to zero and we can easily sync any object in UI with a single line of code. MVVM works well in Web too thanks to Angular/ReactJS. Luck doesn’t leave us in this too.

Also, we have a custom implementation of Protobuf serialization — BSer. It is compatible with Protobuf, but much more easy to use. Maybe our implementation bit slower, but we don’t find any problems in speed. Wire use reflection, and it is implemented by someone who doesn’t know Java (Sorry guys, I like you, Square). Original Protobuf is harder to use and generate a lot of code. Also, we have some additional features on top of original protobuf like inheritance.

To build smooth lists experience in Android, we created a complex logic of loading and sync of UI and DataBase. When we started to optimize iOS we found that iOS have same problems with lists that Android have (and one more) and this lists engine works well for iOS too.

Our storage of data was separated to key-value storages and special list storages that store ordered lists of objects. This idea reduce a lot of logic that need to implement for each platform: we need to implement ~10–15 sql queries and library is ready to use.

Java has best tools for profiling and debugging in the market and if you optimize apps. The developer can easily solve most of the problems with reach toolkits like Memory Analyzer Tool, IntelliJ IDEA, and others.

And in a month we separated all our code from Android and built libraries with GWT and J2ObjC.

We performed some speed checks. One interesting was testing crypto in Web. We compare a subset of BouncyCastle for Java that was converted to JS by GWT and various plain-js libraries like CryptoJS. Surprisingly, GWT crypto work faster up to two times when using RSA with 1024 and 2048 keys. Unfortunately GWT crypto can’t generate keys and just freeze, and we use CryptoJS for key generation and we decided not to fix this bugs in random number generation. The main reason is that BouncyCastle originally was developed for slowest JavaME phones and contains best algorithms and execution optimizations. At iOS, we haven’t spotted any noticeable difference. Using plain java implementations, we can make any crypto that is a huge advantage for us.

We also checked the size of libraries. For the web all messaging logic with custom proto, serialization and storage interfaces after all optimizations are only 90kb. iOS Application extended by 1MB, much less than Swift runtime.

After this, we dramatically reduce our codebase size and usually when we find bug — it is a bug in the library, and we fix it at all platforms by one single fix. Java/Android developers are much easier to hire than iOS devs and also easier than finding good frontend-developer. Now we and our customers can build custom native applications with much smaller development costs.

We are currently in private beta, and if someone want to try our product, in there’s company they may ask me by writing me email steve@actor.im. We usually suggest our product for businesses with 500+ employees. Also we are interested in partners that can help us to make Actor global.

Go to the profile of Stepan Korshakov
Stepan Korshakov

CEO of Actor.im

Go to the profile of Stepan Korshakov
Never miss a story from Stepan Korshakov, when you sign up for Medium. Learn more

FactoryGirl在Rails中的使用

FactoryGirl在Rails中的使用

FactoryGirl is a gem aimed at replacing Fixtures. If you wonder that, take a look at what''s wrong with fixtures.

1. Add factory_girl_rails to your Gemfile

group :test, :development do
    gem ''factory_girl_rails''
end

2. Add to your spec/rails_helper config block:

RSpec.configure do |config|
  #  Other config code
  config.include FactoryGirl::Syntax::Methods  # Add this line
end

3. Define factory

FactoryGirl.define do
    factory :user do
        username ‘foo’
        password ‘secret’
        email {“#{username}@example.com”} # define value dynamically, pass a block
    end
end

4. Use the factory in your spec

user = create(:user)
user = create(:user, username: ‘xiaochuang’, password: ‘xiaochuangchuang’)  # Override the default value.
user = build(:user)  # Will not save to database, faster than create

5. tips and tricks

1. use sequence in model field

Say your user model has a validation of uniqueness for the attribute username, then if you call create(:user) multiple times without passing in the username, you’ll get an error. In this case, you can use sequence to auto generate username for you.

factory :user do
    sequence(:username) {|n| “user#{n}”}
end

Now everytime you create(:user) will return a user with a different username in the form: user0, user1, user2...

2. Use associations

Assume you have a Post model that references to a User.

factory :user do
    username ‘xiaochuang’
    password ‘xiaochuangchuang’
end

factory :post do
    title ‘A new post’
    user #Yes, it’s just that simple.
end

However, the above usage assume that the user field is name user, if not, you can use association to customize the behavior.
association :author, factory: :user, username: ‘xiaochuang’

3. Nested factory

Say your user model have an attribute called admin, this is used frequently and you don’t want to pass in the admin attribute very time. In this case, you can use a nested factory:

factory :user do
    username ‘xiaochuang’
    password ‘xiaochuangchuang’
    admin false
    
    factory :admin do
        admin true
    end
end

In your spec, you can just create(:admin) to create a user that set the admin to true.

4. Use build instead of create if possible.

By using create(:model), you create the model instance and save if to the database. In many cases if you don’t have to save it the the database, you can use build(:model). This will build an instance but wouldn’t save to the database, which makes it a little bit faster.
And also, in many cases you if you can just Model.new, you can bypass factories at all, which is even faster.

有任何意见或建议,或者发现文中任何问题,欢迎留言!

更多文章请访问个人博客
作者:邹小创
Github:https://github.com/ChrisZou
邮件:happystriving@126.com

factory_girl_rails

factory_girl_rails

通过一条 generator 命令来生成 夹具 文件,命令如下:


require ''generators/factory_girl''

module FactoryGirl
  module Generators
    class ModelGenerator < Base
      argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
      class_option :dir, :type => :string, :default => "test/factories", :desc => "The directory where the factories should go"
      
      def create_fixture_file
        template ''fixtures.rb'', File.join(options[:dir], "#{table_name}.rb")
      end
    end
  end

而产生的 template.rb 则是这个



FactoryGirl.define do
  factory :<%= singular_name %> do
<% for attribute in attributes -%>
<%= attribute.name %> <%= attribute.default.inspect %>
<% end -%>
  end
end

再看看其它代码,生成下面两个之一

test/factories
spec/factories

生成的是 Factory (replace fixture) fixtures.rb => factories.rb

从这可以证实


require ''factory_girl''
require ''rails''

module FactoryGirl
  class Railtie < Rails::Railtie

    initializer "factory_girl.set_fixture_replacement" do
      generators = config.respond_to?(:app_generators) ? config.app_generators : config.generators

      if generators.options[:rails][:test_framework] == :rspec
        generators.fixture_replacement :factory_girl, :dir => ''spec/factories''
      else
        generators.test_framework :test_unit, :fixture => false, :fixture_replacement => :factory_girl
      end
    end

    initializer "factory_girl.set_factory_paths" do
      FactoryGirl.definition_file_paths = [
          File.join(Rails.root, ''factories''),
          File.join(Rails.root, ''test'', ''factories''),
          File.join(Rails.root, ''spec'', ''factories'')
      ]
    end

    config.after_initialize do
      FactoryGirl.find_definitions
    end
  end
end

inno-setup – 安装前检查Java是否存在

inno-setup – 安装前检查Java是否存在

我正在为一个jar应用程序创建一个Inno安装程序。
我现在要做的是在继续安装之前检查java是否存在。所以我只需要确保用户能够运行:
java -jar my-app.jar

我现在正在做的是:

[Code]

function InitializeSetup(): Boolean;
var
  ErrorCode: Integer;
  JavaInstalled : Boolean;
  Result1 : Boolean;
begin
  JavaInstalled := RegKeyExists(HKLM,'SOFTWARE\JavaSoft\Java Runtime Environment\1.6');
  if JavaInstalled then
  begin
    Result := true;
  end else
    begin
      Result1 := MsgBox('This tool requires Java Runtime Environment version 1.6 or newer to run. Please download and install the JRE and run this setup again. Do you want to download it Now?',mbConfirmation,MB_YESNO) = idYes;
      if Result1 = false then
      begin
        Result:=false;
      end else
      begin
        Result:=false;
        ShellExec('open','http://javadl.sun.com/webapps/download/AutoDL?BundleId=33787','',SW_SHOWnorMAL,ewNowait,ErrorCode);
      end;
    end;
  end;
end;

我的问题是:

>检查注册表足以确保java的home目录将在PATH中? (能够在控制台中运行“java”)
>如果安装了更高版本的java,那么注册表中的那个密钥是否存在,否则我将不得不检查每个更高版本的可能?
>有没有人有更好的方式来下载java,而不仅仅是显示一个弹出窗口,并将用户带到下载页面?

解决方法

我希望有人觉得这很有用,我所做的是重新使用Inno Setups wiki中的一些代码, >与版本比较为数:
{ Both DecodeVersion and Compareversion functions where taken from the  wiki }
procedure DecodeVersion (verstr: String; var verint: array of Integer);
var
  i,p: Integer; s: string;
begin
  { initialize array }
  verint := [0,0];
  i := 0;
  while ((Length(verstr) > 0) and (i < 4)) do
  begin
    p := pos ('.',verstr);
    if p > 0 then
    begin
      if p = 1 then s:= '0' else s:= copy (verstr,1,p - 1);
      verint[i] := StrToInt(s);
      i := i + 1;
      verstr := copy (verstr,p+1,Length(verstr));
    end
    else
    begin
      verint[i] := StrToInt (verstr);
      verstr := '';
    end;
  end;

end;

function Compareversion (ver1,ver2: String) : Integer;
var
  verint1,verint2: array of Integer;
  i: integer;
begin

  SetArrayLength (verint1,4);
  DecodeVersion (ver1,verint1);

  SetArrayLength (verint2,4);
  DecodeVersion (ver2,verint2);

  Result := 0; i := 0;
  while ((Result = 0) and ( i < 4 )) do
  begin
    if verint1[i] > verint2[i] then
      Result := 1
    else
      if verint1[i] < verint2[i] then
        Result := -1
      else
        Result := 0;
    i := i + 1;
  end;

end;

{ Here's my code }
function InitializeSetup(): Boolean;
var
  ErrorCode: Integer;
  JavaVer : String;
  Result1 : Boolean;
begin
    RegQueryStringValue(HKLM,'SOFTWARE\JavaSoft\Java Runtime Environment','CurrentVersion',JavaVer);
    Result := false;
    if Length( JavaVer ) > 0 then
    begin
        if Compareversion(JavaVer,'1.6') >= 0 then
        begin
            Result := true;
        end;
    end;
    if Result = false then
    begin
        Result1 := MsgBox('This tool requires Java Runtime Environment v1.6 or older to run. Please download and install JRE and run this setup again.' + #13 + #10 + 'Do you want to download it Now?',MB_YESNO) = idYes;
        if Result1 = true then
        begin
            ShellExec('open','http://www.java.com/en/download/manual.jsp#win',ErrorCode);
        end;
    end;
end;

感谢所有的帮助

我们今天的关于Java是否存在像Factory Girl这样的框架?java有框架吗的分享已经告一段落,感谢您的关注,如果您想了解更多关于actor ceo ex3ndr 介绍的actor,java,gwt,j2obj的框架体系、FactoryGirl在Rails中的使用、factory_girl_rails、inno-setup – 安装前检查Java是否存在的相关信息,请在本站查询。

本文标签: