diff --git a/ee/lib/tasks/gitlab/license.rake b/ee/lib/tasks/gitlab/license.rake
index aa300d0698646ad1dd838b504a1125355a463ebd..a727bfd89a00d5d08cd3f2b3fbac74e1bdcd6d8f 100644
--- a/ee/lib/tasks/gitlab/license.rake
+++ b/ee/lib/tasks/gitlab/license.rake
@@ -22,9 +22,10 @@ namespace :gitlab do
       license_file = ENV.fetch(flag, default_license_file)
 
       if File.file?(license_file)
-        if ::License.create(data: File.read(license_file))
+        begin
+          ::License.create!(data: File.read(license_file))
           puts "License Added:\n\nFilePath: #{license_file}".color(:green)
-        else
+        rescue Gitlab::License::Error, ActiveRecord::RecordInvalid
           puts "License Invalid:\n\nFilePath: #{license_file}".color(:red)
           raise "License Invalid"
         end
diff --git a/ee/spec/tasks/gitlab/license_rake_spec.rb b/ee/spec/tasks/gitlab/license_rake_spec.rb
index 8d779500ef80d893a921de729abb4ce8729667b4..f5fb89c569a9427e12d8421726ff62fb237278cd 100644
--- a/ee/spec/tasks/gitlab/license_rake_spec.rb
+++ b/ee/spec/tasks/gitlab/license_rake_spec.rb
@@ -36,11 +36,11 @@
         end
 
         context 'and contains a valid license' do
-          let(:license_file_contents) { 'valid contents' }
+          let(:license) { build(:gitlab_license) }
+          let(:license_file_contents) { license.export }
 
           it 'succeeds in adding the license' do
             expect_file_read(license_path, content: license_file_contents)
-            expect(License).to receive(:create).with(data: license_file_contents).and_return(true)
 
             expect { subject }.not_to raise_error
           end
@@ -51,7 +51,17 @@
 
           it 'fails to add the license' do
             expect_file_read(license_path, content: license_file_contents)
-            expect(License).to receive(:create).with(data: license_file_contents).and_return(false)
+
+            expect { subject }.to raise_error(RuntimeError, "License Invalid")
+          end
+        end
+
+        context 'but contains an expired license' do
+          let(:license) { build(:gitlab_license, expires_at: Date.current - 1.month) }
+          let(:license_file_contents) { license.export }
+
+          it 'fails to add the license' do
+            expect_file_read(license_path, content: license_file_contents)
 
             expect { subject }.to raise_error(RuntimeError, "License Invalid")
           end
@@ -60,7 +70,8 @@
     end
 
     context 'when GITLAB_LICENSE_FILE env variable is not set' do
-      let(:license_file_contents) { 'valid contents' }
+      let(:license) { build(:gitlab_license) }
+      let(:license_file_contents) { license.export }
 
       context 'when default valid license file does exist' do
         before do
@@ -69,7 +80,6 @@
 
         it 'succeeds in adding the license' do
           expect_file_read(default_license_path, content: license_file_contents)
-          expect(License).to receive(:create).with(data: license_file_contents).and_return(true)
 
           expect { subject }.not_to raise_error
         end